Total Pageviews

VAMSI DANDA

Follow by Email

Popular Posts

Electronics hobbyists enjoy playing with the projects.some people start a garage at home. For making any new invention you don't need any big laboratory. you can make your home a LAB.to work you just need a "multimeter". I have been working on some projects.At the beginning stage i made so many disasters in my room.I blasted so many components(one day i burnt my bed).what ever we do we enjoy that.So i just wanted to share some of my work with you.

Place Order

Thursday, 2 August 2012

HC-SR04 ultrasonic sensor interfacing with 8051 microcontroller

If you want to make your robot sense the objects in it's surroundings i suggest you to go for an ULTRASONIC sensor.Though the IR based sensors are cheep, their working range may vary due change in ambient light and won't give accurate range values.

In case of ULTRASONIC sensors they work based on the principle of RADAR(RAdio Detection And Ranging). A RADAR transmits electromagnetic "pulse" towards the target and receives the "echo" reflected by the target.

Then the range of the target is determined by the "time lagging" between transmitted pulse and the received "echo". Generally microwave and ultrasonic frequencies are used in RADARS

Our HC-SR04 ultrasonic sensor works similar to the RADAR mechanism but in a simplified manner. This sensor consists of four PINS

1.Vcc------------------connect to 5V dc

2.Trigger--------------pulse input that triggers the sensor

3.Echo----------------indicates the reception of echo from the target

4.Gnd-----------------ground final

working with sensor step by step

Take a look at the timing diagram of HC-SR04 sensor

This diagram describes the basic algorithm in distance measuring using the sensor.

STEP1.Make
"Trig" pin of the sensor high for 10µs. This initiates a sensor cycle.

STEP2.8 x 40 kHz pulses will be sent from
the transmitting piezzo transducer of the sensor, after which
time the "Echo" pin on the sensor will go from low to high.

STEP3.The
40 kHz sound wave will bounce off the nearest object and return to the sensor.

STEP5. The
distance between the sensor and the detected object can be calculated based on
the length of time the Echo pin is high.

STEP6.If
no object is detected, the Echo pin will stay high for 38ms and then go low.

configuring the 8051 microcontroller:

To inter face the sensor to AT89S51 microcontroller we need two I/O pins. One of them is external interrupt pin(INT0 or INT1) for measuring the pulse width at the echo pin. and any other pin say P3.5 for trigger.

STEP1.Connect the trigger pin of
sensor to P3.5 of AT89S51

STEP2.Connect the echo pin of the
sensor to INT0 (P3.2) of AT89S51

STEP3.Configure the TIMER0 of
8051 in 16 bit mode with “GATE” bit enabled. If the GATE pin is enabled and
timer run control bit TR0 is set, the TIMER0 will be controlled by INT0 pin.
When INT0 is high then the TIMER0 starts counting. Whenever INT0 goes low TIMER0
holds its count. So load the TMOD register with 00001010==0x0A; (better refer to
any book of 8051).

STEP4.As the INT0 pin is input don’t
forget to declare the pin input. Write “1” to the pin to make it input.

algorithm

==>Send a 10 micro second high
pulse at trigger

Initially P3.5=0;

P3.5=1;

Delay(10 micro second);

P3.5=0;

==> "WAIT" until the sensor transmits the eight 40KHz pulses and signal reflection. initially the "ECHO"pin is low when the transmitter completes the pulse the pin goes high then our TIMER0 starts counting.when input at INT0 goes low timer holds count. logic for waiting: while(INT0==0); while(INT0==1); but some times due to errors in the sensor functioning the 8051 microcontroller may go into an "INFINITE LOOP" for that there are two remedies1)use watch dog timer(present on AT89S52 and other advanced versions)2)generate a delay of 40 milliseconds after triggering the ultrasonic sensor.the second option is preferred for beginners. ==>TIMER0 value = time taken by the signal to (go forward+come back)It meas the signal traces the whole distance twice. so time taken by the signal to travel the distance = TIMER0 value/2 ULTRASONIC pulse travels with the speed of sound 340.29 m/s= 34029 cm/s range of target= velocity *time ==> 34029 * TIMER0/2 ==> 17015 * TIMER0At 12MHz TIMER0 gets incremented for 1microsecond. RANGE = 17015 centimeters/seconds * TIMER0 micro seconds

unsigned char get_range(void) { unsigned char range; send_pulse(); while(!INT0);// in sake of these lines you can generate a delay of 40 Milli seconds=40000 micro while (INT0);// seconds DPH=TH0;DPL=TL0; TH0=0xFF;TL0=0xFF; if(DPTR<35000)//actually you need to use 38000 but the sensor may not work at higher levels range=DPTR/59; else range=0; // indicates that there is no obstacle in front of the sensor return range; }void main(){//main begin TMOD=0x09;//timer0 in 16 bit mode with gate enable TR0=1;//timer0 run enabled TH0=0x00;TL0=0x00; P3|=0x04;//setting pin P3.2 to make it INPUT

unsigned int target_range=0; while(1) { target_range=get_range(); //make your own function to display the range value.better to use LCD }}// end of main

it's working but the output is delayed... here are the problems that i'm facing:1) Consistent change in distance.(If object is moved slowly and kept steady)2) Delayed Reaction.3) Hangs automatically. -Could be due to: i) Sudden or Fast changes. ii) Moving closer to the module,i.e. reducing the distance between module and object.

if you are using the same sensor HC-SR 04some times the sensor may not get back to low(logic zero) after 38 Milliseconds. where your INTO pin will struck in while(!INTO); so it will struck.............delayed response means....how your are displaying the rangeon LCD or 7 segment displaythe delay may be caused by the functions of LCD...

Hello im working on ultrasonic sensor for obstacle detection, this means i dont need timer since i dont need to measure distance.

The problem is after the transmission of 8 pulses of 40khz the echo pin becomes low and my led is on (obstacle detected) even there is no obstacle around the sensor. I've generated a loop which checks for echo pin for 38ms then exits. This means led should be off if no obstacle detected, but its on

HC-SR 04 is a distance measuring sensorfor obstacle detection you must use an other onewith three terminals which will give the direct logic voltage 0 or 1. it will be a three terminal device.if you want to use the hc sr 04 with atmega32 you need to use input capture technique.

you need to use an other ultrasonic sensor for obstacle sensing. it will be a three terminal device which will give logic values according to the object presence (like zero and one)the HC sr 04 will give you high when it detects an object and it stays high for some time. by calculating that time we determine the range.

Yes, i'm using the same sensor (HC-SR04).The problem of the delay is not there anymore, i made changes in the LCD interfacing.But it still gives wrong readings.It shows:-1cm,when it is actually 13cm-2cm,when it is actually 21cm-3cm,when it is actually 33cm-4cm,when it is actually 45cm and so on.(I measured using a measuring tape)

your program works fine but tell me how can i hook up the range detected by the program by timer and use this value to generate a pwm signal to run a dc motor of 6 volt but i want to operate it in a manner such as the maximum range gives no or low rpm and as distance reduces from the obstacle the rpm get increased can you please give a program for such problem

some times the sensor echo pin may not return back to zero levelso even after 38ms it means if your timer gives more than thatyour sensor is not workingso i calibrated my sensor it gave me around 35000 microseconds width at "no object"so to omit that condition and get perfect range i used that condition

hey the code is working fine in proteus simulation but in the hardware its is not displaying anything on lcdi even pulled up P0 by 10k resistorsany guidance plzor should i change compiler how do u confirm that HEX transferred to 8051 is correct

Thanks for the code! I was implemented distance measure application by using Infrared sensor. But here i want to sample the IR signal, so that we can measure the distance very accuratly. So my question is Home many samples i have to take to sample the ir signal

Hello Sir,nice information abt line follower robot.We r working under same project in our college we have used Atmega8L microcontroller, L293D motor controller(we purchase robot online),WinAvr(4 ver),flash boot software for interface on computer system & connected them via cable.But we r facing one problem is that we are unable to reset it to normal mode the robot continuously following same program that loaded last time .How to solve this problem?Please help us sir .......

so your using boot flash to program your device there r two modes of operations (program mode and execution mode) so you have to find the boot reset pin if you connect the pin to ground and push the reset button then your device will be recognized by the PCthen try program it.after loading programdisable boot reset pin (disconnect from ground)and push the reset buttonthen your code will start execution...better mail me at vaabrobotics@gmail.comfor further clarifications

hi karungenerating exact time delay on 8051 at 12mhz will be lil difficult so i used that whileas the CPU runs at(1/12 of clock called the machine cycle = 1us) and delay will give you some random garbage range value if your sensor is not working as you are NOT checking for echo pin

actually im using the data-pointer(16 bit) register the DPTR to calculate the timeif you check the "regx51.h" you wont find DPTR as single( but DPL,DPH are avilable)so i used the "sfr16" macro to make a DPTR rigister...(just a carzy try)hey actually it worked...

there are 2 types of 3 pin sensor1)ping sensor =both trigger and echo are interconnected2)digital sensor=this will only sense the presence of an object like digital out put 0 or 1you can use it as normal i/o sensor you cant get range...

thanks man for identifying that mistake i made the changes check once again.no need of P1=0xff; but P3=0x04; is important when you are using a pin of 8051 as input the last bit written to this pin must be '1' as P3^2 is INT0 the echo pin .

in this statement you have to write '==' instead of '=''=' is assignment operator'==' is a relational operatorP1_3=0; will store '0' in P1_3usage of this inside "if" has no effect..if(P1_3==0) is wright statement

sensor gives active high on obstacle detection. that to be not more than 38 milliseconds.so check of 1 on that pin instead of 0. and in side the if condition{ led on; delay of 1second ; if delay not used you cant see if the LED is on led off;}

if(P3_2==0){P1_0=0; delay(100);}else{P1_0=1; delay(100);}}}//////the echo pin seems receiving the signal from the transmitter...but the led just keep on lighting up...is it because i put more than one "while(1)" loop?

P1=0x00; //make p1 as outputP2=0X00;P0=0x00;P3=0xff;TMOD=0x19; // set timer 0 and timer 1 in mode 1 and set the gate for timer 0

transmit(); //function for transmit the ul.so.receive(); //function for receiveng the ul.so.d1=range/100; //calculation for the display the digit 1 on first 7 segmenta=range%100;d2=a/10; //calculation for the display the digit 2 on second 7 segmentd3=a%10; //calculation for the display the digit 3 on third 7 segmentfor(j=0;j<100;j++){display1(d1); //display the digit 1 on first 7 segmentmsdelay(1);display2(d2); //display the digit 2 on second 7 segmentmsdelay(1);display3(d3); //calculation for the display the digit 3 on third 7 segmentmsdelay(1);

}}

void transmit(void) //function for transmit the ul.so.{TH1=0xff; //load TH0 with ff and load TL0 with f5 for 10 micro second delayTL1=0xf5;trig=1; //make high the trig pinTR1=1; //run the clockwhile(TF1==1) // wait for the 10 micro second trig=0; //make triger low after 10 micro secondTR1=0;TF1=0;}

unsigned int receive(){TH0=0x00;TL0=0x00;TR0=1; while(m1==0); //wait till the ul.so. complete the transmit the 8 pulses and get high and high m1 will turn on the timerwhile(m1==1); //wait till the reflection receive is completed and low m1 will turn on the timerDPH=TH0; //take timer value for distance calculationDPL=TL0;if(DPTR<35000) //if reflection is not received in 35000 micro so there is no obstacle within 400 cm{range=DPTR/59; //take the value of distanceTR0=0;TF0=0;}else{range=0; //no obstacle}return(range);}

Its working fine. But when It goes above range it shows full 7segmnt(8.8.8.)And than I have to push RESET for new measurement. Even I add default case, its not working.So what correction I needed?and how to use LCD 16x2 instead of 7segment.

i am getting some problem ....just i want to take action upon particular distance from robo through pin 2.4and 2.5......................................this code gives distance accurately but not gives any responce for pin 2.4and 2.5.....pls sir help me as soon as possible ....i will asways greatfull to u for this ...thanks