Arduino and HC-SR04 ultrasonic sensor

10 september 2011 | Classified in: Electronic | Tags: Arduino, Ultrasonic, Sensor, HC-SR04

Here is just a simple code example.

Video:


Code:


/*
 HC-SR04 Ping distance sensor]
 VCC to arduino 5v GND to arduino GND
 Echo to Arduino pin 13 Trig to Arduino pin 12
 More info at: http://goo.gl/kJ8Gl
 */

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 200 || distance <= 0){
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(500);
}

The Theory Behind It:

In the program above, we want to calculate the distance of an object in front of the ultrasonic sensor. This sensor can send a "ping" at a given moment and receive the ping bouncing back on an object at another given moment.
A ping is nothing but a sound that is inaudible for the human hear and this is why this sensor is called "ultrasonic".
The sensor send a ping at a time t1 and receive the bouncing ping at a time t2.
Knowing the speed of sound, the time difference Δt = t2 - t1 can give us an idea of the distance of an object.

Example, if Δt = 500 microseconds, we know it took 250 microseconds for the ping to hit an object and another 250 microseconds to come back.
The approximate speed of sound in dry air is given by the formula:
c = 331.5 + 0.6 * [air temperature in degrees Celsius]
At 20°C, c = 331.5 + 0.6 * 20 = 343.5 m/s
If we convert the speed in centimetres per microseconds we get:
c = 343.5 * 100 / 1000000 = 0.03435 cm/ųs
The distance is therefore, D = (Δt/2) * c
or D = 250 * 0.03435 = 8.6 cm

Instead of using the Speed of Sound, we can also use the "Pace of Sound".
The Pace of Sound = 1 / Speed of Sound = 1 / 0.03435 = 29.1 ųs/cm
In this case the equation to calculate the distance become: D = (Δt/2) / Pace of sound
and for the example above: D = 250 / 29.1 = 8.6 cm

61 comments

Avatar Gravatar Randy said: transparent#1 [friday 24 february 2012 at 05:44]

Thanks. This worked perfect on the first try.

Avatar Gravatar rou said: transparent#2 [sunday 04 march 2012 at 17:01]

thank you very much !!!

Avatar Gravatar grisisiknis said: transparent#3 [tuesday 27 march 2012 at 17:48]

bookmarked!!!

Avatar Gravatar elen said: transparent#4 [tuesday 27 march 2012 at 20:05]

I need your help, PLEASE!!! I beg you!

how do I send the signal from the US sensor to a buzzer?

This is what I did, but it's not working. Please help!

int ultraSoundSignal = 7;
int val = 0;
int ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13;
int BUZZ = 4
byte i;

void setup()
{
Serial.begin(9600); // Sets the baud rate to 9600
pinMode(ledPin, OUTPUT); // Sets the digital pin as output
}

void loop()
{
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/

digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse
* -------------------------------------------------------------------
*/

pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}
while(val == HIGH)
{ // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}

ultrasoundValue = timecount; // Append echo pulse time to ultrasoundValue
serialWrite('A'); // Example identifier for the sensor
printInteger(ultrasoundValue);
serialWrite(10);
//serialWrite(13);

if(timecount > 0)

{
pinMode(BUZZ,OUTPUT);
for (i=0;i<6;i++)
{
digitalWrite(BUZZ,HIGH);
delay(500);
digitalWrite(BUZZ,LOW);
delay(500);
}
}

Avatar Gravatar Virtualmix said: transparent#5 [saturday 31 march 2012 at 03:58]

@elen: Hello! I would like to help but I don't think I understand what you would like to do.
What is a "US sensor" and how do you want it to interact with a buzzer?
The HC-SR04 is used to calculate a distance but I cannot see any distance calculation in your code.
Please provide more explanation :)

Avatar Gravatar Mattmouthe said: transparent#6 [thursday 05 april 2012 at 13:18]

Thank you for your precious advices! If you want, I made a diagram of the assembly
[img] https://lh6.googleusercontent.com/0MBDh9JeEdOUaRABh5vOTBiYmamo6eAbVURFq0yq0KS_iWtwhYWs5Ytx67R2o3NNeHO3qkrDtCtA6YYOx5AMuD6l0A[/img]
See you
Mattmouthe

Avatar Gravatar faithfull said: transparent#7 [friday 06 april 2012 at 00:20]

First of all, nice job!!! And thx for the code.

I have one question, can you help me by coding a lcd panel for the hc-sr04 ? i will get the distance on it.

Sorry for my bad english.

greetings from germany

Avatar Gravatar Virtualmix said: transparent#8 [friday 06 april 2012 at 17:54]

@faithfull: I can try to help. What modele of LCD panel is it? Have you started writing any code?

Avatar Gravatar faithfull said: transparent#9 [friday 06 april 2012 at 22:11]

i am a absolutly beginner. I am learning by my self with the arduino examples and the www.

But i have no idea how to connect a lcd pannel on a breadboard with the arduino.
I buyed this one here (PDF of my LCD Panal):

http://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCcQFjAA&url=http%3A%2F%2Fwww.mikrocontroller.net%2Fattachment%2F61375%2Flcd_av1624.pdf&ei=jk1_T-_kJuiG0AWkq7D9Bg&usg=AFQjCNGXq13elmWzGfd2-BiJGUW3-KG3mw&sig2=HqLh4fAb8QlV8WFcu13Gig

Avatar Gravatar Virtualmix said: transparent#10 [saturday 07 april 2012 at 06:17]

@faithfull: You should use the LiquidCrystal Library available for Arduino.
Try to run this example code and then you can modify it to display anything you want, like the distance measured with the HC-SR04 sensor.
From what I can see, your LCD looks like a standard one. It's a bit of a pain to wire it up to the Arduino but if you follow the Arduino tutorial you should be ok.
If you don't want to bother with a lots of cables, you can get a "lcd keypad shield", very cheap from ebay and extremely easy to use. Cheers!

Avatar Gravatar Virtualmix said: transparent#11 [saturday 07 april 2012 at 06:54]

@faithfull: The code will look like that (not tested):

#include <LiquidCrystal.h>

#define trigPin 2
#define echoPin 3

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void setup() {
lcd.begin(16, 2);
lcd.print("Distance (cm):");
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
lcd.setCursor(0, 1);
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
lcd.print("Out of range");
}
else {
lcd.print(distance);
}
delay(500);
}

Avatar Gravatar faithfull said: transparent#12 [saturday 07 april 2012 at 10:40]

thany you very much.

i will try this.
before i started with arduino (one month ago), i thought that the coding must be very difficult and long, but i see, it was much easier...

Avatar Gravatar sham said: transparent#13 [sunday 08 april 2012 at 13:48]

thak you very mucth work fine

Avatar Gravatar faithfull said: transparent#14 [tuesday 10 april 2012 at 14:19]

Hello, it´s me again.
I have one little issue with my HC-SR04. I have just a range between 1 cm and 6 cm.
I can do anything but it wont help, can you help me please ? I took a Video maybee it helps to understand what i am talking about. If nothing is in front of the HC-SR04 it shows 6 cm, and the closest measurement is 1 cm.

http://www.youtube.com/watch?v=BTvVEg4tPyc

Avatar Gravatar faithfull said: transparent#15 [tuesday 10 april 2012 at 15:09]

My friend, i found the issue. my false was that i put the hc-sr04 in the middle of my breadboard. at the end of it, it will give me correct measurements.

thanks :)

Avatar Gravatar Virtualmix said: transparent#16 [wednesday 11 april 2012 at 11:07]

@faithfull: Thanks for the message! I have done the same mistake before but the range was a bit different (only working between 8 & 20cm)...
Glad you've solve the problem!

Avatar Gravatar faithfull said: transparent#17 [friday 20 april 2012 at 00:45]

I want to say thank you again. My HC-SR04 works greate. i have worked a few days on my new code and think it is still perfekt for my. here you can see a short vid of my battleground :)

http://www.youtube.com/watch?v=CWlVmYTiF9E

Avatar Gravatar virtualmix said: transparent#18 [friday 20 april 2012 at 17:53]

@faithfull: your video is awesome and you've done a very nice work with wiring everything together. Thank you so much for sharing the link here!

Avatar Gravatar nick27 said: transparent#19 [friday 20 april 2012 at 18:49]

hi virtual mix...i very impressed at this blog very helpfull.i was strugling alot to undrstand the math behind the sensor but now its cleared..thanks alot.

Avatar Gravatar nick27 said: transparent#20 [friday 20 april 2012 at 18:50]

but i need major help on how to code for the sensor in c using a pic18f452..
could you please help me with a sample code or any resource which could help me !!please!!
i need some serius help with my project..

Avatar Gravatar faithfull said: transparent#21 [friday 20 april 2012 at 19:44]

finaly finished: http://www.youtube.com/watch?v=Nz7B6i59cYk

i will mount this in a toy car as a parkingassistent system :)

Avatar Gravatar Virtualmix said: transparent#22 [monday 23 april 2012 at 12:27]

@ faithfull: Well done mate!

@nick27: I wish I could help but I have absolutely no experience with PIC programming :-/

Avatar Gravatar muffin123 said: transparent#23 [sunday 03 june 2012 at 20:04]

hello,

i am using the hc-sro4 with the ardumoto motor driver shield and the Arexx "Mr. Basic" robot platform. i can't figure out how to make the motors respond to the distances calcuated by the hc-sro4. could someone please help me write the code for it?

Avatar Gravatar Virtualmix said: transparent#24 [monday 04 june 2012 at 10:55]

@muffin123: Could you please give a bit more details? What do you want the motors to do exactly?
Cheers.

Avatar Gravatar muffin123 said: transparent#25 [monday 04 june 2012 at 19:06]

hi,

when the hc-sro4 detectes an object 5 cm from the sensor i want the motors to reverse for 1 second then i want motor A to to go forward and motor B to go backward for 500 miliseconds then i want both motors to go forward.

Avatar Gravatar multifungi said: transparent#26 [tuesday 05 june 2012 at 19:10]

Thanks for the code! I was looking around downloading multiple libraries and your clean code was the only one that worked, now i try to mix it with a waveshield. Thanks!

Avatar Gravatar muffin123 said: transparent#27 [friday 08 june 2012 at 21:13]

hey, Vurtualmix

what's taking you so long to respond? I've been waiting 4+ days...

Avatar Gravatar Virtualmix said: transparent#28 [saturday 09 june 2012 at 14:05]

@muffin123: What you want to do is easy and I am sure you can do it yourself, especially with all the example code above.

Now, 5 cm is very close distance from the sensor, I recommend you to use a larger range (15cm seems more reasonable) in order to have better results.

Here is a little bit of help but you will need to improve this yourself.
//------------------------------------------------------------------------------------------//
#define trigPin 12
#define echoPin 13
//This is where you will plug the motors, change it if necessary
#define MotorForwardA 5
#define MotorBackwardA 6
#define MotorForwardB 7
#define MotorBackwardB 8
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
//set motors as output
pinMode(MotorForwardA, OUTPUT);
pinMode(MotorBackwardA, OUTPUT);
pinMode(MotorForwardB, OUTPUT);
pinMode(MotorBackwardB, OUTPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else if (distance <= 15){
digitalWrite(MotorBackwardA, HIGH);
digitalWrite(MotorBackwardB, HIGH);
digitalWrite(MotorForwardA, LOW);
digitalWrite(MotorForwardB, LOW);
delay(1000);
digitalWrite(MotorBackwardA, LOW);
digitalWrite(MotorBackwardB, HIGH);
digitalWrite(MotorForwardA, LOW);
digitalWrite(MotorForwardB, LOW);
delay(500);
digitalWrite(MotorBackwardA, LOW);
digitalWrite(MotorBackwardB, LOW);
digitalWrite(MotorForwardA, HIGH);
digitalWrite(MotorForwardB, HIGH);
}
delay(500);
}

Avatar Gravatar muffin123 said: transparent#29 [sunday 10 june 2012 at 03:55]

thank you so much for the code!!!

Avatar Gravatar Samuel Dette said: transparent#30 [thursday 14 june 2012 at 19:19]

Hi there, nice job and thanks for the code! It works but I still got two problems. First is, that the frequency of messuarind the distance is to low. I need that to be faster for using the "US-sensor" ;) as an instrument for a live-performance. The other thing that worries me is the irregularity that comes up sometimes. Once in every 5-10 "Messuares" there is a totally wrong number that (in my project) would make a strange noise (and that woulnd't be nice...)

Can you help me with that?

Cheers, Samuel

Avatar Gravatar Samuel Dette said: transparent#31 [thursday 14 june 2012 at 19:29]

Actually the wrong messuared values pop up every 5th time and if you come to a distance further than 40 cm the values get even more unreliable.

And my 3rd problem is the very wide spread of the "ultrasonic wave beam". I actually wanted to put the senosr on the floor and work within distancies between 20 - 200 cm. But the ultrasonic wont get reflected by my hand if I am further away than aprox 80 cm...

I would be very glad, if someone could help me here.

Greets to faithfull as well from germany :)

Avatar Gravatar Rizz said: transparent#32 [wednesday 20 june 2012 at 10:52]

hi
i tried to build a program based on your code to make 2 hc-sr04 working but it seems like both of my sensor are not working.here is my program.please help me

#define trigPin1 13
#define echoPin1 12
#define trigPin2 11
#define echoPin2 10
#define led1 7
#define led2 6
#define led3 5

void setup()
{
Serial.begin (9600);
pinMode (trigPin1, OUTPUT);
pinMode (echoPin1, INPUT);
pinMode (trigPin2, OUTPUT);
pinMode (echoPin2, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
}

void loop()
{
int duration1, distance1, duration2, distance2;
digitalWrite (trigPin1, HIGH);
digitalWrite (trigPin2, HIGH);
delayMicroseconds(1000);
digitalWrite (trigPin1, LOW);
digitalWrite (trigPin2, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2/2) / 29.1;

if (distance1 >= 200 || distance1 <= 0)
{
Serial.println ("out of range");
}
else if (distance1 > 0 && distance1 <= 10 && distance2 > 0 && distance2 <= 10)
{
digitalWrite (led1, HIGH);
digitalWrite (led2, HIGH);
digitalWrite (led3, HIGH);
Serial.print(distance1);
Serial.println(" cm");
}
else if (distance1 >= 10 && distance1 <= 20 && distance2 >= 10 && distance2 <= 20)
{
digitalWrite (led1, LOW);
digitalWrite (led2, HIGH);
digitalWrite (led3, HIGH);
Serial.print(distance1);
Serial.println(" cm");
}
else if (distance1 >= 20 && distance1 <= 30 && distance2 >= 20 && distance2 <= 30)
{
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, HIGH);
Serial.print(distance1);
Serial.println(" cm");
}
else if (distance1 >= 10 && distance1 <= 20 && distance2 >= 20 && distance2 <= 30)
{
digitalWrite (led1, LOW);
digitalWrite (led2, HIGH);
digitalWrite (led3, HIGH);
Serial.print(distance1);
Serial.println(" cm");
}
else if (distance1 > 0 && distance1 <= 10 && distance2 >= 20 && distance2 <= 30)
{
digitalWrite (led1, HIGH);
digitalWrite (led2, LOW);
digitalWrite (led3, HIGH);
Serial.print(distance1);
Serial.println(" cm");
}
else
{
digitalWrite (led1, LOW);
digitalWrite (led2, LOW);
digitalWrite (led3, LOW);
Serial.print(distance1);
Serial.println(" cm");
}
delay(500);
}

Avatar Gravatar mithun said: transparent#33 [thursday 28 june 2012 at 17:51]

If any one know,how to write micro-c cord to use ping sensor(3-pin sonar sensor)
by using "mikroC PRO for PIC" help me.
thanks.

Avatar Gravatar Virtualmix said: transparent#34 [friday 29 june 2012 at 17:32]

@Samuel Dette: For your first issue, I would recommend to add a loop in the code and average the measured values. You will loose a bit of accuracy per reading but will get consistent results.
The second issue is simply a limitation of the sensor, you need to use it at a certain level above the ground to receive a decent measurement.

@Rizz: Do one sensor at the time, (code for first sensor, then code for second sensor) instead of combining them together.

@mithun: Sorry I have no experience with PIC, maybe someone else here can help.

Avatar Gravatar Anthony said: transparent#35 [monday 09 july 2012 at 18:02]

change
int duration, distance;
to
float duration, distance;
for maybe more precision

Avatar Gravatar Anthony said: transparent#36 [monday 09 july 2012 at 18:26]

And if you have a thermistor, you can plug that in to get current temp of where your sensor is

float duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);

Avatar Gravatar Anthony said: transparent#37 [monday 09 july 2012 at 18:27]

\\Comment modified by moderator: Please use Pastebin to paste long code instead of comments. Thank you.\\
Code example using termistor

Avatar Gravatar girish gujar said: transparent#38 [thursday 02 august 2012 at 19:41]

does any 1 have avr code for this??

Avatar Gravatar BB haji said: transparent#39 [thursday 02 august 2012 at 22:38]

Hi,
would someone tell me how you get the command line to show you the reading for the distance?
I have uploaded the code and then what?
thank you

Avatar Gravatar Titch said: transparent#40 [friday 14 september 2012 at 14:23]

I uploaded this worked first time no issues. on test found to be precise has given a basis for experimentation thanks. nice explanation on the math straight forward and to the point and good worked example are you a tutor by chance lol?

Avatar Gravatar Titch said: transparent#41 [friday 14 september 2012 at 15:00]

@mithun: on a PIC you will have a T0INT pin which is timer 0 interupt pin or depending on the PIC it might be T1INT or words to that effect you set the timer to count clearing the timer overflow flag in the INTCON reg measure the duration of the count from the interupt of the pulse.... also use a zener diode in reverse bias to clamp input and reduce noise. But the code is vastly more complicated on PIC than in arduino I would personnally recommend using arduino for what you are trying to do but if you are going to use PIC regardless perhaps a 16F690 would be a good 1 to use as you can program the Fosc from about 36KHz to 8MHz and prescaler 1/256. I wish you good luck in your project

Avatar Gravatar llama said: transparent#42 [sunday 30 september 2012 at 20:23]

thank you~ and nice background song haha

Avatar Gravatar Tim Shaw said: transparent#43 [tuesday 09 october 2012 at 18:34]

Hello.

Thanks for this, its great. I got it working reading centimetres straight away.

Do you know if there any way to internally route the PMW signal to MIDI that can be ready MAX or Ableton?

I want the different signal values to correspond to different notes playable by the synths in Ableton. I am quite new to code do you know where I would put the corresponding code in the sketch?

Any help would be very much appreciated.

Thanks
T

Avatar Gravatar NN said: transparent#44 [thursday 18 october 2012 at 08:02]

can anyone help my problem out. i want to change the distance from centimeter into percentage. can anyone help me how to create the program?

Avatar Gravatar virtualmix said: transparent#45 [friday 19 october 2012 at 13:48]

@NN: In the program above, replace this part:

else {
Serial.print(distance);
Serial.println(" cm");
}

With this:

else {
distance = distance/80*100;
Serial.print(distance);
Serial.println(" percent");
}

This is assuming 80cm is the maximum distance (100%) and 0 cm is the minimum distance (0%).

Disclamer: Not tested but it should work. Adapt if necessary.

Avatar Gravatar Ehrenmal said: transparent#46 [tuesday 23 october 2012 at 04:25]

Hello, thank you very much for the code. It seems to be the only reliable one.

The only issue I have is that it stops detecting at about 37 cms or so. The sensor is supposedly detects up to 400cm or so I read.

Is this true? Is there a way to augment its reach?

Avatar Gravatar virtualmix said: transparent#47 [tuesday 23 october 2012 at 13:02]

@Ehrenmal: Glad to hear the code is useful.
The operating range for the sensor shown on the video is from 10cm to 80cm. This is not something you can modify yourself. If you need a different operating range you will have to use a different sensor. Have a look at infrared sensors (Sharp Sensors are cheap and easy to use).
If your sensor stops detecting at 37cm, ensure there is enough clearance below the sensor. Someone had a same issue in comments above.

Avatar Gravatar Jihru said: transparent#48 [saturday 03 november 2012 at 08:29]

Hi.!. Please help me on my project. I need asm code for ultrasonic sensor. I am using US-100 UltraSonic Sensor and PIC16F877A. What the code needs to do is detect object within 1-8 centimeters from sensor then it has to blink an LED to indicate that there is an object within that range. I really appreciate it if anyone can help me.

Avatar Gravatar HaiYang said: transparent#49 [thursday 08 november 2012 at 15:36]

Since the datasheet mentions that the trigger input signal should last for 10us, wouldn't it be better to delay the trigPin for 10 microseconds?

Avatar Gravatar virtualmix said: transparent#50 [friday 09 november 2012 at 09:19]

@HaiYang: Thank you for your comment, if in the datasheet then it's probably a very good idea! I will test and modify my article when I get a chance and if it gives good result :-)

Avatar Gravatar geonn said: transparent#51 [wednesday 21 november 2012 at 16:31]

Hi! I'm trying to connect 4 Sr04 sensors but when i try to grab distances from more than two sensors, the serial seems to lag. Do you know what i can do for that?

Avatar Gravatar geonn said: transparent#52 [wednesday 21 november 2012 at 16:42]

here is my code::

int trigPin[] = {6,8,10,12};
int echoPin[] = {7,9,11,13};
int duration[4], distance[4];


void setup() {
Serial.begin (115200);
for (int i=0; i<4; i++) {
pinMode(trigPin[i], OUTPUT);
pinMode(echoPin[i], INPUT);
}
}

void loop() {
for (int i=0; i<4; i++) {
digitalWrite(trigPin[i], HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin[i], LOW);
duration[i] = pulseIn(echoPin[i], HIGH);
Serial.print("<");
Serial.print(duration[i]);
Serial.print("-");
Serial.print(i);
Serial.print(">");
delayMicroseconds(500);
}
}

Avatar Gravatar sara said: transparent#53 [sunday 16 december 2012 at 20:13]

Hi
thank u so much
did you use at mega8 microcontroller for programming??
please give answer

Avatar Gravatar virtualmix said: transparent#54 [thursday 20 december 2012 at 11:33]

@sara: I used an Arduino Uno with an ATmega 328

Avatar Gravatar virtualmix said: transparent#55 [thursday 20 december 2012 at 11:40]

@geonn: I am not sure exactly why your code is not working but if I were you I would do four different function, one per sensor. Then call these functions independently in loop{} and test each one of them separately for debugging.
(It's just an idea I haven't tested...)

Avatar Gravatar thomas said: transparent#56 [wednesday 26 december 2012 at 12:29]

how do you connect it to an out put and could you give me a diagram (i dont know these things got it the arduino at christmas and im only 11

Avatar Gravatar Richard said: transparent#57 [friday 28 december 2012 at 12:25]

Many thanks. Your example worked first time! Now I know my sensor works, time to build my first bot! :-D

Avatar Gravatar Steven said: transparent#58 [wednesday 09 january 2013 at 18:22]

Is it possible to use 2 sensors, one on either side, and then add them together so that the printout on the LCD screen only shows one number, the totals from both sensors added together....please help me, I need this for a project. Thanks a lot.

Avatar Gravatar aries said: transparent#59 [thursday 10 january 2013 at 16:12]

how can I make a program using the ultrasonic sensor ping))) in connection with my arduino board? our project is a sensor that will provide the obstruction/object for the blind people? do you know a program?

Avatar Gravatar ammon D said: transparent#60 [friday 25 january 2013 at 20:55]

I cannot get it to print to the serial monitor, this error comes up when I try to upload: avrdude: stk500_getsync(): not in sync: resp=0x00. My OS is Windows 7, does that have anything to do with it?

Avatar Gravatar Vikash said: transparent#61 [friday 22 february 2013 at 13:32]

Where can i view the result. i am using Windows7. Please help me.

Write a comment

Capcha
Enter image code : 

Categories

Last articles

Last comments


Member of the Internet Defence League