Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com 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.

hcsr04 interfaced to 8051 microcontroller

Status
Not open for further replies.

Disha Karnataki

Full Member level 5
Joined
Jul 20, 2013
Messages
249
Helped
9
Reputation
18
Reaction score
8
Trophy points
18
Location
india
Activity points
2,231
i am using hcsr04 ultrasonic sensor as here : **broken link removed**
interfaced to 8051.
i have connected TRIG PIN TO " P1.7"
ECHO PIN TO " P0.1 "
the timing diagram of the sensor is as here :
https://www.ezdenki.com/graphics/hc-sr04-timing-chart.png
with no pulse given to the sensor ECHO pin must be low.
But i found that ECHO pin is high though i have not triggered the sensor.
i have configured port 1 as output port and port 0 as input port.
What could be the problem?
 

Hi,

No GND connection?

Klaus
 

vcc,gnd connected to 5V supply from microcontroller.
And trig,echo connected to pin p1.7 and pin p0.1 respectively..
 

Hi,

Is Echo also high when disconnected from microcontroller?

Klaus
 

thanks for reply
i have already got it tested working fine and measuring distance properly as expected.
Now i am interfacing two ultrasound sensors it is actually for a robot following human project where in the two sensors will detect to move right or left depending on position of the human.
So, ultrasound echo pin goes low when it detects an object and this low signal can be used to detect presence of a person.
i have written the code but, the problem i am facing is when there is no object in front of sensors then too.. it goes low. No matter that sensor o/p has to go low after 38ms(as given in datasheet) if no object is detected but, the sensor echo pins are going low before 38ms (meaning it is detecting some obstruction). I am unable to find from where it is sensing this when no object at front..
 

Hi,

Maybe the sensor needs a minimum distance to the floor, or is is influenced by the other sensor.
Datasheet and application notes should give this information.

Klaus
 

hello,

.. has to go low after 38ms

38 ms => 6400 mm max distance (in theory)
I tested yhis device and get ,in pratice, maxima 3900mm
so, in your software, did you put a maxima for distance measurment less than 38mS
If no object detected, what is the raw value of distance (time in µS)

are you using your 2 sensors in same time..
or in alternance, with a little pause between each meaure to avoid cross detection ?
 

thanks,paul
as u rightly mentioned in practise it can measure upto 400cm(4000mm).
i did put the max limit,the object need to be detected within 100cm if the distance calculated is above that then the leds display nothing as programmed i will put up my code:
this is my first code only with one sensor interfaced & working fine
port P1 PIN 0 (echo) PIN 1(trig) connected .
port P2 LED connected it displays binary value of the measured distance in cm.
here is the code with only one sensor interfaced:
Code:
    //INITIALISATION
#include <reg51.h>
void delay(unsigned int);   // to give a pulse of  10us delay to activate ultrasound sensor
void leddelay(unsigned int);
sbit echo1=P1^0;     //echo of ultrasound sensor
sbit trig1=P1^1;      //trig of ultrasound sensor
sfr16 DPTR =0x82;
  //MAIN PROGRAM
void main()
{
unsigned int range=0,y=0;
TMOD=0X01;
trig1=0;        //o/p
echo1=1;	  //i/p
P2=0X00;    //O/P  port leds connected
while(1)
{
trig1=1;
delay(1);       10us delay for activation
trig1=0;
while(echo1==0);  
TR0=1;
while(echo1==1);   wait for object detecetion
TR0=0; 
DPH=TH0;
DPL=TL0;
if(DPTR<30000)      actually had to take 38000 but fine   
  range=DPTR*0.01847091;     //0.01847091 is value got to convert timer value into cm
else
  range=0;
if(range<100)                    
P2=range;
else
P2=0X00;
leddelay(999);     //some delay for display to be visible
}					   
}
//10us delay routine
void delay(unsigned int g)  
{
for(;g!=0;g--)
{
TMOD=0X01;
TL0=0XF6;
TH0=0XFF;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}
}
//some  delay  for leds 
void leddelay(unsigned int x)
{
unsigned int y,z=0;
for(z=0;z<30;z++)
{
for(y=0;y<9000;y++)
for(;x!=0;x--);
}
}

- - - Updated - - -

yes klaus i did feel so ..
hence, what i did was i did not set any maximum limit and with only one sensor interfaced i tried the program and with no object and good ceiling and floor clearance i tried but, i got the display of leds indicating some obstruction no matter the value what the leds displayed was large may be the max limit the sensors can measure(400cm). But, they did not display zero(which actually had to be ) that is what is worrying me as the sensor will always keep on detecting though no obstruction

- - - Updated - - -

- - - Updated - - -

paul ,klaus
this is my second program where in i have interfaced two ultrasound sensors
port p1 pin0,pin1 first ultras sensor. port p1 pin6,pin7 second ultras sensor.
port p2 leds connected.
But this program is displaying no o/p . it is displaying only when both sensors go low at once
here is the program:
Code:
#include <reg51.h>
void delay(unsigned int);
void leddelay(unsigned int);
sbit echo1=P1^0;
sbit trig1=P1^1;
sbit echo2=P1^6;
sbit trig2=P1^7;
sfr16 DPTR =0x82;
void main()
{
unsigned int range=0,y=0;
TMOD=0X01;
trig1=0;   //o/p
echo1=1;	  //i/p
trig2=0;  //o/p
echo2=1;  //i/p
P2=0X00;    //O/P
while(1)
{
trig1=1;  //delay of 10us for both sensors
trig2=1;
delay(1);
trig1=0;
trig2=0;
while(echo1==0&&echo2==0); //wait till echo pin goes high as they
// will be low initially and will go high after a 10us pulse at trig pin of sensors
TR0=1;  //start timer 
while(echo1!=0||echo2!=0);   //if echo1 goes lowfirst that mean object moving toward left 
//& if echo2 goes low first then object moving towards right if both go low then object exactly in front of sensor..
TR0=0;                      
DPH=TH0;
DPL=TL0;
if(DPTR<30000)
  range=DPTR*0.01847091;   //everything same as prev program    
else
  range=0;
if(range<100)
P2=range;
else
P2=0X00;    //don't dispaly if object far than 100cm
leddelay(999);  
}					   
}
//10us delay subroutine
void delay(unsigned int g)
{
for(;g!=0;g--)
{
TMOD=0X01;
TL0=0XF6;
TH0=0XFF;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}
}
void leddelay(unsigned int x)
{
unsigned int y,z=0;
for(z=0;z<30;z++)
{
for(y=0;y<9000;y++)
for(;x!=0;x--);
}
}
 
Last edited:

hello,



i suggest this
dont't check receiver immediatly , just after starting the emission slave
because you can detect without target in front or with interference beween both devices.
so the delay of 200µS , after triggering the emitter.
so there is a blind zone of near 30mm ahead the sensor,


is delay(1); a delay value of 10µS ?


Code:
while(1)
{
trig1=1;  //delay of 10us for both sensors
trig2=1;
delay(1);  // delay of 10µS ??
trig1=0;
trig2=0;
// Dead zoneof 30mm ; receiver must not ear emitter during emission salve   
 // wait 8 periode de 25µS( 40Khz) => 200µS
Delay_200µS();
// llop until one echo is received ,means echo level not equal 1
while((echo1==1)&&(echo2==1));
... take elapsed time

is it possible to use only one emitter centered and the 2 receivers on lateral side.
to have only one frequency to receive ( even phase will not be the same)
maybe just disconnect one US emitter.

because with 2 sender at 40KHz+-x Hz , you can get " low frequency mixing
if one is 40,5 and the other is 39,5
1 Khz modulation resulting.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top