Introduction

In this tutorial, we will be showing you how to use the ultrasonic sensor with the Arduino.


Components you need

  • Arduino UNO or similar
  • Breadboard
  • Jumper Wires
  • HC-SR04: Ultrasonic sensor

New Components

Ultrasonic sensor

The ultrasonic sensor has 4 connection pins as shown in the figure below:

As with all electronic components that need to be powered up, the ultrasonic sensor has both ther GND and 5V pins, that will connect to the corresponding power pins on the Arduino.

The TRIG pin is used for input. We will use it to activate the sensor so that it sends out an ultrasonic wave.

The ECHO pin is used for output. We will use it to receive the time travelled by the wave from which we can deduce the distance through simple calculations.

Connecting step-by-step

Step 1

First, connect GND to the negative power row (blue) of the breadboard and 5V to the positive power row (red).


Step 2

Next, connect the negative power row of the breadboard to a column and the positive power row to another column, leaving 2 columns in between.


Step 3

Now connect the TRIG pin to pin number 10 on the Arduino.


Step 4

Connect the ECHO pin to pin number 11 on the Arduino.


Step 5

Insert the sensor so that the pins on the sensor are in the right columns.

The final circuit should look similar to the diagram below:

Code

In this section, you will learn how to write the code for controlling the servo motor. You will be able to set an angle for the rotation of the servo motor.
As we did in the previous tutorial, we have put the code below so you can read it once and then will run you through it.


Install NewPing library

Library page. Download and install with provided instructions.


Measure distance


				

				

Code explanation

Add NewPing library

#include <NewPing.h>

Imports the code needed to use the ultrasonic sensor

This pre-written code can help us to use the sensor.

Set the sensor settings

#define TRIGGER_PIN 10 #define ECHO_PIN 11 #define MAX_DISTANCE 200

Define the pins we connected to the sensors.

Initialize object

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

Initialises the ultrasonic sensor object object with the pins and maximum distance as parameters.

If the object is farther away (in cm) than MAX_DISTANCE the sensor will not detect it.

Create distance variable

int distanceCM = 0;

Sets the distance to a default value of 0.

Delay

delay(50);

The distance will be measured every 50 milliseconds (20 times per second).

Measure distance

distanceCM = sonar.ping_cm();

The library makes the distance measurement very easy. The result is saved as integer in the variable distanceCM and can be used.


Exercises

Exercise 1

As an exercise, also store the distance in inches in a seperate variable. 1 inch = 2.54 cm

Is there an easier way to do this (existing function)? Find out by reading the documentation for the NewPing library.

Exercise 2

Change the time between measurements.

Is there a lower limit for the pause between measurements?

Exercise 3

Blink an LED if an object is within a certain range, for example between 11cm and 121cm


Challenge

Move a servo in relationship to the ultrasonic distance measurement. You could control the servo with your hand.

For example a distance of 5cm could mean an angle 0. Distance 98cm could be 180 degrees.

Hint: Find out about the map function.

Next Lesson Previous Lesson