No. 11 – Measurement of distance

Task: Measure a distance with the HC-SR04 ultrasonic sensor and show the result on the serial monitor.

Required equipment: microcontroller board / cables / Breadboard / HC-SR04 ultrasonic sensor

How does the ultrasonic sensor work?

The sensor has four pins.

a) 5V (+)           b) GND(-)             c) echo             d) trigger

The contacts 5V and GND are meant for the power supply. Through the „trigger“ pin the sensor gets a short signal (5V) from the microcontroller board to create a sound wave. As soon as the sound wave hits a wall or other objects, it will be reflected and comes back to the ultrasonic sensor. When the sensor detects this returned sound wave, the sensor will send a signal to the Arduino microcontroller through the „echo“ pin. The Arduino-board measures the time between the transmission and the return of the sound wave, and converts this time into a distance.

 

Setup:

Ultra1

 

Code:


int trigger=7; //”trigger” on pin 7.

int echo=6; //”echo” on pin 6.

long time=0; //The value “time” will safe the time between transmission and returning of the soundwave.

long dist=0; //The value “dist” will save the calculated distance. It will start with “0”. Instead of “int” //we are using “long” for this value, to save a bigger number.

void setup()

{

Serial.begin (9600); //Starting the serial communication. It will send the data from the arduino board to //the computer to show it on the serial monitor.

pinMode(trigger, OUTPUT); //”trigger” (Pin 7) is an output.

pinMode(echo, INPUT); //”echo” (Pin 6) is an input.

}

void loop()

{

digitalWrite(trigger, LOW); //Low voltage on the trigger pin to produce a clear signal.

delay(5); //….for 5 milliseconds.

digitalWrite(trigger, HIGH); //Creating the soundwave.

delay(10); //..for 10 milliseconds.

digitalWrite(trigger, LOW); //Stop creating the soundwave.

time = pulseIn(echo, HIGH); //With the command pulseIn (Capital “i” in the front of the “n”) the arduino //board measures the time between sending and receiving the soundwave.

dist = (time/2) * 0.03432; //This calculation transforms the measured time into the distance in centimeter. //(The sound needs 29,1 seconds for one centimeter. The time gets divided with two, because we only want to //get one distance and not the two ways that the soundwave has to take).This value is multiplied with the //speed of sound (cm/ms) so you get the value for the distance in centimeter.

if (dist >= 500 || dist <= 0) //If the distance gets over 500cm OR under 0cm, the measurement is no longer //accurate.

{

Serial.println(„No measurement“); //So the serial monitor displays “No measurement”

}

else //otherwise

{

Serial.print(dist); //The calculated distance is shown on the serial monitor.

Serial.println(„cm“);

}

delay(1000); //This command causes a short break between the measurements.

}


Sketch without explanations:


int trigger=7; 

int echo=6; 

long time=0;

long dist=0; 

void setup()

{

Serial.begin (9600); 

pinMode(trigger, OUTPUT); 

pinMode(echo, INPUT); 

}

void loop()

{

digitalWrite(trigger, LOW); 

delay(5); 

digitalWrite(trigger, HIGH); 

delay(10); 

digitalWrite(trigger, LOW); 

time = pulseIn(echo, HIGH); 

dist = (time/2) * 0.03432; 

if (dist >= 500 || dist <= 0) 

{

Serial.println(„No measurement“);

}

else 

{

Serial.print(dist); 

Serial.println(„cm“);

}

delay(1000); 

}


Extension of the sketch

If the distance is less than 80cm, the piezo speaker should beep.


int trigger=12;

int echo=13;

long time=0;

long dist=0;

int piezo=5; //Piezo speaker on pin 5.

void setup()

{

Serial.begin (9600);

pinMode(trigger, OUTPUT);

pinMode(echo, INPUT);

pinMode(piezo, OUTPUT); //The pin (5) connected to the piezo is a output.

}

void loop()

{

digitalWrite(trigger, LOW);

delay(5);

digitalWrite(trigger, HIGH);

delay(10);

digitalWrite(trigger, LOW);

time = pulseIn(echo, HIGH);

dist = (time/2) * 0.03432;

if (dist >= 500 || dist <= 0)

{

Serial.println(No measurement);

}

else

{

Serial.print(dist);

Serial.println(cm);

}

if (dist <= 80) //If the measured distance gets 80 or shorter…

{

digitalWrite(piezo,HIGH); //..the piezo should beep

}

else //If not …

{

digitalWrite(piezo,LOW); //..the speaker should be quiet.

}

delay(1000);

}


Reverse warning system

With this code we are able to create a reverse warning system. Additional to the ultrasonic sensor we are going to connect a LED with pin 12.

Are you able to construct a reverse warning system without any images?


int trigger=7;

int echo=6;

long time=0;

int LED=12;

long dist=0;

void setup()

{

Serial.begin (9600);

pinMode(trigger, OUTPUT);

pinMode(echo, INPUT);

pinMode(12, OUTPUT);

}

void loop()

{

digitalWrite(trigger, LOW);

delay(5);

digitalWrite(trigger, HIGH);

delay(10);

digitalWrite(trigger, LOW);

time = pulseIn(echo, HIGH);

dist = (time/2) * 0.03432;

if (dist >= 500 || dist <= 0)

{

Serial.println(No measurement);

}

else

{

Serial.print(dist);

Serial.println(cm);

}

if (dist <= 40)

{

digitalWrite(LED, HIGH);

delay(dist*3);

digitalWrite(LED, LOW);

delay(dist*3);

}

}