sensor design

Discussion in 'Embedded Systems and Microcontrollers' started by coolroose, Sep 1, 2011.

  1. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    I need help with this project, i don't know how to start

    Sensor Design and Analog Digital Conversion.

    Objective
    • Learn how to design sensors with embedded microcontrollers
    • Understand the operation of ADC
    • Understand the design of sensor circuitry
    Description
    You are required to design a light intensity sensor controlled by a PIC microcontroller. The parts given include a PIC16F684, a photoresistor,
    a LED and assorted diodes and resistors.
    You can use a breadboard or a wire-*‐wrapping prototype board (recommended for a rigid design). The ADC module in PIC16F684 will be used to convert analog signal to digital values. Your PIC based light sensor
    should be able to 1. Convert the light density information to a variable voltage (0-*‐5V). 2. Obtain an analog input signal (sensory data) via an analog pin of the PIC (e.g. AN0) 3. Use the internal ADC of PIC to convert analog signals to digital values 4. Turn on the LED if the sensor is encapsulated in a dark box or put in a dark environment. 5. Turn off the LED if the sensor is placed in a well lighted environment. 6. (Optionally)
    display the ADC result to a LCD module
     
  2. ftsolutions

    Active Member

    Nov 21, 2009
    48
    2
    It seems to spell out the circuit needs and programming requirements pretty explicitly.
    First, get the data sheet for the photoresistor and determine its voltage and current characteristics/requirements, assuming that you will simply power everything from the same +5V DC supply for running the PIC processor. This will allow you to determine what, if any series resistor(s) you may need to tie the photoresistor to ground or 5V and to connect to the analog input pin on the PIC. Check the operation of the circuit and make sure it meets the requirements of the analog input on the PIC, then proceed with the PIC wiring and programming.
     
  3. kavli

    New Member

    Aug 1, 2011
    23
    2
    Tell us a little bit more about what you already know. For instance, do you know how to read and interpret the information in the PIC's datasheet? Option registers, etc?

    Do you know how to connect the components electrically?

    What do you feel is most difficult: The programming part or the electrical part?

    Do you know flowcharting? Can you make a flow diagram of the function of the program?

    -- K
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Given: The parts given include a PIC16F684, a photoresistor,
    a LED and assorted diodes and resistors.

    0. Since the PIC16F684 runs off 5V, assume you have 5V to run it off. Eventually you'll need to really get 5V, either from a bench supply or other source.


    1. Convert the light density information to a variable voltage (0-*‐5V).

    That's what the photoresistor and the fixed resistors are for. Got an ohmmeter? Put it on the photoresistor and see how it responds to light. Now rummage thru your box of fixed resistors and pick one to put in series with the photoresistor. Your intent here is to make a voltage divider from the 5V supply that gives an output from near 0V to near 5V when you change the light on the photoresistor from dark to light.


    2. Obtain an analog input signal (sensory data) via an analog pin of the PIC (e.g. AN0)

    That means just connect the resistor divider from (1.) to a suitable AN pin, I would expect AN0 to be a good choice.


    3. Use the internal ADC of PIC to convert analog signals to digital values

    Bang open the PIC spec sheet, get confused, come back and ask some specific questions. It's not impossibly hard, but there is some subtleties we all got wrong our first few times.


    4. Turn on the LED if the sensor is encapsulated in a dark box or put in a dark environment.
    5. Turn off the LED if the sensor is placed in a well lighted environment.


    Pick another free PIC pin, set it to be a digital output. Note if the pin also has an analog function it is analog by default at power up, and you need to turn that off as part of your initialization.

    The was the photoresistor divider was set up above gives you 0 to 5V of output, and the A2D will have an output of 0 to 255 (8 bit mode) or 0 to 1023 (10 bit mode). 8 bits is plenty for this app.

    Read the voltage on your divider that is the change from light to dark (like your hand over the photoresistor or such), and read the voltage (call it Vx). The A2D should be reading 255 * Vx / 5V at that point. (Or just skip all that and guess it's 127.)

    Your code will be:

    Init
    Loop:
    Read A2D
    if (A2D > 127) turn LED off
    if (A2D <= 127) turn LED on
    goto Loop


    6. (Optionally) display the ADC result to a LCD module[/QUOTE]

    Just get the LED working first. If you are thinking of doing this later, leave RC0 to RC3 free of anything (you will need them for the LCD driver).

    Go out and download MPLAB from Microchip if you don't have it yet. The HI-TECH C compiler you get with that should be able to handle this device.
     
    coolroose likes this.
  5. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Here some code to look at i did it some years back
    Code ( (Unknown Language)):
    1.  
    2.     ;
    3.     ;  be80be's example ADC program for 16F684
    4.     ;
    5.     ;
    6.             list    p=16f684        ; list directive to define processor
    7.             #include <P16F684.inc>
    8.             errorlevel -302         ; Turn off banking message
    9.             radix   dec
    10.  
    11.             __CONFIG _BOD_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF
    12.  
    13.  
    14.             CBLOCK  0x020
    15.     shadow
    16.     newadc
    17.     oldadc
    18.             endc
    19.  
    20.     v5.0    equ     50000/196       ; adc value for 5.0v
    21.     v4.0    equ     40000/196       ; adc value for 4.0v
    22.     v3.0    equ     30000/196       ; adc value for 3.0v
    23.     v2.5    equ     25000/196       ; adc value for 2.5v
    24.     v2.0    equ     20000/196       ; adc value for 2.0v
    25.     v1.0    equ     10000/196       ; adc value for 1.0v
    26.  
    27.             org     0x000           ; reset vector
    28.     init
    29.             bsf     STATUS,RP0      ; select Register Page 1
    30.             movlw   0xFF            ;
    31.             movwf   TRISA           ; Make PortA all input
    32.             clrf    TRISC           ; Make PortC all output
    33.             movlw   0x10            ; A2D Clock Fosc/8
    34.             movwf   ADCON1          ;
    35.             bcf     STATUS,RP0      ; back to Register Page 0
    36.             bsf     STATUS,RP1      ; select Register Page 2
    37.             movlw   0xFF            ; we want all Port A pins Analog
    38.             movwf   ANSEL           ;
    39.             bcf     STATUS,RP1      ; back to Register Page 0
    40.             clrf    ADCON0          ;
    41.             movlw   0x01            ;
    42.             movwf   ADCON0          ; configure A2D for Channel 0
    43.     loop
    44.             call    adcdelay        ; delay to charge cap
    45.             bsf     ADCON0,GO       ; start adc conversion
    46.             btfss   ADCON0,GO       ; complete? yes, skip, else
    47.             goto    $-1             ; branch (wait for complete)
    48.             movf    ADRESH,W        ; new adc reading, 0..255
    49.     ;
    50.     ;  get absolute "delta" between 'new' and 'old' ADC readings
    51.     ;
    52.             movwf   newadc          ; W = new reading, 0..255
    53.             subwf   oldadc,W        ; subtract from 'oldadc'
    54.             skpc                    ; borrow? no, skip, else
    55.             sublw   0               ; twos complement WREG
    56.     ;
    57.     ;  use 'newadc' if ∆ adc >= 2 points, else use 'oldadc'
    58.     ;
    59.             sublw   2               ; C=0 if ∆ adc >= 2 points
    60.             movf    oldadc,W        ; use 'oldadc' if C=1
    61.             skpc                    ; C=1? yes, skip, else
    62.             movf    newadc,W        ; use 'newadc' if C=0
    63.             movwf   oldadc          ; update 'oldadc' var
    64.     ;
    65.     ;  adc 'window' compare code
    66.     ;
    67.             clrf    shadow          ; clear LED shadow reg'
    68.             addlw   -v5.0           ; C = adcval >= 255 (5.0v)
    69.             rlf     shadow,F        ; move C into 'shadow'
    70.             btfss   shadow,0        ; borrow? no, skip, else
    71.             addlw   v5.0            ; undo the subtract
    72.  
    73.             addlw   -v4.0           ; C = adcval >= 204 (4.0v)
    74.             rlf     shadow,F        ; move C into 'shadow'
    75.             btfss   shadow,0        ; borrow? no, skip, else
    76.             addlw   v4.0            ; undo the subtract
    77.  
    78.             addlw   -v3.0           ; C = adcval >= 153 (3.0v)
    79.             rlf     shadow,F        ; move C into 'shadow'
    80.             btfss   shadow,0        ; borrow? no, skip, else
    81.             addlw   v3.0            ; undo the subtract
    82.  
    83.             addlw   -v2.5           ; C = adcval >= 127 (2.5v)
    84.             rlf     shadow,F        ; move C into 'shadow'
    85.             btfss   shadow,0        ; borrow? no, skip, else
    86.             addlw   v2.5            ; undo the subtract
    87.  
    88.             addlw   -v2.0           ; C = adcval >= 102 (2.0v)
    89.             rlf     shadow,F        ; move C into 'shadow'
    90.             btfss   shadow,0        ; borrow? no, skip, else
    91.             addlw   v2.0            ; undo the subtract
    92.  
    93.             addlw   -v1.0           ; C = adcval >= 51  (1.0v)
    94.             rlf     shadow,W        ;
    95.             movwf   PORTC           ; update LED display
    96.             goto    loop            ; loop
    97.  
    98.     adcdelay
    99.             nop                     ; 1 cycle
    100.             return                  ; 5 cycles (including call)
    101.  
    102.             end
    103.  
     
    coolroose likes this.
  6. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    Many Thanks to Be80Be for helping me accomplished the project I posted last week
     
Loading...