Proton BASIC Compiler - A short how to... Led as a Sensor and emiter..


  • Pic® Basic


  • A short how to... Led as a Sensor and emiter.. by Seconika

    I have seen several pages online about LED as sensor.
    Here is a link to the page where I got the information from.
    http://www.thebox.myzen.co.uk/Workshop/LED_Sensing.html

    This page give's a really good explanation on how it all works.

    I never found examples with PIC and PICBASIC/PROTON BASIC...

    This is how I did it..
    There are most certain many ways that are correct and better than this..
    But I hope that this article can help me make a better solution.


    Code:
    ' _______________________________________________________________________
    '                                                                        '
    ' This shows a way to use an ordinary LED as both LED light              '
    ' and sensor at the same time.                                           '
    '                                                                        '
    ' You can use this to make an LED in to a "Light with touch sensor"      '
    '                                                                        '
    ' This example is reworked from a site with code for ATMEL mcu.          '
    ' http://www.thebox.myzen.co.uk/Workshop/LED_Sensing.html                '
    '                                                                        '
    ' Feel free to use it if you like it.                                    '
    ' If you have a better way of doing this, please let me know.            '
    ' (You find me @ the Crownhill forum, Username: seconika                 '
    '                                                                        '
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤  HARDWARE ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤               '
    '                                                                        '
    ' Wire up the LED so that the anode is connected to a digital pin        '
    ' and the cathode is connected to a an analogue input pin.               '
    ' Don’t forget a current limiting resistor in each LED.                  '
    '                                                                        '
    ' So, that’s simple enough.                                              '
    '                                                                        '
    ' PORTB.7 ------>|-------|||||------- PORTA.0                            '
    '              LED     RESISTOR                                          '
    '                                                                        '
    ' _______________________________________________________________________'
    
    
    Device = 18F452     ' Select device
    Xtal = 20           ' Set speed
    
        PortB_Pullups = Off     ' Disable Pullup on portB
    
        ' Set Serial output.
        Hserial_Baud = 38400            ' Set baud rate to 9600
        Hserial_RCSTA = %10010000       ' Enable serial port and continuous receive
        Hserial_TXSTA = %00100100       ' Enable transmit and asynchronous mode
        Hserial_Clear = On                ' Enable Error clearing on received characters
        
        'Set ADC
           Adin_Res    10                    ' Set the resolution to 10
        Adin_Tad    FRC                    ' Choose the RC osc for ADC samples
           Adin_Stime    80                    ' Allow 80us for charge time
           
        DelayMS 100                        ' Wait for PICmicro to stabilise
        TRISA.0 = 1                        ' Setup bit-0 of PortA as an input
           ADCON1 = %10000010                ' Set PORTA analog and right justify result           
    
    
    Dim Raw1 As Byte           ' get some variables to save data in.
    Dim Raw2 As Byte           ' get some variables to save data in.
    Dim LightVal As Byte    ' get some variables to save data in.
    Dim tByte    As Byte
    
    DelayMS 100
    
    While 1 = 1                 ' Create an endless loop
    '1)   Turn LED on.    
        Low PORTA.0             ' Set PORTA.0 To logic zero.
        High PORTB.7            ' Set PORTB.7 To logic one.
        DelayMS 10              ' Now the Led can shine for a while :)
        
    '2)   Set LED in measurement mode (Turns the LED off)
        TRISA.0 = 1             ' Set PORTA.0 To ADC input
        Low PORTB.7             ' Set PORTB.7 to logic zero
        
    '3-4) Take a measurement of the analogue voltage, this is a reference level for later use.
        Raw1 = ADIn 0            ' Read the ADC
    
    '5)   Wait while the photon current is integrated
    '     the longer the more sensitive but too long 
    '     and effects other than photo current dominate. 
        DelayMS 8               
    
    '6)    Measure the voltage again and subtract it from 
    '      the reference value you took before the integration time. 
    '      This value is your light reading
        Raw2 = ADIn 0            ' Read the ADC 
    
        TRISA.0 = 0             'Low PORTA.0    'Turn LED on again
        High PORTB.7   '
        
        LightVal = Raw2 - Raw1
    
        If LightVal > 115 Then
           Toggle PORTB.0
           tByte = tByte + 1
        End If
        
        If tByte > 255 Then
           tByte = 0
           Toggle PORTB.0  
        End If    
        
    Wend        'Make it all over again :)
    I'm in the work of fine tuning the code for a project of mine, where I'm going to use this function as push buttons on my homemade preamp.

    I have implemented some PWM to the code, to make the buttons dimmed to different brightness for different functions/states.

    Its also very easy to have the LED turned on or off but still work as a sensor.
    Im trying to have them glowing with a slow pulsing effect to make it look more "Cool"

    I have made some meters with code using LED as sensors.
    I will have some more examples where I have the led's glowing/puling , code where the led is normally off and turns on when pushed, and also the other way around.

    I now have some led's in an array by 12 x 12 for use as an "numpad" and it works nice.
    I just need to make it less sensitive as it is now, all I need to do is move a finger some cm. above the key

    Comments, suggestions, and additions are welcome.

    /Niklas

    Admin - please post comments here in the main forum
  • Recent Activity

    rcurl-44

    PICKitPlus Forum

    Thread Starter: evan

    PICkitPlus PICkitPlus is a total replacement for the existing Microchip PICkit2™ and PICkit3™ Windows GUI software. The PICkitPlus software ...

    rcurl Yesterday, 20:28 Go to last post
    John Drew-26

    Request for Assistance - PIC16F15323

    Thread Starter: gdesantis

    After having successfully programmed various enhanced PIC16 MCUs, I plan to use a PIC16F15323 in a new application. As always, I first start with a...

    John Drew Yesterday, 23:03 Go to last post