Page 1
Welcome! Thank you for purchasing our AZ-Delivery HC-SR04 Ultrasonic Sensor Module. On the following pages, you will be introduced to how to use and set up this handy device. Have fun!
Page 2
Areas of application Education and teaching: Use in schools, universities and training institutions to teach the basics of electronics, programming and embedded systems. Research and development: Use in research and development projects to create prototypes and experiments in the fields of electronics and computer science. Prototype development: Use in the development and testing of new electronic circuits and devices.
Page 3
consult a doctor. Caution: Keep the product out of the reach of children and pets to avoid accidental contact and swallowing of small parts. Note: Store the product in a safe, closed container when not in use. Attention: Avoid contact of the product with food and drinks.
Table of Contents Introduction....................3 Calculating the distance................4 Specifications....................6 The pinout.....................7 How to set-up Arduino IDE................8 How to set-up the Raspberry Pi and Python..........12 Connecting the module with Atmega328P..........13 Sketch example..................14 Connecting the module with Raspberry Pi..........18 Libraries and tools for Python..............20 Python script....................21 - 2 -...
Introduction The HC-SR04 ultrasonic sensor module is a device that can measure distance using ultrasonic sound waves. It can determine the distance between the module and other objects in vicinity with high accuracy. The sensor is commonly used in obstacle avoiding devices, various distance measuring devices, automation projects, car parking systems, proximity alarms, etc.
Calculating the distance The module emits ultrasonic sound waves on a frequency that is higher than the human hearing range (more than 20kHz). The speed of the sound waves traveling through the air is approximately 343m/s at room temperature (20°C). This speed value depends on the environmental circumstances like temperature, humidity and variations in air pressure.
Page 7
Because the sound waves travel 343 meters per one second, to measure distances from 20mm up to 4000mm (the range of the module) the measurements are done in microseconds. So, the speed of the ultrasonic sound waves should be converted from the m/s into cm/µs, which is: 343m/s = 0.0343 cm/µs = 1/29 cm/µs...
Specifications Power supply voltage: up to 5V Operating voltage: from 3V to 5V Output voltage: Current consumption: 15mA Quiescent current: less than 2mA Ultrasonic frequency: 40kHz Trigger input signal: 10µS TTL pulse Measuring angle: 30 degree Effectual angle: less than 15 degrees Operating distance range: from 20mm to 4000mm (1in to 13 feet) Claimed precision:...
The pinout The HC-SR04 module has four pins. The pinout is shown on the following image: Note: The output voltage of the module is in the 5V range. In order to use the module with the Raspberry Pi, the device called logic level converter should be used.
How to set-up Arduino IDE If the Arduino IDE is not installed, follow the link and download the installation file for the operating system of choice. For Windows users, double click on the downloaded .exe file and follow the instructions in the installation window. - 8 -...
Page 11
For Linux users, download a file with the extension .tar.xz, which has to be extracted. When it is extracted, go to the extracted directory and open the terminal in that directory. Two .sh scripts have to be executed, the first called arduino-linux-setup.sh and the second called install.sh.
Page 12
Almost all operating systems come with a text editor preinstalled (for example, Windows comes with Notepad, Linux Ubuntu comes with Gedit, Linux Raspbian comes with Leafpad, etc.). All of these text editors are perfectly fine for the purpose of the eBook. Next thing is to check if your PC can detect an Atmega328P board.
Page 13
If the Arduino IDE is used on Windows, port names are as follows: For Linux users, for example port name is /dev/ttyUSBx, where x represents integer number between 0 and 9. - 11 -...
How to set-up the Raspberry Pi and Python For the Raspberry Pi, first the operating system has to be installed, then everything has to be set-up so that it can be used in the Headless mode. The Headless mode enables remote connection to the Raspberry Pi, without the need for a PC screen Monitor, mouse or keyboard.
Connecting the module with Atmega328P Connect the module with the Atmega328P as shown on the following image: Module pin Mc pin Wire color Red wire TRIG Blue wire ECHO Green wire Black wire - 13 -...
Sketch example #define TRIG_PIN 2 #define ECHO_PIN 3 long duration, cm, inches; void setup() { Serial.begin(9600); pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); void loop() { digitalWrite(TRIG_PIN, LOW); delayMicroseconds(5); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); duration = pulseIn(ECHO_PIN, HIGH); cm = (duration / 2) / 29.1; // Divide by 29.1 or multiply by 0.0343 inches = (duration / 2) / 74;...
Page 17
Upload the sketch to the Atmega328P and run the Serial Monitor (Tools > Serial Monitor). The result should look like as on the following image: - 15 -...
Page 18
The sketch starts with creating two macros TRIG_PIN and ECHO_PIN. These macros represent the digital pins of Atmega328P to which pins of the module are connected. Next, three long variables are created duration, cm and inches. In the duration variable the time interval of the measurement is saved. In the cm variable the calculated distance in the centimeters is stored.
Page 19
The second argument of the pulseln() function can have two values: HIGH or LOW. When the value of the second argument is HIGH, the function waits for the signal on the ECHO_PIN to change its state from LOW to HIGH to start the measurement. If the value of the second argument is LOW, then the function waits for the signal on the ECHO_PIN to change its state from HIGH to LOW to start the measurement.
Connecting the module with Raspberry Pi Connect the module with the Raspberry Pi as shown on the following image: - 18 -...
Page 21
Module pin Logic Level Converter (LLC) pin Wire color Red wire TRIG Blue wire ECHO Green wire Black wire LLC pin Raspberry Pi pin Physical pin Wire color Red wire Orange wire GPIO23 Blue wire GPIO24 Green wire Black wire 3V3 via 10kΩ...
Libraries and tools for Python To use the module with the Raspberry Pi, the library RPi.GPIO has to be installed. If the library is not installed, open the terminal and run the following commands, one by one: sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install python3-rpi.gpio The example of the original script made by Matt Hawkins can be found on the following link.
Page 24
GPIO.setmode(GPIO.BCM) TRIG_PIN ECHO_PIN GPIO.setup(TRIG_PIN, GPIO.OUT) GPIO.setup(ECHO_PIN, GPIO.IN) print('[Press Ctrl + C to end program!]') try: GPIO.output(TRIG_PIN, False) time.sleep(0.5) while True: distance measure_average() print('Distance: {:5.1f}cm'.format(distance)) time.sleep(1) except KeyboardInterrupt: print('\nScript end!') finally: GPIO.cleanup() - 22 -...
Page 25
Save the script by the name ultrasonic.py. To run the script, open the terminal in the directory where the script is saved and run the following command: python3 ultrasonic.py The result should look like as on the following image: To stop the script press ‘CTRL + C’ on the keyboard. - 23 -...
Page 26
The script starts with importing two libraries: time and RPi.GPIO. Next, two functions are created: measure() and measure_average(). Both functions have no arguments and return the double value. The measure() function is used to do the distance measurement and calculation. The algorithm for measuring and calculating the distance is in this function.
Page 27
Where “5.1f”: “5” means the output contains 5 decimal places; “.1” means that there is one digit after the decimal point; and “f” means that the value is of type float. To stop the script press ‘CTRL + C’ on the keyboard. This is called the keyboard interrupt.
Page 28
Internet. If you are looking for the high quality microelectronics and accessories, AZ-Delivery Vertriebs GmbH is the right company to get them from. You will be provided with numerous application examples, full installation guides, eBooks, libraries and assistance from our technical experts.
Need help?
Do you have a question about the HC-SR04 and is the answer not in the manual?
Questions and answers