my project Capacitive Sensing LED dimmer 12F1822 via PWM


Closed Thread
Results 1 to 40 of 43

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    @Acetronics2... great minds must think alike
    That's pretty much what I did.

    @Charlie... my design is specifically designed to run on battery power. The PIC current drain should not change by much. Unless one starts trying to supply current via other pins. That is why I would limit other pin outs to supplying ground when active. (I think this is correct, but I am open for debate)

    My intent was to specifically create a Camping/Emergency light that could be dimmed for maximum battery life. My crude measurements with the PWM output is that on low the current is around 1, 6, 25, 50, 100 milliamps respectivily with each of the 5 steps.

    I really was not going to do much, if anything, else with the other pins.

    In this case I think the current draw of the PIC should be quite stable and very low.
    If one starts doing other things with the other pins then all bets are off with the zener/resistor supply.

    And, hey, since I am a hobbiest it might be fun to let the smoke out of a PIC to learn a little. See how much abuse one of these little jems can take and keep on ticking. (evil grin)

    Here is my code that turns the light off after approx 30 min.
    I may modify it to blink the led to warn that the 30 min is about to expire so the user can hit the touch sense pad again to go another 30. The sky is the limit and that is the beauty (as you all surely know) of software control. Can we all say "feature creep".

    PS. As I look at the code I wonder if some are the pause statements sprinkled around are really necessay.
    One thing I do notice is that the touch sense requires a definate quick tap to be detected. I think this is because the calculation inside the interrupt routine demands a change of approx 20% from the last INT to be counted as a "touch". One can have all sorts of fun playing with some of those numbers and seeing how it changes the Capacitive sensitivity.



    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : Dwight and others                                 *
    '*  Notice  : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 11/29/2013                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :   PIC12F1822                                                *
    '*          :                                                   *
    '****************************************************************
    #CONFIG
      __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_SWDTEN & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
      __CONFIG _CONFIG2, _LVP_OFF
    #ENDCONFIG
    
    DEFINE  OSC 8                'tell picbasic what speed the oscillator is
    
    INCLUDE "DT_INTS-14.bas"     ' 
    INCLUDE "ReEnterPBP.bas"     ' Include if using PBP interrupts
    
    OSCCON = %01110010         
                            '0 = 4x PLL is disabled
                            ' 1110 = 8 MHz 
                            '     0 = unused
                            '      1X = Internal oscillator
    
    
    ANSELA = %00000100        'make port A.2 is analog
    TRISA  = %00000100        'port A.2 is input
    WPUA   = %00000000        'turn off weak pull ups
    
    APFCON = %00000001        'Alternate Pin Function Control Reg (default %00000000)
                              'move CCP to PortA.5 (for PWM output)
    OPTION_REG = %10000111     
                            '1 = All weak pull-ups are disabled (except MCLR, if it is enabled)
                            ' 0 = Interrupt on falling edge of RB0/INT pin
                            '  0 = Timer0 clock is internal cycle clock (Fosc/4)
                            '   0 = Increment on low-to-high transition on RA4/T0CKI pin
                            '    0 = Prescaler is assigned to the Timer0 module
                            '     111 = 1:256 Timer0 prescaler rate
                    
    T1CON = %11000001       'enable timer 1 (bit 0) & source the clock from the CPS module (bit 6 & 7 =11
    
    CPSCON0 = %10001100     'set the CPS module highest frequency availabe (for vcc mode) + timer0 clock sourced from CPS module. (BIT 1)
                            '1 = CPS module is enabled
                            ' 0 = CPS module is in the low range. Internal oscillator voltage references are used.
                            '  00 = unused
                            '    11 = Oscillator is in High Range. Charge/Discharge Current is nominally 18 µA
                            '      0 = Capacitive Sensing Oscillator Status bit (only readable)
                            '       0 = Timer0 clock source is controlled by the core/Timer0 module and is FOSC/4
    
    CPSCON1 = %00000010        '0010 = channel 2, (CPS2)
    
    CM1CON0 = 0   ' COMPARATOR OFF
    CM1CON1 = 0   ' COMPARATOR OFF
    
    CPS2_PRESENT    var WORD
    CPS2_THRESHOLD  var WORD
    CPS2_LAST       var WORD
    
    '       Software variables
    '       ==================
    Duty    VAR WORD
    stepp   var byte
    AutoOFF var word
    
    
    ' ----------------[ I/O Definitions ]-----------------------------------
    FET VAR PORTA.5  ' PWM output to control the FET switch
    
    '       Software/Hardware initialisation
    '       ================================
    PAUSE 100                           ' OSC settle delay
    Autooff = 0
    Duty = 0
    stepp = 5
    hpwm 1,255,250                     ' Start with Light full ON
    pause 1000
         
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR0_INT,  _Timer0_Int,   pbp,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
     
    TMR0 = 0                  'clear  TIMER0
    @ INT_ENABLE  TMR0_INT    'enable timer0 interrupts
    '***********************************************************************
    '------------------------------< Main program >-------------------------
    Main:
    if autooff = 0 then skipoff
    if stepp = 0 then autooff = 0
    
    autooff = autooff + 1
    if autooff = 600 then    '(20=1 minute, 600=30 minutes) X delay till AutoOFF
        autooff = 0
        hpwm 1,0,250  'turn LED off after X delay
        stepp = 0
    endif
    
    SkipOFF:
    pause 1000   ' not a whole lot going on here
    
    goto main
    
    '*****************************************************************************
    Timer0_Int:
    @ INT_DISABLE  TMR0_INT   ' stop timer0 interrupts while we're in here
    pause 25
    
    CPS2_PRESENT = TMR1                                ' take a snapshot of Timer1's present count.
    CPS2_THRESHOLD = CPS2_LAST - ((CPS2_LAST/10)*2)    ' this sets the 'trigger' up for a 20% diversion (finger press)
    CPS2_LAST = CPS2_PRESENT                           ' store away the present timer0count for the next time we come into the interrupt routine
    
    if cps2_present < cps2_threshold then
        autooff = 1
        stepp = stepp +1
        if stepp>6 then stepp=0            ' check for rollover
        lookup stepp, [0,1,16,64,128,255,255],duty  'choose level, 5 levels plus OFF
        if stepp=6 then 
            hpwm 1,0,1000    'off              ' arrive here when brightness is MAX
            pause 100                          '    So blink to let user know.
            hpwm 1,255,1000  'on
            pause 100
            hpwm 1,0,1000    'off
            pause 100
            hpwm 1,255,1000  'on              ' blink to show that you are at the highest level
        endif    
        hpwm 1,duty,250                    ' else set new level                       ' button pressed so increment stepp  
        pause 100
    endif
    
    pause 50      
    TMR0 = 0                  ' clear TIMER0    
    TMR1 = 0                  ' clear TIMER1
    @ INT_ENABLE  TMR0_INT    ' re-enable timer0 interrupt
    @ INT_RETURN   
    
    end
    Last edited by Heckler; - 4th December 2013 at 03:49.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  2. #2
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Just wondering if anybody has taken the time to bread board up the circuit and see how it works?

    If you do build the circuit I suggest you lower the value of the Resistor in the Zener ckt to around 4700 ohms.
    (Assuming you are trying to drive it with 12 Volts)


    I still need to learn about how to best design a small circuit board with an included touch pad. I still am unsure if I need a surrounding ground to make the touch sense work better.

    My testing shows that it does not seem to make much difference.


    Here is a link to a video of my circuit working

    (the video is in my drop box so I am not sure how this will work, its about 8 meg)

    https://dl.dropboxusercontent.com/u/...LED_dimmer.m4v
    Last edited by Heckler; - 4th December 2013 at 04:18.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  3. #3
    Join Date
    Jan 2011
    Location
    Sydney, Australia
    Posts
    172


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Hi Dwight,

    Are you looking to make these Camping/Emergency lights as a commercial venture or just for your own use?
    If it is the latter, you would probably be best served (from a purist aspect) to throw a bit more coin at an LDO voltage regulator.

    From my own curiosity standpoint, what have you used for the capacitive touch sensor in your prototype and how do you plan to implement the sensor in your production unit?

    Cheers
    Barry
    VK2XBP

  4. #4
    Join Date
    Apr 2016
    Location
    Mi, USA
    Posts
    24


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Hello!
    I'm new here, but I like your capacitive touch program example. The only difference I would like to be able to do is have 3 touch pads, one for brightness up, brightness down, and one for on/off. I just wonder where
    I would put the code that would scan the three cps inputs, would it be in the main program area, or in the interrupt itself? I'm also driving 5 leds that indicate what brightness I am at. Currently, I have the scan code in the main program area, and the determination of which switch is pressed in the interrupt portion. However, I keep having problems with the touch working intermittently. Can you see where I am having problems?
    I am using a PIC16LF722A for the micro.


    '************************************************* ***************
    '* Notes : Copyright (c) 2016 *
    '* : Editor MicroCode Studio Plus PBP 3.0.6.0 *
    '* : LS1236-A Startrol Touch Controller *
    '* : MUST BUILD PROJECT WITH MPASM ASSEMBLER *
    '* : Device = 16LF722A-I/SO *
    '* Version : 1y.0 *
    '* Name : LS1236_1y.BAS *
    '* Author : R. WRIGHT *
    '* : *
    '************************************************* ***************

    'SET CONFIGURATION FOR INTERNAL OSCILLATOR WITH PLL MULTIPLIER ON
    #CONFIG
    __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDTE_OFF & _BOR_OFF & _PLLEN_ON
    #ENDCONFIG


    define OSC 4


    INCLUDE "DT_INTS-14.bas" ' PBP interrupt routines (MUST BE IN SAME FOLDER)
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
    ' Courtesy of Derryl Taylor, PBP Wizard!

    OSCCON = %00010000 'SETS INT OSC TO 4Mhz
    ANSELB = %00000111 'SETS INPUTS B0-B2 AS ANALOG
    TRISB = %00000111 'SETS INPUTS B0-B2 AS INPUTS
    WPUB = %00000000 'DISABLES WEAK PULL-UP BITS
    'TRISC = %00001000 'SETS INPUT C3 AS INPUT

    OPTION_REG = %10000111
    '1 = All weak pull-ups are disabled (except MCLR, if it is enabled)
    ' 0 = Interrupt on falling edge of RB0/INT pin
    ' 0 = Timer0 clock is internal cycle clock (Fosc/4)
    ' 0 = Increment on low-to-high transition on RA4/T0CKI pin
    ' 0 = Prescaler is assigned to the Timer0 module
    ' 111 = 1:256 Timer0 prescaler rate

    T1CON = %11000001 'enable timer 1 (bit 0) & source the clock from the CPS module (bit 6 & 7 =11)
    'set the CPS module highest frequency availabe (for vcc mode) + timer0 clock sourced from CPS module. (BIT 1)


    CPSCON0 = %10001100 '1 = CPS module is enabled
    ' 000 = unused
    ' 11 = Oscillator is in High Range. Charge/Discharge Current is nominally 18 µA
    ' 0 = Capacitive Sensing Oscillator Status bit (only readable)
    ' 0 = Timer0 clock source is controlled by the core/Timer0 module and is FOSC/4

    CPSCON1 = %00000001 '0001 = channel 1, (CPS1)


    ' Software variables

    CPSX_PRESENT var WORD 'VARIABLE FOR BUTTON CURRENT VALUE
    CPSX_THRESHOLD var WORD 'VARIABLE FOR BUTTON THRESHOLD VALUE
    CPSX_LAST var WORD 'VARIABLE FOR PREVIOUS BUTTON VALUE
    Duty VAR WORD 'VARIABLE FOR DUTY CYCLE
    stepp var byte 'VARIABLE FOR BRIGHTNESS LEVEL
    incnt var byte 'BUTTON PUSH INCRIMENT
    LOCLEV VAR BYTE 'LOCAL MODULE BRIGHTNESS LEVEL
    REMLEV VAR BYTE 'REMOTE MODULE BRIGHTNESS LEVEL
    IOTOG VAR BIT 'TOGGLE BIT FOR I/O BUTTON

    ' variables used by interrupt handler, comment out as per assembler
    wsave VAR BYTE $20 SYSTEM ' location for W if in bank0
    'wsave VAR BYTE $70 SYSTEM ' alternate save location for W
    ' if using $70, comment wsave1-3

    ' --- IF any of these three lines cause an error ?? ------------------------
    ' Comment them out to fix the problem ----
    ' -- Which variables are needed, depends on the Chip you are using --
    wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1
    'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2
    'wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3





    ' SET DEVICE I/O PINS


    LED1 VAR PORTA.1 ' SET LED LEVEL INDICATORS
    LED2 VAR PORTA.2
    LED3 VAR PORTA.3
    LED4 VAR PORTA.4
    LED5 VAR PORTA.5


    ' Software/Hardware initialization

    PAUSE 50 ' OSC settle delay
    Duty = 255 ' SET INITIAL BRIGHTNESS TO FULL ON
    stepp = 5 ' SET INITIAL LOOKUP TABLE TO 5
    incnt = 1 ' set input counter to input 1
    iotog = 1 ' SET I/O BUTTON TOGGLE TO high
    hpwm 1,DUTY,1000 ' INITIALIZE HARDWARE PWM TO SELECTED LEVELS
    HIGH LED5 ' TURN ON OUTPUT FOR LED 5
    pause 1000 ' LET EVERYTHING SETTLE

    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler TMR0_INT, _Timer0_Int, pbp, yes
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    TMR0 = 0 'clear TIMER0
    @ INT_ENABLE TMR0_INT 'enable timer0 interrupts
    PAUSE 100


    '************************************************* **********************
    '------------------------------< Main program >-------------------------

    'SCAN INPUTS UNTIL FINGER DEPRESSION IS DETECTED
    Main:

    incnt = 2
    cpscon1 = %00000001 ' 0001 = channel 1, (CPS1), DOWN BUTTON
    ANSELB = %00000010 ' SETS INPUT B1 AS ANALOG
    TRISB = %00000010 ' SETS INPUTS B1 AS INPUTS
    PAUSE 100
    INcnt = 3
    cpscon1 = %00000010 ' 0010 = channel 2, (CPS2), UP BUTTON
    ANSELB = %00000100 ' SETS INPUT B2 AS ANALOG
    TRISB = %00000100 ' SETS INPUT B2 AS INPUT
    PAuse 100
    incnt = 1
    cpscon1 = %00000000 ' 0000 = channel 0, (CPS0), 1/0 BUTTON
    ANSELB = %00000001 ' SETS INPUT B0 AS ANALOG
    TRISB = %00000001 ' SETS INPUT B0 AS INPUT
    Pause 100 ' WAIT FOR INTERRUPT

    Goto Main

    '************************************************* ****************************
    Timer0_Int:
    @ INT_DISABLE TMR0_INT ; stop timer0 interrupts while we're in here
    pause 100

    ' store away the present timer0 count for the next time we come into the interrupt routine
    CPSX_PRESENT = TMR1 ' take a snapshot of Timer1's present count.
    CPSX_THRESHOLD = CPSX_PRESENT - ((CPSX_LAST/10)*5) ' this sets the 'trigger' up for a 50% diversion (finger press)
    CPSX_LAST = CPSX_PRESENT

    'DETERMINES WHICH BUTTON HAS BEEN PRESSED
    IF CPSX_PRESENT < CPSX_THRESHOLD THEN
    if (incnt = 3) AND (STEPP > 1) then ' down button pressed so decriment stepp
    stepp = stepp - 1
    GOTO SKIP1
    ENDIF
    If (incnt = 2) AND (STEPP < 5) then ' up button pressed so incriment stepp
    stepp = stepp + 1
    GOTO SKIP1
    endif
    IF (INCNT = 1) AND (iotog = 0) THEN ' 1/0 button pressed for off
    STEPP = 0
    IOTOG = 1
    GOTO SKIP1
    ENDIF
    IF (INCNT = 1) AND (iotog = 1) THEN ' 1/0 button pressed for on
    STEPP = 5
    IOTOG = 0
    ENDIF


    ' lights appropriate leds according to the current lookup table value
    SKIP1:
    IF STEPP = 5 THEN
    HIGH LED5
    ENDIF
    IF STEPP = 4 THEN
    HIGH LED4
    ENDIF
    IF STEPP = 3 THEN
    HIGH LED3
    ENDIF
    IF STEPP = 2 THEN
    HIGH LED2
    ENDIF
    IF STEPP = 1 THEN
    HIGH LED1
    ENDIF
    if stepp = 0 THEN
    LOW LED1
    endif



    ' SETS HARDWARE PWM OUTPUT 1 TO DESIRED LEVEL
    lookup stepp, [0,16,32,64,128,255],duty ' choose level, 5 levels plus OFF
    hpwm 1,duty,1000 ' set new level
    ' pause 100

    ENDIF


    pause 100
    TMR0 = 0 ' clear TIMER0
    TMR1 = 0 ' clear TIMER1
    @ INT_ENABLE TMR0_INT ; re-enable timer0 interrupt
    @ INT_RETURN

    resume

  5. #5
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    Quote Originally Posted by Heckler View Post
    @Charlie... my design is specifically designed to run on battery power. The PIC current drain should not change by much. Unless one starts trying to supply current via other pins.
    Respectfully, that's simply wrong. Every different instruction uses a slightly different amount of current. Those current changes result in noise. Put a cap across the zener to resolve it. And if minimal current is your objective, you are wasting more in the zener than is used in the PIC. Use a regulator and use the PIC sleep capability to get down to minimal current. If you are really interested in parts count, have a look at some of the other PIC devices. The 12HV615 for instance - I've used this as a touch dimmer for LED lighting - can run off of 12V directly (well, through a resistor) because it has an integrated regulator. The method it uses, though, does still waste a little current.

  6. #6
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: my project Capacitive Sensing LED dimmer 12F1822 via PWM

    @Barry... building this just for hobby fun. And always enjoy using something I build myself. Probably just build a few for myself. Possibly thinking of using it as I teach electronics to Boy Scouts for the electronics merit badge. The final requirement is for them to build a project.

    The touch sensor on my proto type is a bit of copper tape (Scotch #1245) stuck to my desk with regular scotch tape over the top as an insulator.

    In the final I envision a small ckt board, say 1" x 2" or so, with a "touch" area for the button.

    @Charlie... I did not know that there was an "HV" pic. Thats cool. Might have to take a look at getting a couple of samples.

    So regarding the noise... A small cap across the power input is probably a good idea. What problems might the noise cause? Intermittent operation? I took a look with an o-scope across the power input (vss, vdd) and didn't see what I thought was a lot of noise (lot is a relative term though)

    Yeah I am clear on the fact that a Zener supply is wastefull, but hey 2ma, though your point is well taken. I just don't have one of those LVD regulators on hand.

    Again, just an exercise in understanding how to set-up and use the cap sense and maybe come up with a little fun project.

    Really my signature line says it all for me.
    Kinda like, why does one seemed determined to solve a rubics cube. (and no, I can't)
    BUT I can easily waste? 4 or 5 hours trying to figure out the cap sense set up and coding on a little 8 pin pic
    Last edited by Heckler; - 5th December 2013 at 03:31.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

Similar Threads

  1. 16F726 Capacitive sensing module
    By Byte_Butcher in forum General
    Replies: 39
    Last Post: - 15th May 2014, 19:40
  2. Need help setting up Capacitive Sense 12F1822
    By Heckler in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th November 2013, 21:10
  3. Replies: 1
    Last Post: - 6th August 2012, 10:57
  4. Capacitive sensing module on 16f1936
    By grahamg in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 25th April 2010, 14:17
  5. mTouch capacitive sensing
    By jrprogrammer in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 1st November 2008, 22:54

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts