Introduction: Getting Started With Ultrasonic Module and Arduino
In this tutorial, you’ll learn about the ultrasonic module and find out how it works. In the end, you’ll be able to connect it to the Arduino board and use it as a motion detector to control your monitor with hand gestures.
What You Will Learn
- What ultrasonic module is and how it works
- How to use ultrasonic module with Arduino
- Controlling a monitor using the ultrasonic module
Step 1: General Information About Ultrasonic Module
The ultrasonic sensor is a proximity sensor. This sensor calculates the time and direction of ultrasound in the air by sending a short audio pulse at a frequency higher than the human hearing range and receiving it after reflection of the surface of the object. Using this time, the distance is measured at high resolution. The ultrasonic method is one of the best methods for measuring distance and detecting objects due to the high accuracy and speed, the lack of contact with the object, no corrosion and proper price. Most of the ultrasonic modules include a Transmitter and a receiver. The receiver and transmitter of the SRF04 and SRF05 modules are adjacent to each other and can detect objects in the range of 2 to 300 cm accurately. The SRF05 module is an improved version of SRF04. This version supports dual-mode and includes 5 pins to work with. But the SRF04 module has 4 pins and supports only one mode. Both of these modules have a common mode that is known as mode1.
In mode 1 first, you should apply a pulse for at least 10 microseconds to the trig pin. The module will automatically send 8 pulses at the frequency 40 kHz and set the Echo pin to 1. The Echo pin remains high until the module receives the reflected pulses from an object. Then we can calculate the time between sending and receiving the signal by measuring the time that the Echo pin is at a high logic level. In this mode leave the SRF05 OUT pin without connection.
Mode 2, which is only available in the SRF05 module, uses a single pin for both Trigger and Echo signals. To use this mode, connect the OUT pin to the ground and use the Echo pin to send and receive the Echo pulse. We can calculate the distance from the first object by measuring the signal duration, as mentioned before.
Step 2: Required Materials
Step 3: Ultrasonic Module and Arduino Interfacing
The connection between the ultrasonic module and Arduino is very simple. Just connect the Trig and Echo pins to two pins of Arduino and introduce these two pins to Arduino. Using these pins, Arduino calculates the distance from the first object in front of the module, as explained before.
Also, do not forget to connect the VCC and the GND of the module to 5V and the GND of Arduino.
Step 4: Circuit
Step 5: Code
Let’s take a closer look to the code:
sending a 10 microsecond pulse on the trig pin.
duration = pulseIn(echoPin, HIGH);
the pulseIn() function calculates the time that the Echo pin is high, which is actually equal to the time between the signal being transmitted and received.
By multiplying the time duration and the speed of sound, you can calculate the distance traveled by the sound.
Step 6: Controlling Your Monitor With Arduino and Ultrasonic Module
Suppose a video is playing on your screen. We want to control the video to play or stop, its volume and moving it forward and backward just by using hand gestures.
To do this connect two ultrasonic modules to an Arduino Leonardo board, and place the modules in the right and the left above your monitor.
Upload the following code on your board. Now you can stop or play the video by placing your both hands in 20 to 30 centimeters distance from the modules!
If you place your right hand at the distance of 9 to 15cm in front of the right module and move it away slightly from it, the video goes back 5 seconds. And if you close your hand slightly to the module, the video goes 5 seconds forward.
Don’t worry about the volume! By placing your left hand at a distance of 9 to 15cm from the module and closing it to the module, the volume of the video will be increased and by moving your hand away it will be decreased.
Step 7: Circuit
Step 8: Code
You need the Keyboard.h library for this part. You can download it from the following link:
Step 9: What’s Next?
- Try to calculate the distance from every object around the ultrasonic module by adding a servo motor to the module.
- Try to define other gestures for new options like changing the speed of playing the video.