Purpose: The embedded controller's analog to digital converter can be used to measure an analog input voltage. The digital value of the input voltage can then be saved and used to direct the program output.
Objective: This workshop is designed to familiarize the student with elements of the embedded microcontroller.
Real world application: Robotics light sensor
Requirements: Hardware: Photocell (12K to 1M ohms), (3) LEDs, Flashlight, Voltmeter
Software commands: Call ad0
Procedure: Design and implement a voltage indicator. Arrange the LEDs in a column, when the input voltage at ADC0 is < 2.5 volts light the bottom LED, when the input voltage is > 2.5 but < 3.5 then light the middle LED, finally if the input voltage is > 3.5 volts then light the top LED. Wire the input to ADC0 as follows:
Use a flashlight to vary the input voltage. Check the circuit operation using a voltmeter to verify the input voltage to ADC0.
Note: Place a 330 ohm resistor in series with LED to prevent overdriving the LED.
Circuit Drawing for Workshop #5
PicBasic Code for Workshop #5
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
'A program to convert an analog voltage input to a digital
'representation (in millivolts) and, based on that voltage, light
'the corresponding high, middle or low LED
'Herb Wagner
'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
symbol mv=w4
'designate mv to hold millivolt
'input
set: call ad0
'convert analog signal at ADC0
w0 = w10 * 6 / 5
'to a millivolts
w1 = w10 / 50
w2 = w10 / 1000
mv = w0 + w1 + w2
pause 500
'1/2 sec delay time
low 5: low 6 :low 7 'set all
outputs to low
'test the voltage input
if mv < 2500 then go_4_it
'if less than 2.5V light
'low LED
if mv > 2500 and mv < 3500
then go_2_it 'if between
'2.5V and 3.5V light
'middle LED
if mv > 3500 then go_6_it 'if
greater than 3.5V light
'high LED
goto set
'default
'***********************************************************
go_4_it:
high 7
goto set
'***********************************************************
go_2_it:
high 6
goto set
'***********************************************************
go_6_it:
high 5
goto set
'***********************************************************
end