logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

Using Arduino with Parts and Sensors – Ultrasonic Sensor

Device Plus Editorial Team
Published by Device Plus Editorial Team at January 26, 2016
Categories
  • Arduino
Tags
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//********************************************************************
//*Program for displaying distance using the ultrasonic sensor
//********************************************************************
int Duration = 0; //Time to receive signal
double Distance = 0; //Distance
void setup() {
Serial.begin( 9600 );
pinMode( 2, OUTPUT );
pinMode( 3, INPUT );
}
void loop() {
digitalWrite( 2, HIGH ); //Send ultrasound wave
delayMicroseconds( 10 ); //
digitalWrite( 2, LOW );
Duration = pulseIn( 3, HIGH ); //Input from sensor
if (Duration > 0) {
Duration = Duration/2; //Halve round-trip distance
Distance = Duration*340*100/1000000; //Set speed of sound to be 340m/s
Serial.print("Distance:");
Serial.print(Distance);
Serial.println(" cm");
}
delay(500);
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//*******************************************************************************
//*Program to display distance using an ultrasonic sensor and a temperature sensor
//*******************************************************************************
int Duration = 0; //Time to receive signal
double Distance = 0; //Distance
void setup() {
Serial.begin( 9600 );
pinMode( 2, OUTPUT );
pinMode( 3, INPUT );
pinMode( 4, OUTPUT );
pinMode( 5, INPUT );
}
void loop() {
int ans , temp , tv ; //Variable for measuring temperature
ans = analogRead(0) ; //Read in sensor value from Analog pin 0
tv = map(ans,0,1023,0,5000) ; //Convert sensor value to voltage
temp = map(tv,300,1600,-30,100) ; //Convert voltage to temperature (The LM61 measures from -30 to 100 degrees Celsius)
Serial.print("temp:");
Serial.print(temp); //Display temperature
Serial.print("c");
digitalWrite( 2, HIGH ); //Send ultrasonic pulse
delayMicroseconds( 10 ); //
digitalWrite( 2, LOW );
Duration = pulseIn( 3, HIGH,5000 );
if (Duration > 0) {
Distance = Duration/2;
float sspeed = 331.5+0.6*temp;
Serial.print("tspeed:");
Serial.print(sspeed);
Serial.print("m/sec");
Distance = Distance*sspeed*100/1000000; //
Serial.print("tDistance:");
Serial.print(Distance);
Serial.print("cm");
}
Serial.println("");
delay(500);
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//*****************************************
//*Program for simple distance meter
//*****************************************
int Duration = 0; //Time to receive signal
double Distance = 0; //Distance
void setup() {
Serial.begin( 9600 );
//For the ultrasonic sensor
pinMode( 2, OUTPUT );
pinMode( 3, INPUT );
//For the 7-segment LED
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
//Define LED layout
boolean Num_Array[10][7]={
{1,1,1,1,1,1,0}, //0
{0,1,1,0,0,0,0}, //1
{1,1,0,1,1,0,1}, //2
{1,1,1,1,0,0,1}, //3
{0,1,1,0,0,1,1}, //4
{1,0,1,1,0,1,1}, //5
{1,0,1,1,1,1,1}, //6
{1,1,1,0,0,1,0}, //7
{1,1,1,1,1,1,1}, //8
{1,1,1,1,0,1,1} //9
};
//Define function for LED display
void NumPrint(int Number){
Number = Number - 48;
Serial.println(Number);
for (int w=0; w<=6; w++){
digitalWrite(w+4,-Num_Array[Number][w]);
}
}
//Set all LEDs to not display
void off7SegLED(){
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
}
void on7SegLED(){
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
void view7SegLED(int n){
String str = String(n);
//Fill in 0 when 2 digits
if(100 > n){
str = "0"+str;
}
//Fill in 00 when only 1 digit
if(10 > n){
str = "00"+str;
}
on7SegLED();
digitalWrite(12,LOW);
NumPrint(str.charAt(0));
delay(20);
on7SegLED();
digitalWrite(13,LOW);
NumPrint(int(str.charAt(1)));
delay(20);
on7SegLED();
digitalWrite(11,LOW);
NumPrint(int(str.charAt(2)));
delay(20);
on7SegLED();
}
void loop() {
int ans , temp , tv ; //Variable for temperature measurement
ans = analogRead(0) ; //Read in sensor value from Analog pin 0
tv = map(ans,0,1023,0,5000) ; //Convert sensor value to voltage
temp = map(tv,300,1600,-30,100) ; //Convert voltage to temperature (The LM61 measures from -30 to 100 degrees Celsius)
Serial.print("temp:");
Serial.print(temp); //Display temperature
Serial.print("c");
Distance = 0;
digitalWrite( 2, HIGH ); //Send ultrasonic pulse
delayMicroseconds( 10 ); //
digitalWrite( 2, LOW );
analogWrite(9,0);
Duration = pulseIn( 3, HIGH,5000 );
if (Duration > 0) {
Distance = Duration/2;
float sspeed = 331.5+0.6*temp;
Serial.print("tspeed:");
Serial.print(sspeed);
Serial.print("m/sec");
Distance = Distance*sspeed*100/1000000; //
Serial.print("tDistance:");
Serial.print(Distance);
Serial.print("cm");
int val = 1023 - Distance*50;
analogWrite(9,val);
}
//Display to 7-segment LED in millimeters
view7SegLED(Distance*10);
Serial.println("");
// delay(500);
}

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!

Device Plus Editorial Team
Device Plus Editorial Team
Device Plus is for everyone who loves electronics and mechatronics.

Check us out on Social Media

  • Facebook
  • X

Recommended Posts

  • Using Arduino with Parts and Sensors – Speakers Part 1Using Arduino with Parts and Sensors – Speakers Part 1
  • Using Arduino with Parts and Sensors – Speakers Part 2Using Arduino with Parts and Sensors – Speakers Part 2
  • Using ESP-WROOM-02 Wifi Module As Arduino MCUUsing ESP-WROOM-02 Wifi Module As Arduino MCU
  • Using Sensors with the Arduino: Create a Simple Device to Detect Movement and ReactUsing Sensors with the Arduino: Create a Simple Device to Detect Movement and React
  • Using Arduino with Parts and Sensors – Infrared Remote Control (Last Part)Using Arduino with Parts and Sensors – Infrared Remote Control (Last Part)
  • Using Arduino with Parts and Sensors – Accelerometer Part 1Using Arduino with Parts and Sensors – Accelerometer Part 1
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • X
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort