# sensor design

#### coolroose

Joined Aug 31, 2010
16
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

#### ftsolutions

Joined Nov 21, 2009
48
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.

#### kavli

Joined Aug 1, 2011
23
I need help with this project, i don't know how to start
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

#### ErnieM

Joined Apr 24, 2011
8,328
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.)

Init
Loop:
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.

#### be80be

Joined Jul 5, 2008
2,069
Here some code to look at i did it some years back
Rich (BB code):
    ;
;  be80be's example ADC program for 16F684
;
;
list    p=16f684        ; list directive to define processor
#include <P16F684.inc>
errorlevel -302         ; Turn off banking message

__CONFIG _BOD_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF

CBLOCK  0x020
endc

v5.0    equ     50000/196       ; adc value for 5.0v
v4.0    equ     40000/196       ; adc value for 4.0v
v3.0    equ     30000/196       ; adc value for 3.0v
v2.5    equ     25000/196       ; adc value for 2.5v
v2.0    equ     20000/196       ; adc value for 2.0v
v1.0    equ     10000/196       ; adc value for 1.0v

org     0x000           ; reset vector
init
bsf     STATUS,RP0      ; select Register Page 1
movlw   0xFF            ;
movwf   TRISA           ; Make PortA all input
clrf    TRISC           ; Make PortC all output
movlw   0x10            ; A2D Clock Fosc/8
bcf     STATUS,RP0      ; back to Register Page 0
bsf     STATUS,RP1      ; select Register Page 2
movlw   0xFF            ; we want all Port A pins Analog
movwf   ANSEL           ;
bcf     STATUS,RP1      ; back to Register Page 0
movlw   0x01            ;
movwf   ADCON0          ; configure A2D for Channel 0
loop
call    adcdelay        ; delay to charge cap
btfss   ADCON0,GO       ; complete? yes, skip, else
goto    \$-1             ; branch (wait for complete)
;
;
skpc                    ; borrow? no, skip, else
sublw   0               ; twos complement WREG
;
;
sublw   2               ; C=0 if ∆ adc >= 2 points
skpc                    ; C=1? yes, skip, else
;
;
btfss   shadow,0        ; borrow? no, skip, else
addlw   v5.0            ; undo the subtract

btfss   shadow,0        ; borrow? no, skip, else
addlw   v4.0            ; undo the subtract

btfss   shadow,0        ; borrow? no, skip, else
addlw   v3.0            ; undo the subtract

btfss   shadow,0        ; borrow? no, skip, else
addlw   v2.5            ; undo the subtract

btfss   shadow,0        ; borrow? no, skip, else
addlw   v2.0            ; undo the subtract

movwf   PORTC           ; update LED display
goto    loop            ; loop

end