Select Page

Using the ADC module in the PIC10F322 – we are going to use a photoresistor (see previous post on Using A Photoresistor) and a Potentiometer (another post here about it) – that will turn on and off a LED based on the amount of light.

The trigger point is adjustable using the Potentiometer. Of course you could instead of an LED – use it to trigger a relay or something else.

The Setup

RA1 – Potentiometer
RA2 – Photoresistor – voltage increases with the amount of light

High level…

The code goes though the setup RA0, output, RA1 analog input, RA2, analog input.
The loop – reads the value from the Potentiometer and photoresistor
It then compares the two – if the value of the photoresistor is greater than the Potentiometer – the LED will turn on. Otherwise it is off.

Digging in….

The only thing that has really changed in this code from previous – is the ADC_read funcation. I have updated it to take a value of the ADC channel.

This will allows for one funcation, to read multiple channels.

Future ideas…

This is just a simple – quick and easy project. There is an issue, the light could bounce where you get a on/off on/off. Etc.. Quick way to resolve this is to put in some delay function, or use a debounce code routine for the photoresistor.

The Circuit…

The Code

 * File:   adc_photo.c
 * Author: Jamie
 * Created on 9/14/2021, 8:04 PM

#include <xc.h>
#include <stdint.h>

//Device Configuration
#pragma config FOSC = INTOSC  // Oscillator Selection 
#pragma config BOREN = ON    // Brown-out Reset
#pragma config WDTE = OFF    // Watchdog Timer
#pragma config PWRTE = ON    // Power-up Timer
#pragma config MCLRE = OFF   // MCLR Pin Function Select bit->MCLR pin function is digital input, MCLR internally tied to VDD
#pragma config CP = OFF      // Code Protection 
#pragma config LVP = OFF     // Low-Voltage Programming 
#pragma config LPBOR = ON    // Brown-out Reset Selection bits
#pragma config BORV = LO    // Brown-out Reset Voltage Selection
#pragma config WRT = OFF    // Flash Memory Self-Write Protection

//Used to calculate the delay time - Change depending on processor Speed
#define _XTAL_FREQ 8000000  //8 MHz (default after Reset)

//Function prototypes
void setup (void);
uint8_t ADC_Read(uint8_t channel);

void main(void)
    uint8_t pot_value;  //Var for Potentiometer 
    uint8_t light_value; //Var for photoresistor values
        pot_value = ADC_Read(1);
        light_value = ADC_Read(2);
        //Check values - turn on LED if light is over the set value - otherwise off
        if (light_value > pot_value){
            LATAbits.LATA0 = 1;
             LATAbits.LATA0 = 0;

void setup(){
    //Pin LED is connected to
    TRISAbits.TRISA0 = 0;  //Make pin Output RA0
    ANSELAbits.ANSA0 =0;   //Disable Analog RA0
    LATAbits.LATA0 = 0;     //Make the output Low RA0
    //Setup for pin that the POT is connected to
    TRISAbits.TRISA1 = 1;  //Make pin Input RA1
    ANSELAbits.ANSA1 =1;   //Enable Analog RA1
     //Setup for pin that the Photoresistor is connected to
    TRISAbits.TRISA2 = 1;  //Make pin Input RA2
    ANSELAbits.ANSA2 =1;   //Enable Analog RA2
    //Set Analog conversion clock FOSC/32, since we are running 8Mhz, we need to have a conversion time at or greater 1uS
    //FOSC/32 will give us 4uS
    ADCONbits.ADCS = 0b010; 
    //Select the Analog channel - RA1 or AN1
    ADCONbits.CHS = 0b001;
    //Turn on the ADC module
    ADCONbits.ADON = 1;     

uint8_t ADC_Read(uint8_t channel){
    //Uses the channel input to select the ADC channel - valid input is 0,1,2
    //Starts the ADC read and waits until a conversion is complete before returning
    //Returns the ADC value
    switch (channel){
        case 0  :
                //Select the Analog channel - RA0 or AN0
                ADCONbits.CHS = 0b000;
        case 1  :
                //Select the Analog channel - RA1 or AN1
                ADCONbits.CHS = 0b001;
        case 2  :
                //Select the Analog channel - RA2 or AN2
                ADCONbits.CHS = 0b010;
    __delay_ms(10);  //Wait 10ms for ADC input to become stable 
    ADCONbits.GO_nDONE = 1;  //Start the conversion
    while (ADCONbits.GO_nDONE == 1){
    return ADRES;

Have a technology project in mind? Let’s talk!

I am currently available for hire

Need a custom technology solution for your business? Please send me an email ( to discuss your project needs.

I can help you get started on the road of building a simple and well-tested working solution, together, with your team.