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

RA0 – LED
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…

10f322 adc photo

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)
{
    setup();
    
    uint8_t pot_value;  //Var for Potentiometer 
    uint8_t light_value; //Var for photoresistor values
    
    while(1){
    
        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;
        }
        else{
             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;
                break;
                
        case 1  :
                //Select the Analog channel - RA1 or AN1
                ADCONbits.CHS = 0b001;
                break;
                
        case 2  :
                //Select the Analog channel - RA2 or AN2
                ADCONbits.CHS = 0b010;
                break;
    }
    
    
    __delay_ms(10);  //Wait 10ms for ADC input to become stable 
    
    
    ADCONbits.GO_nDONE = 1;  //Start the conversion
    
    while (ADCONbits.GO_nDONE == 1){
        NOP();
    }
    
    return ADRES;
}

Have a Project or Idea!?

I am Available for Freelance Projects

My skills are always primed and ready for new opportunities to be put to work, and I am ever on the lookout to connect with individuals who share a similar mindset.

If you’re intrigued and wish to collaborate, connect, or simply indulge in a stimulating conversation, don’t hesitate! Drop me an email and let’s begin our journey. I eagerly anticipate our interaction!

jamie@jamiestarling.com


Pin It on Pinterest

Share This