Using Arduino with Parts and Sensors – Ultrasonic Sensor

Arduino with ultrasonic sensor

Arduino with ultrasonic sensor

Last time, we dealt with the photoreflector, using its properties to write programs. This time, we will look at the ultrasonic sensor. The ultrasonic sensor is often used to measure distance in the same way as the photoreflector. Compared to the photoreflector, which only makes very close measurements, over a few centimeters, the ultrasound sensor is able to measure objects at a distance of around 2cm to 4m.

Today’s Electronics Recipe

Time to complete – 90 minutes

Required parts

Arduino (Arduino Uno R3) https://www.adafruit.com/products/50
Breadboard https://www.adafruit.com/products/64
Ultrasonic ranging module (HC-SR04 http://www.amazon.com/SainSmart-HC-SR04-Ranging-Detector-Distance/dp/B004U8TOE6)
Temperature sensor (LM61CIZ http://www.mouser.com/search/ProductDetail.aspx?R=0virtualkey0virtualkeyLM61CIZ-NOPB)
3 digit 7-segment LED (ROHM LB-303MA http://www.mouser.com/ProductDetail/ROHM-Semiconductor/LB-303MA/?qs=hegm%2fm%252b%2fz5mYWUQkvy7vww%3d%3d)
Resistor 220Ω

What Is an Ultrasonic Sensor?

Arduino Ultrasonic sensor
Picture 1 Ultrasonic sensor

What is an ultrasonic sensor exactly? The ultrasonic ranging module we will use this time, the HC-SR04, is designed such that it outputs, a 40kHz frequency wave from one of the two parts attached to the module, an ultrasonic speaker, and receives the reflected sound using the other, an ultrasonic microphone. By sending out ultrasound waves, reflecting it off objects and measuring the time it takes to come back, it is able to measure the distance to that object.

The ultrasonic wave is a “sound”. So, to calculate the distance, we multiply the time taken with the speed of sound.  The speed of sound in the air can be determined by “331.5 + 0.6 t (m/sec)“, where “t” is room temperature in degrees Celsius.

Using the Ultrasonic Sensor

Let’s try using the ultrasonic sensor. Unlike electronic parts like the photoreflector, which we saw last time, all the circuit needed  is already incorporated in the ultrasonic sensor module. So, we are able to connect the Arduino and the module directly.

Arduino ultrasonic sensor
Picture 2 Terminals on the ultrasonic sensor

The ultrasonic sensor has the following terminals:

  • Vcc – Power in
  • Trig – Trigger, send a signal for emission of ultrasound wave
  • Echo – Echo, receive signal on reception of ultrasound wave
  • GND – Ground

The simplest way to connect it to the Arduino is by forming the circuit shown below.

Arduino ultrasensor breadboard wiring
Figure 1 Connecting the ultrasonic sensor module and the Arduino

Arduino ultrasonic sensor breadboard wiring
Picture 3 Test circuit for ultrasonic sensor module

Let’s start writing the program. By checking how to use the ultrasonic sensor module on its website, it seems that a 10μsec (microsecond) HIGH output to the Trig terminal results in eight 40kHz pulses being sent. Then, all that remains is to receive this from the Echo terminal – the actual program becomes what’s shown below.
※Here, we will neglect room temperature for determining the speed of sound, and simply use 340m/s.

The part highlighted in red is a key part for using the ultrasonic sensor module.
First, we output an ultrasound wave for 10 microseconds using the following three lines.

digitalWrite( 2, HIGH ); //Send ultrasonic wave

delayMicroseconds( 10 ); //

digitalWrite( 2, LOW );

As written in the specifications, this will send out 8 pulses; next, we catch the ultrasonic pulse that we sent using the pulseIn function.

Duration = pulseIn( 3, HIGH ); //Input from sensor

The pulseIn function begins the measurement immediately after it is set to HIGH and returns the time taken for the pulse impinging on the specific pin to be set to LOW=off.
While actually running the program, if you move your hand towards or away from the sensor and check the serial monitor, you will be able to confirm changes in the returned values (Figure 2).

Arduino serial monitor input value
Figure 2 Check input values on the serial monitor

Measuring the Distance More Accurately with the Ultrasonic Sensor

In the above example, we used 340m/s for the speed of sound. But given that the speed of sound is easily affected by temperature, let’s try to measure distance with the ultrasonic sensor more accurately.
※To make proper measurements, it is necessary to consider air pressure and humidity. For the equations, etc., check out the Wikipedia article on the speed of sound.

Arduino ultrasound sensor wiring
Figure 3 The circuit with an added temperature sensor

Arduino with ultrasound sensor and temperature sensor
Picture 4 Setup with an added temperature sensor

Circuit with added temperature sensor
Figure 4 Measurement result using circuit with an added temperature sensor

Arduino ultrasound and temperature sensors
Picture 5 Measuring the distance using a ruler

The environment of the experiment was at 21 degrees Celsius, so the speed of sound was 331.5+0.6×21=344.1 m/s . Compared to the 340 m/s used before, there is a clear difference. Picture 5 shows the result from measuring the distance by placing a ruler. There are small variations due to the object’s angle and shape, and their effect on the reflection of the sound. But the value shown is basically the same as the distance shown by the ruler.

Creating a Simple Distance Meter

Now that we’re beginning to understand how to use the ultrasonic sensor, let’s try to implement it as a simple distance meter. By using the 7-segment LED introduced in a previous article, we can confirm the distance obtained from the ultrasonic sensor on the 7-segment LED. Here, we will use a 7 segment LED that can display up to three digits, the RPR-220 (ROHM).

You can find the datasheet for the ROHM three digit LED numeric display LB-303MA here.

Arduino ultrasound and temperature sensor
Picture 6 Implementation of a simple distance meter

By passing the distance (in units of millimeters) to the view7SegLED () function, given in red, you will be able to display the distance on the 7-segment LED.
Inside the View7SegLED() function, the numbers in each digit are refreshed and displayed every delay(20) cycle.

Objects That Can Be Detected Using the Ultrasonic Sensor

The ultrasonic sensor uses sound, so it cannot accurately detect objects that absorb sound.
Figure 5 shows the result when a cloth is placed in front of the ultrasonic sensor. You can see that the sensor value is only given some of the time, and that it’s not detecting correctly.
When using the sensor in practice, it’s recommended that it is used on objects that reflect sound (plastic, wood, glass, metals etc.).

Arduino measuring distance with ultrasound sensor
Picture 7 Try measuring the distance to a cloth

result when mesuring cloth
Figure 5 Result when a cloth is placed in front of the ultrasonic sensor

Summary

Today, we measured distance using the ultrasonic sensor module. This sensor module already has the circuit required to measure distance, so it can be used easily. Because this module can be used to determine whether an object is in front of it or not, it can play the role of a robot’s eye.
With a little ingenuity, it may be possible to discover other applications for this sensor. It’s worth all sorts of trial and error to find out!