Touch switch ON/OFF


Closed Thread
Results 1 to 6 of 6
  1. #1

    Default Touch switch ON/OFF

    Hi Folks,
    currently I'm trying adapt this touch switch code which I found in code examples (http://www.picbasic.co.uk/forum/showthread.php?t=2671)
    The code is works fine as is, but I need to modify it to respond only to one input pin GPIO.0 I don't need three inputs.
    Here is my modified code (not working):
    Code:
    '   Few cents touch sensor
    '   ======================
    '   Using : PIC12F683
    '    
    '   Circuit is quite simple. One pin(GP4) is used as a signal generator.
    '   This pin send signal to all "virtual push button" (GPIO<2:0>)
    '   via 3 X 22K resistor. The touch sensor could be almost everything 
    '   conductive.  
    '
    '   Once a sensor is touched, the program will blink a LED as follow
    '                            GPIO.0 => 1 Blink
    '                            GPIO.1 => 2 Blink
    '                            GPIO.2 => 3 Blink
    
    
    '   PIC config and programming mode
    '   ===============================
    #CONFIG  
    cfg = _INTOSCIO
    cfg&= _WDT_OFF                 
    cfg&= _PWRTE_OFF
    cfg&= _MCLRE_OFF
    cfg&= _CP_OFF
    cfg&= _CPD_OFF
    cfg&= _BOD_OFF
    cfg&= _IESO_OFF
    cfg&= _FCMEN_OFF
     __CONFIG cfg
    #ENDCONFIG
    
    DEFINE OSC 4     ; Use a 4 MHZ internal clock 
    
    ANSEL = 0   
    CMCON0 = 7 
    TRISIO = %000001  ;gpio0 input rest output
    
    '   I/O Alias definition
    '   ====================
        '
        LED        VAR GPIO.5
        Generator  var GPIO.4
        
    '   Hardware definition
    '   ===================
        '                           
        
    '   Variable definition
    '   ===================
        '
        Sensor var byte
        Looop   var byte
    
    '   EEPROM assignement
    '   ==================
        '
        data @3,3,0,2,1 ' table to convert 'Sensor' variable result to according
                        ' LED blink. See Result list bellow
        
    '   Software/hardware initialisation
    '   ================================
        '
        led = 0
        generator = 0
        
                
    Start:
        ' Short explanation of the whole thing.
        '       1. Send a high level to all sensor input => GPIO<2:0>
        '       2. Read GPIO port 
        '       3. Send a low level to all sensor input
        '       4. Keep only GPIO<2:0> bits
        '       5. Test result
        '
        ' If no sensor has been touched, the result will be 7 => 0000 0111, case
        ' else, the body capacitance will introduce sufficient delay between
        ' step 1 and 2 wich will keep the according bit to 0.  
        '
        ' Results will be as follow
        '               NoSensor => 0000 0111 => 7
        '               GPIO.0   => 0000 0110 => 6
        '               GPIO.1   => 0000 0101 => 5
        '               GPIO.2   => 0000 0011 => 3
        '
        repeat
            Generator = 1       ' enable sensor power
            Sensor = GPIO       ' read sensor
            Generator = 0       ' disable sensor power
            Sensor = Sensor & %000001 ' keep only Sensor bits HERE I CHANGE VALUE FROM 7 (0111) TO 1 (0001)
                                       
            until Sensor != %000001   ' redo the test untill one sensor is touch HERE I CHANGE VALUE FROM 7 (0111) TO 1 (0001)
        '
        ' Now we will flash an LED to confirm wich sensor has been touch
        '                      GPIO.0 => 1 Blink
        '                      GPIO.1 => 2 Blink
        '                      GPIO.2 => 3 Blink
        '
        read sensor,looop ' convert result to blink
        repeat
            LED = 1
            PAUSE 200
            lED = 0
            PAUSE 200
            looop = looop - 1
            until looop = 0
    
        goto start ' do it again...
    What I overlooked in the code? Can someone point me when I'm wrong? Thanks.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Touch switch ON/OFF

    OK, I'll answer myself. I got it working. I needed a simple ON/OFF touch switch for controlling solder fumes extractor fan.
    The code is minimalist, because PIC10F222, but working on other PIC as well. Tested on12F683, 12F1840.
    One touch switch ON output, second switch OFF output.
    Here the code:
    Code:
    ; PIC10F222
    #CONFIG  
    cfg = _IOFSCS_8MHZ
    cfg&= _MCPU_OFF                 
    cfg&= _WDT_OFF                                     
    cfg&= _MCLRE_OFF
    cfg&= _CP_OFF
     __CONFIG cfg
    #ENDCONFIG
    
    OPTION_REG.5=0
    ADCON0 = 0 
    TRISIO =%0100
    
    
    LED        VAR GPIO.0
    Generator  var GPIO.1
    
    Sensor     var byte
    
    LED = 0
    Generator = 0
        
                
    Start:
    repeat
    Generator = 1       ' enable sensor power
    Sensor = GPIO      ' read sensor
    Generator = 0       ' disable sensor power
    Sensor = Sensor & 4 ' keep only Sensor bits       
    until Sensor != 1  ' redo the test untill one sensor is touch
        
    if sensor = 0 then toggle LED : pause 500
    
    goto Start

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,379


    Did you find this post helpful? Yes | No

    Default Re: Touch switch ON/OFF

    so what was wrong with the first draft ?
    Warning I'm not a teacher

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: Touch switch ON/OFF

    Possibly stupid question but why not have one touch switch, first press turns on, next turns off ... ad nauseam.
    George

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Touch switch ON/OFF

    towlerg: that's how it works. First touch turns ON, second touch turns OFF.

    Richard: so what was wrong with the first draft ?

    basically nothing. First I didn't proper understand how the code works and wrong output handling. Then I hooked up serial converter and send out the results. After understanding the principe it's easy to modify it to my needs.
    I needed this for switching an extraction fan as I wrote before, under my stereoscopic soldering microscope only with touching with soldering iron tip, or finger. It's more comfortable then struggling with rocker (main) switch on microscope body.
    If anyone interested how it looks and works, here a short video:
    Last edited by louislouis; - 5th April 2020 at 18:09. Reason: readability

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


    Did you find this post helpful? Yes | No

    Default Re: Touch switch ON/OFF

    If you want a super easy stand alone touch switch I highly recommend these...

    https://www.amazon.com/TTP223-Capaci...194533&sr=8-27

    They can be configured either momentary or latching and either active HI or LO.
    I have used them and they work very well.
    dwight
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

Similar Threads

  1. Replies: 0
    Last Post: - 26th January 2015, 06:18
  2. Toggle switch (latching switch) state
    By craigwb in forum General
    Replies: 4
    Last Post: - 6th November 2011, 15:18
  3. Replies: 6
    Last Post: - 9th January 2011, 16:26
  4. Touch Sensitive Switch
    By BobP in forum General
    Replies: 8
    Last Post: - 23rd August 2007, 23:27
  5. touch switch dimmer
    By helmut in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 18th August 2007, 12:25

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