Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

generating 40Khz pulses with a microcontroller

Not open for further replies.


Junior Member level 2
Aug 15, 2007
Reaction score
Trophy points
Activity points
location detection microcontroller ir base

generating 40Khz pulses with a microcontroller

Software based ambient light detection.

the sensor is controlled by a microcontroller to generate the low duty cycle pulses, you can benefit from the High and Low pulses to be able to detect any false readings due to ambient light. This is done by recording 2 different outputs of the sensor, one of them during the ON pulse (the sensor is emitting infra red light) and the other during the OFF time. and compare the results.

Output recorded during:

Software based deduction

On pluse Off time
1 0 There is definitely an Obstacle in front of the

1 1 The sensor is saturated by ambient light, thus we
can't know if there is an obstacle

0 0 There is definitely Nothing in front of the sensor,
the way is clear

0 1 This reading is un logical, there is something wrong
with the sensor.

hardware connections:

1)LED connected to PIN RC3 of micro controller;

2)IR receiver IC TSOP48 has a digital output that can be directly modulated by a micro controller. The output of the IC is on pin 1 of the IC. The output of the receiver IC is connected to pin RC2 of micro controller.

3) microcontroller generates low duty cycle pulses at pin RA2 at 40KHz.
RA2 is connected to the CONTROL input which is linked to a transmitter

the CONTROL input is connected to the source of low duty cycle pulses which could be from a 555timer but im not using a 555timer. i want to use the PIC16f690 to do that.


The transmit burst is initiated from the microcontroller. The transmit burst is essentially a function that’s called whenever the microcontroller requests for it.
The 40 kHz waveform has a period of:25us

Software Algorithm
A simple delay function will be used to toggle pin RA2 .I set the high part of the output to 5uS and the low part to 20uS.

A variable is needed to keep track of the number of delays to indicate end of transmission. The total number of delays is equal to 20 (10*2). Ten is the number of pulses sent in a burst and 2 is the number of delays per pulse. Each time a delay occurs, a variable is incremented to keep track of the number of delays. Once this variable reaches 20 the transmission is complete.
Pseudo code
Set pin RA2 high. Run first delay of 5uS.
Upon completion of delay set pin RA2 low. Run second delay for 20us.
After each delay increment a variable. Keep track of variable till it reaches 20. At this point transmission is complete.

here is my code:

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

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

void Transmit40(void)
ANSEL=0; // Set inputs to digital
TRISC=0x04; //making pin RC2 input

int t=0;
int cnt=0;

while(cnt<20) //Tx 10 IR pulses/burst
{ if (t==0)
PORTA=PORTA^0x04; //send IR, Toggles RA2
ir1=RC2; // O/P IR bit1= O/P pin of receiver IC

else if (t==1)

PORTA=PORTA^0x04; //stop IR, Toggles bit 2 of
//portA-IR control pin
// connected to RA2
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; //light up led connected to RC3 if detect object;


void main(void)



i am a beginner. need some assistance please for my project.

i am generating low duty cycle pulses from the microcontroller to the transmitting infrared LED.

i want my project "infra red proximity detector" to be immune to ambient light.


when receiver detects an object then the LED must light up.

when it does not detect an object, the LED must not light up.

ambient light such as a desk lamp can falsely trigger the receiver.
when i shine light onto the receiver i want the LED to remain off.

when i programed the chip and put it into the circuit. The LED pulses but i dont want this.

could some1 please assist me.

40khz pulsed infrared sensor

can you rephrase in few lines what do you want?
just to generate a 40 khz signal
pls be brief

Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to