Hello all. I've been working on a robot using the exp. from a book "Make an Arduino-controlled Robot" will i have it mostly working but i hit a wall with the sonar distance sensors the book uses a Parallax sensors with 3(Vcc,ping,Gnd) pins and i have a HC-SR04 with 4(Vcc,trig,echo,gnd). i edited the code to sperat the ping pin to trig and echo. but its don'ts seem to work. if anyone can hello me. i would be thankful. i add the code maybe someone with more coding knowledge can fine the problem.
/*********************************
code for ping distance sensor
**********************************/
// Returns the distance in inches
// this will return 0 if no ping sensor is connected or the distance is greater than around 10 feet
int pingGetDistance(int echoPin)
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
int trigPin =5;
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
analogWrite(trigPin, LOW);
delayMicroseconds(2);
analogWrite(trigPin, HIGH);
delayMicroseconds(5);
analogWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH, 20000); // if a pulse does not arrive in 20 ms then the ping sensor is not connected
if(duration >=20000)
duration = 10000;
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
return (cm * 10) / 25 ; // convert cm to inches
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
In myexperience with these sonars I found that they MUST have a solid 5 volt supply or they give wierd results or don't work. I was using USB power when I first hooked them up and fought for hours trying to get cnsistant ranges. Then I found out about the 5v requirement and tried a better supply (9V battery). They worked great. The USB was only putting out about 4.75 V and that is not enough.
Is that the entire sketch? If so, you need to define a setup and loop function for it to work.
Also, you seem to be setting the return time to 10000 if the pulse is out of range? And then also divided by 2 when converting to cm.
Your main problem is that you are using analogWrite on the outpulse when you should be using digitalWrite.
digitalWrite can either be HIGH or LOW, while analogWrite can be 0-255. You used analogWrite(trigPin,HIGH)
Anyway, here's some updated code.
/****************************
distance sensor code
****************************/
/*********************************
code for ping distance sensor
**********************************/
// Returns the distance in inches
// this will return 0 if no ping sensor is connected or the distance is greater than around 10 feet
int inches;
void setup() //Setup function, arduino runs this at beginning once time.
{
Serial.begin(9600);
}
void loop() //Loop function, arduino runs this repeatedly.
{
inches = pingGetDistance(????); //Replace the ???? with whatever pin echo is hooked to.
Serial.println(inches);
delay(25);
}
int pingGetDistance(int echoPin)
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
int trigPin =5;
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH, 20000); // if a pulse does not arrive in 20 ms then the ping sensor is not connected
if(duration >=20000)
duration = 10000;
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
return (cm * 10) / 25 ; // convert cm to inches
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
The HC-SR04 requires a 10 uSec pulse to trigger... your 5 uSec pulse probably isn't long enough. [Edit: NewPing comment says that testing shows initial LOW has to be 4 uSec to be reliable as well.]
There's a Ping library to simplify this, but I recommend the NewPing library... it's easier to use and has a timer mode that allows you to continue with other processing while waiting for the echo. It works with my HC-SR04 just fine. (I've been working on a similar robot this week... had the HC-SR04 up and running in minutes with NewPing.)
Marduk:
Hello all. I've been working on a robot using the exp. from a book "Make an Arduino-controlled Robot" will i have it mostly working but i hit a wall with the sonar distance sensors the book uses a Parallax sensors with 3(Vcc,ping,Gnd) pins and i have a HC-SR04 with 4(Vcc,trig,echo,gnd). i edited the code to sperat the ping pin to trig and echo. but its don'ts seem to work. if anyone can hello me. i would be thankful. i add the code maybe someone with more coding knowledge can fine the problem.
You'll have problems with that sketch. You can't set the pulseIn timeout like that because ultrasonic sensors sometimes take 20ms before they even start. Really, using pulesIn is not an option. The NewPing library takes care of all of this for you. Allowing you to simply focus on doing something with the results rather than trying to get it working. Also, if you're doing a robot, I would HIGHLY suggest you use a more interrupt-driven sketch. NewPing works well with this using the ping_timer() method. Robots don't typically turn out the way you'd like if don't use an interrupt-driven or more multi-tasking like programming paradigm.