Home > Electronic Tutorials > Microcontroller Tutorials > PIC 16F877A Project Tutorial > Analog Input (Photoresistor, Sonar sensor, IR sensor, …)

PIC16F877A Microcontroller Project Tutorial

Analog Input
(Photoresistor, Sonar sensor, IR sensor, …)

Materials & Circuit

Depends on the device. For example, a sonar sensor might have 3 wires: power, ground & signal. You would connect the signal wire to one of pins AN0-AN7. See the device’s datasheet for help.


//the rest of the code is identical to code for photoresistor input, except here we call it ‘sonar’
int16 sonar = 0;
set_adc_channel(1);//set the pic to read from AN1


  • You can use the photoresistor tutorial code to read any sort of analog input device, as long as the device is designed so you can run a signal wire to the input pin(s).
  • Remember, only the AN0-AN7 pins allow analog input, and you have to call setup_adc functions & set_adc_channel as in photo resistor tutorial.
  • Roughly, PIC will read 0 volts as 0, and 5 volts as 255.
Note: To report broken links or to submit your projects please send email to Webmaster