Control the HC-SR04 ultrasonic sensor using the ESP32 card

ESP32 02-02-24
99 0

Tutorial plan

1- What is the HC-SR04 ultrasonic sensor ?

2- The components required to use HC-SR04 ultrasonic sensor by ESP32 board

3- Mounting the ESP32 board with the HC-SR04 ultrasonic sensor

4- Program the ESP32 board with Micropython to determine the distance between the HC-SR04 sensor and an object

 

 

What is the HC-SR04 ultrasonic sensor ?

The HC-SR04 is an ultrasonic distance measuring sensor module. It is commonly used for measuring distances and detecting obstacles in various applications, such as robotics, home automation, and industrial projects. The sensor works on the principle of ultrasonic sound waves.

Here’s a basic overview of how the HC-SR04 sensor works:

Ultrasonic Transmitter: The sensor has an ultrasonic transmitter that emits short pulses of ultrasonic sound waves.

Ultrasonic Receiver: It also has a receiver that listens for the echo of the transmitted ultrasonic waves.

Distance Calculation: The sensor calculates the time taken for the ultrasonic waves to travel to the target object and back. Using the speed of sound in the air, it then calculates the distance to the object.

Output: The sensor provides an output in the form of a pulse width or a voltage signal that corresponds to the measured distance.

The HC-SR04 typically has four pins:

VCC (Power): Connects to the positive power supply (5V or 3.3V).

Trig (Trigger): Sends a short pulse to trigger the sensor to transmit ultrasonic waves.

Echo: Receives the echo signal and outputs a pulse whose width corresponds to the distance.

GND (Ground): Connects to the ground of the circuit.

ESP32 and other microcontrollers are often used to interface with the HC-SR04 sensor, making it a popular choice for hobbyists and developers working on projects involving distance measurement or obstacle avoidance. The sensor’s simplicity, low cost, and ease of use contribute to its widespread popularity in the maker community.

The components required to use HC-SR04 ultrasonic sensor by ESP32 board

ESP32 board:

ESP32 card

The ESP32 is a powerful microcontroller developed by Espressif Systems. It’s renowned for its integrated Wi-Fi and Bluetooth capabilities, making it a popular choice for various IoT (Internet of Things) applications.

HC-SR04 ultrasonic Sensor

HC-SR04

The HC-SR04 is an ultrasonic distance measuring sensor module.

Jumper Wires:

Jumper wires

For making temporary connections and wiring between components.

Breadboard:

Test plate

A breadboard is a useful tool for creating temporary electronic circuits. It allows you to connect components without soldering.

Mounting the ESP32 board with the HC-SR04 ultrasonic sensor

Mounting the ESP32 board with the HC-SR04 ultrasonic sensorMounting the ESP32 board with the HC-SR04 ultrasonic sensor

This represents the connections between the HC-SR04 ultrasonic sensor and the ESP32 board:

– Connect the VCC(+) pin of the HC-SR04 ultrasonic sensor to the 3.3V pin on the ESP32 board.

– Connect the Trig pin of the HC-SR04 ultrasonic sensor to GPIO16 pin on the ESP32 board.

– Connect the Echo pin of the HC-SR04 ultrasonic sensor to GPIO17 pin on the ESP32 board.

– Connect the GND(-) pin of the DHT22 sensor to any ground (GND) pin on the ESP32 board.

Program the ESP32 board with Micropython to determine the distance between the HC-SR04 sensor and an object

To measure the distance using an HC-SR04 sensor with an ESP32 board using MicroPython, you need to install the MicroPython firmware on your ESP32 and then write a Python script to interface with the sensor. Here’s a simple example code :

1- Flash your ESP32 with MicroPython using this file esp32-20210902-v1.17.bin.

2- import this library : hc-sr04

3- Create a new Python script and write the following code:

This program continuously measures the distance between the HC-SR04 and the object sensor and prints the result.

0 comment

Leave a comment

Veuillez noter s'il vous plaît*

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Purpose of this website

Educational robotics refers to the use of robots and robotics technology to promote learning in educational settings. It involves the integration of technology, engineering, and computer science into the classroom, allowing students to engage in hands-on, project-based learning experiences.
In this context, our website represents an excellent resource for parents, teachers and children who wish to discover robotics.

Contact details

Zaouiet Kontech-Jemmel-Monastir-Tunisia

+216 92 886 231

medaliprof@gmail.com

Pictures of the articles

Robotic site created by MedAli-Teacher info