• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

software based AMBIENT light detection

Not open for further replies.
/*generating 40KHz low duty cycle pulses which will drive an Infrared transmitter. When an object is
infront of the infrared transmitter then the receiver IC will detect the object and an LED will GLOW.

i want the project to be immune to ambient light such as a desk lamp.
1)when i shine light on the transmitter i want the led to be off.
2)when there is no object infront of the transmitter i want the led to be off.
3)when there is an object infront of the transmitter i want the led to glow.

i programmed the pic16f690 and put it in my circuit. this is what happens:
1)LED pulses continuously when no object in front of it.
2)when object infront of transmitter it stops glowing
3)when shine light on transmitter the LED pulses very fast.

hardware connections:
LED connected to RC3
output of receiver connected to RC2
micro generates pulses at RA2

here is my code:

#include <pic.h>
#include "pic.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#include "delay.h"

void Transmit40(void);

bit ir1,ir2;   // 2 different outputs of sensor during On & Off pulse
unsigned char ir;  // stores final result

void Transmit40(void)

	int t=0;
	int cnt=0; 

	while(cnt<20)	 //Tx 10 IR pulses/burst
		{	if (t==0)			    
				PORTA=PORTA^0x04; //send IR, Toggles RA2.  RA2 connected to control input in attached circuit diagram
				ir1=RC2;	/ IR bit1= RC2
			else if (t==1)		    	
				PORTA=PORTA^0x04;  //stop IR, Toggles RA2, RA2 is source of low duty cycle pulses
				ir2=RC2;  // O/P IR bit2= O/P pin of receiver IC

			if ((ir1 == 1)&(ir2 == 0))  //Obstacle detected
				ir = 1;

				ir = 0;// The way is clear in front of the sensor

			RC3=ir;// led connected to RC3, LED indicates detection  


void main(void)
ANSEL=0; // Set inputs to digital
TRISC=0x04;//making pin RC2 input.  output of detector IC connected to RC2 
while (1)

the hardware part of my circuit works. i initially used a 555timer to generate pulses and the hardware worked.
all im doin now is using a microcontroller to generate pulses instead of a 555timer. so the problem lies in the software.

please do assist me to correct my code. it is for a project Im a beginner with the pic*/


Last edited:


New Member
Hmm, does this forum have "CODE" tags? If so, wrapping your code in them makes it much easier to read through.
Yes, apparently there are code tags.
Last edited:
Not open for further replies.

Latest threads

EE World Online Articles