ultrasonic sensor hc-sr04: how to give digital output

hi ive just got the hc-sr04 and ive been scouring the web but i cant find any thing
i want to give a digital output using
if (distance > 30) command but i have no luck
thanks

p.s i only started coding a few weeks ago

Was there nothing in the 109,000 results that google managed to find?

Start here: http://playground.arduino.cc/Code/NewPing

I always start by reading and understanding any example sketches I can find. If you get stuck you can always ask on the forum for an explanation.

Happy pinging.

Edit: I'd guess from your other post on this subject that you just want someone to write a sketch for you. Better to have tried and failed than to never have tried at all.

If you can't supply the code, we cannot say why it does not work.

Weedpharma

Going by your other thread on what I suspect is the exact same question, you want the LED to come on when the distance is over a certain value?

You said in that other thread you can't find how to code an if?... you clearly didn't look very well, since for the exact answer you don't even need to leave this site.

When you're back at your own computer, get back to us with the code you have so far. If it doesn't work, explain what's happening that shouldn't be, or what isn't that should be, and then you'll get some proper help.

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int cm = 0;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
pinMode ( 13, OUTPUT);
Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
if ( cm >= 30)
digitalWrite( 13, HIGH);
}

sorry guys about the messy post i thought i posted on another thread but I’m here now so heres my code, i can’t test it because my breadboard is coming tomorrow annoyed face so year thanks and also wow this fourm is fast and its the first one I’ve used.

ps I’ve changed it: I’ve added int cm = 0; and arduino software says makes sense but i don’t know if it will blink

You’re not putting the calculated distance into the variable called cm. (Not to be confused with the text “cm” that it prints.)

So you need something like this:

unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
cm = uS / US_ROUNDTRIP_CM; //put the calc'd distance into variable cm
Serial.print(cm); //print the variable's value
Serial.println(" cm");  //print the label, note the leading space

if ( cm >= 30)  
digitalWrite( 13, HIGH);

Also note that code snippet of mine is in a code box, which you do by selecting the code and hitting the </> icon above the :slight_smile: smiley

AWSEOME thanks for posting under 1h and being patient with my coding skills i will reply in a few days and tell you if it works

how do you reply so fast?

okay this is what i got

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int cm = 0;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  pinMode ( 13, OUTPUT);
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
cm = uS / US_ROUNDTRIP_CM; //put the calc'd distance into variable cm
Serial.print(cm); //print the variable's value
Serial.println(" cm");  //print the label, note the leading space

if ( cm >= 30)  
digitalWrite( 13, HIGH);
}
[code]

JimboZA asked you to put your code in a code box not as you have just replied.

Put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Weedpharma

help9meh:
how do you reply so fast?

11am on a Sunday....

help9meh:
okay this is what i got

Well, let us know how it works out once you try it. I see the closing } is missing, but that's probably just a copy/paste error.

Pretty please, put the code in a box like I did. Go back and edit that last post and just put a [code] in front and [/code] behind so that...

the code
{looks}
like this

done

fg�2��22��3��2�2��32f�2��g�2�332r����fr2���3�ff23�gg�2g���2g��r�22�32f32r���2rד2322�3�r�22���r�2�גg2��32g3�2��fg�2��22��3��2�2��32f�2��g�2�332r����fr2���3�ff23�gg�2g���2g��r��grg���2��3�r2��gr���22�2���2�����3�2r���fg��2�2�3�r��2��2g���3�2�2�332g�2g�2��3�2��f���32������rg�����f��3�2��r32f���g�g3��2�3�r��g22�g22�2����2��g����2�2��32r�2g��3�����rg����2g���3��2f���g��r����2��23rr����g��f��32���g3�g�2��222���2��2g���3�2�2�332r�2g���������rg���2g3�����2g���g�2��32��r�2g�����2g��322���g3��2��222���2��2g���;�2�2��32g�2g���3����3�gfr3�g���3�22��3�fr2����rg���3�r22��g32f����32f3���g�g3�322�3�g3�22��2322�3��r��2���rg�22���2��2��gr��g��2��32r����gr2����fg���3�f22��2g�2g���3�gg3��3�2g��3�r22����2g��r��222��g thats what im getting on my serial monitor
here is my code and I’m not using a breadboard I’m using a pc board

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int cm = 0;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  pinMode ( 13, OUTPUT);
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
cm = uS / US_ROUNDTRIP_CM; //put the calc'd distance into variable cm
Serial.print(cm); //print the variable's value
Serial.println(" cm");  //print the label, note the leading space

if ( cm >= 30);
digitalWrite( 13, HIGH);
}
  [code]

Try a baud rate of 9600....

Serial.begin(9600);

edit... or check that the monitor is set to 115200 in the bottom right corner.

thanks and i did some tests with just it on its own and the pc bard is faulty

ohh yea and it was set to 9600 thanks

does it also matter if the lights light up

help9meh:
does it also matter if the lights light up

What lights?

By the way, I don't think there's anything in your code to turn the LED off again, once you've turned it off. So if the distance goes over 30cm and the LED goes on, it will stay on if it goes under 30 again. You need an "else" to set it low.

okay did it again and all i got was a high pitch whine from the ultrasonic sensor

If I remember I'll try your current code later today when I get home. It's 830am here right now, though.