Login
Register
Robot Builder Articles
 
 


Measuring Light on a PIC 16F62x

Posted by on Thursday, March 24, 2005 (GST)

Measuring Light on a PIC 16F62x

Introduction

 

The PIC 16F62X series is a pin-compatible upgrade to the old F84 offering lots of extra internal goodies such as timers, a USART and a couple of comparators. There are no analogue to digital converters (ADC) as such but it is a simple job to use a comparator, a timer and a few lines of software to make one. A typical robot requirement is for measuring light levels either for data-logging purposes (one sensor needed) or for light-following (two sensors required).

 

Electronics

 

Only two components are needed (besides the PIC): a 0.1 m F capacitor and an ORP12 type Light Dependent Resistor (LDR). LDRs don’t react to changes in light as quickly as a phototransistor, but they give a nice range of resistance from a few ohms in intense light to hundreds of kohms in darkness. Fig 1 gives the circuit.

      

LS Fig1.gif

Operation

 

The LDR and capacitor are connected to the VIN- input of comparator 2 via port pin RA1. A comparator circuit requires a reference voltage on its other input VIN+ to set the point at which the output switches with the rising voltage on VIN-. Fortunately the PIC has a programmable VREF which can be turned on by software, and is internally connected to comparator 2 when comparator Mode 5 is selected by the program.

Our ADC works by measuring how long it takes for the capacitor to charge up to the VREF voltage and flip the comparator. The charging rate is set by the resistance value of the LDR, so the brighter the light the lower the resistance and the faster the capacitor charges. Basic circuit theory tells us that the time constant for a capacitor and resistance in series is given by:

 

T = CR seconds

where T is the time taken for the capacitor to charge to 0.63 of its final value.

 

We are using a capacitor of value C = 0.1 m F and say the LDR has a resistance of 50 k W . This gives a time constant T = 0.005 seconds. The final voltage is the supply voltage of 5 volts and 0.63 of this is about 3.2 volts. I set the VREF to 3.6 volts which means that starting with a discharged capacitor, the comparator will trigger after about 0.005 seconds. A different resistance will give a different time so now we have a measurement system where resistance value is proportional to time. I use Timer 1 in the PIC to measure this time.   With a clock speed of 1 MHz the timer would have reached about 600 in this example.

Software Listing

 

            org 0            ; Reset Vector

            goto INIT

            org 4            ; Interrupt Vector

            goto COMP

            org 5

INIT:        bank0

            movlw H'05'      ; Comp config mode 5

            movwf CMCON

            movlw H'14'      ; Timer 1 prescale by 2

            movwf T1CON      ; A 1 MHz clock is used

            clrf PORTA

            clrf PORTB

            bank1

            movlw H'CF'      ; Enable Vref = 3.6v

            movwf VRCON

            bsf INTCON,GIE   ; Enable Global Int

      bsf INTCON,PEIE  ; Unmask Peripheral Int

      .

.

CONVERT:    bank1            ; Main program loop

      bcf TRISA,1      ; Set RA1 as output

      bank0

      bcf PORTA,1      ; Discharge capacitor

      bcf PIR1,CMIF    ; Clear comp Int flag

      bank1

      bsf TRISA,1      ; Reset RA1 as input,

                       ; cap starts charging

      bsf PIE1,CMIE    ; Unmask Comp Int  

      bank0

      clrf TMR1L       ; Clear Timer 1 L

      clrf TMR1H       ; Clear Timer 1 H

      bsf T1CON,TMR1ON ; Start Timer 1

      movf CMCON,f     ; Read to sync output

      bank1

WAIT:       btfsc PIE1,CMIE  ; Wait for comp Int

                             ; ie wait until Int

      goto WAIT        ; Enable bit has been

                       ; cleared

GETDATA:                     ; Timer 1 contains

            .                ; light level value

      .

.

goto CONVERT     ; Loop for new data

;           Comparator Interrupt Service routine

COMP:       bank0

bcf T1CON,TMR1ON ; Stop Timer 1

            bcf PIR1,CMIF    ; Clear Interrupt flag

            bank1

            bcf PIE1,CMIE    ; Mask Comparator Int

            retfie

The software consists of three parts: Initialisation, main program loop and the comparator interrupt service routine. The main program loop discharges the capacitor and clears Timer 1. It then allows the capacitor to begin charging again, while Timer 1 starts counting. The comparator interrupt is enabled and a short wait loop is entered which only exits when an interrupt has been serviced. On leaving the wait loop, Timer 1 will have been stopped and contains a value proportional to the light level falling on LDR1.

 

Expansion

 

The PIC contains two comparators and so two light sensors may be monitored using both the comparators in Mode 3.

                                                                                  Bill Marshall


Average Rating:

Add Your Comment

Comments:


how can i find the intensity
By vijitron on Saturday, June 23, 2007 (GST)

<p>your explanation was good in working of ur model, but how can  i measure the intensity of light using the time period .</p>

Reply to this Comment



Search ...

Go

New Articles


electrical liquid tight flexible metallic conduit,LT fittings




A buck-mode converter for a stable output
voltage and/or current from an unstable
or variable input. Preliminary.




A brief introduction to using a H-Bidge IC and a PIC microcontroller to control the speed of a DC motor.





Most Popular Articles


A brief introduction to using a H-Bidge IC and a PIC microcontroller to control the speed of a DC motor.




A brief introduction to the use of ultrasonics in measuring distances to objects. A circuit for a 4-channel sonar system is described.




Quick Start Guide to Microcontrollers, how to choose and where to go.