0

I am trying to write a sketch where a function will only be triggered when the ultrasound doesn't return back. Thus when the echo pin is checked after 1 second after the trigger pin was written high, it must be low. Will it be a digitalRead function?

  • Many people use the PulseIn function (arduino.cc/en/Reference/PulseIn). You can set a time-out in there. When it doesn't return before the timeout, it will return a 0. This way you can check if it's 0 (nothing) or a distance ( 0> ). – Paul Jan 20 '16 at 11:14
  • can you give me some reference on the timeout function – HDatta Jan 20 '16 at 11:24
  • The timeout isn't a function. PulseIn is the function (check the reference), timeout is an argument of that function. You can do PulseIn(echoPin, HIGH, timeout); to receive the time that pin is high. When it's longer as the set timeout, it'll return 0. – Paul Jan 20 '16 at 11:51
1

What type of Ultrasonic sensor do you have?

I'll go with HC-SR04, since I've got one of those laying around here. The other ultrasonic sensors should be working in the same way.

Before you start!

Read the datasheet. A fast search on google with: "[manufacturer][device id][device description] datasheet .pdf" will do good.

Example: http://www.micropik.com/PDF/HCSR04.pdf

The datasheet will tell you important things/values and sometimes even interfacing examples or code. Like:

  • Max input voltage
  • Max current
  • Pin-out diagrams
  • How to interface with the device
  • Min/max operating temperature

Wiring up

The datasheet will tell you what to connect, to which pins.

We've got 5V, Trigger (input), Echo (output), 0V (ground).

(As per datasheet)

It's best to do the wiring before you put power on it, because you can accidently short it when moving wires around.

Interfacing

I would recommend checking some code examples before you attempt to roll your own. Or just start from an example and edit it to your specifications.

The timing diagram shows us it needs a 10uS pulse on the trigger input. And the duration of the echo will show the distance (58 uS per cm or 148 uS per inch).

The datasheet also suggest to use over 60ms measurement cycles.

Code

Taking all that into account:

#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10

const unsigned long timeout = 400*58; //Max of 400CM, adjust this to your own max.

void setup() {
  Serial.begin(115200);//Stop using 9600 as default, it's not fast.
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
}

void loop() {
  unsigned long distance;//unsigned, since we don't get negative distances.
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); //10 uS pulse.
  digitalWrite(trigPin, LOW);

  distance = round(pulseIn(echoPin, HIGH,timeout) /58);//Distance in CM's, use /148 for inches.
  if (distance == 0){//Reached timeout
    Serial.println("Out of range");
  }
  else {
    Serial.print(distance);
    Serial.println(" cm");
  }
  delay(60);//Wait 60 ms for next cycle.
}

To be honest, I'm not even slightly sure if this code is working. I modified this code: http://www.instructables.com/id/Simple-Arduino-and-HC-SR04-Example/

So that you're actually using the timeout right, and do the timing according to the datasheet.

  • This is my first time working with arduino so I was having trouble grasping the format of the PulseIn function. Do we need to put some time in place of time-out or does it use the delayMicroseconds(10) value... – HDatta Jan 20 '16 at 14:03
  • @Reptile1234 in my example, I added the line const unsigned long timeout = 400*58; //Max of 400CM, adjust this to your own max. effectively storing the value of 400*58 in the variabel named timeout, and used this as an argument to the PulseIn function. You could also just put the value in the arguments of that function. The delayMicroseconds is to put the trigger pin high (wait 10uS) and then low, so the ranging will start. – Paul Jan 20 '16 at 14:08
  • What is the use of making the variable? – HDatta Jan 20 '16 at 14:21
  • Not much really, it's more readable and easier to change I guess. – Paul Jan 20 '16 at 14:24
  • so what is meant by the 400cm there... I thought that timeout will have a specific amount of time. – HDatta Jan 20 '16 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.