Ultrasonic Sensor With Micropython Using BharatPi Board
by bharatpi in Circuits > Electronics
448 Views, 1 Favorites, 0 Comments
Ultrasonic Sensor With Micropython Using BharatPi Board
In the world of electronics and robotics, Bharath Pi has emerged as a popular choice for hobbyists and enthusiasts. One of the key reasons for its widespread adoption is the ease of integration with various sensors. Among these, the ultrasonic sensor stands out for its versatility and applications in distance measurement, obstacle avoidance, robotics, and more.
In this blog, you’ll learn how to use the HC-SR04 Ultrasonic Sensor with the ESP32 to get the distance to an object using Micropython firmware. This tutorial covers how to wire the sensor to the ESP32 boards and provides a simple Micropython script to get the distance to an object and display it on an lcd display.
The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an object. This sensor reads from 2cm to 400cm , which is good for most hobbyist projects. In addition, this particular module comes with ultrasonic transmitter and receiver modules.
Supplies
Required Components:
To complete this tutorial you need the following parts:
1. HC-SR04 Ultrasonic Sensor
2. ESP32
3. Jumper Wires
4. LCD Sensor
5. USB Cable
To follow this instruction you need an IDE to write and upload the code to your board, we are using uPyCraft IDE here. . You also need Micropython firmware installed in your ESP32 boards.
if you don't have the setup means then you can check the README file of installing and setup of uPyCraft and Thonny IDE in our Bharat Pi github page you can find the link below,
> https://github.com/Bharat-Pi/MicroPython/blob/main/README.md
Technical Specifications
In this project, we are using Bharat Pi which has both ESP32 microcontroller and a SimCom A7672S 4G/LTE module. Bharat Pi is an IoT prototyping board for students, innovators, startups, and developers. A simplified board with an all-in-one compute, network and storage on a single board to build rapid prototypes.
How Does the HC-SR04 Ultrasonic Sensor Work
The ultrasonic sensor uses sonar to determine the distance to an object. The ultrasound transmitter (trig pin) emits a high-frequency sound (40 kHz). The sound travels through the air. If it finds an object, it bounces back to the module.The ultrasound receiver (echo pin) receives the reflected sound (echo).
Here’s the pinout of the HC-SR04 Ultrasonic Sensor :
VCC
Trig
Echo
GND
HC-SR04 Ultrasonic Sensor Technical Data
The following table shows the key features of the HC-SR04 ultrasonic sensor. For more information, you should consult the sensor’s datasheet.
Power Supply − +5V DC
Quiescent Current − <2mA
Working Current − 15mA
Effectual Angle − <15°
Ranging Distance − 2cm – 400 cm/1″ – 13ft
Resolution − 0.3 cm
Measuring Angle − 30 degree
Schematic – ESP32 With HC-SR04 Ultrasonic Sensor
HC-SR04 Ultrasonic Sensor Pinout
VCC - Power Supply
Trig - Trigger Input Pin
Echo - Echo Output Pin
GND - GND
Wire the HC-SR04 ultrasonic sensor to the ESP32 as shown in the following schematic diagram. We’re connecting the Trig pin to GPIO 33 and the Echo pin to GPIO 32, but you can use any other suitable pins.
ESP32 Wiring With Ultrasonic Sensor
Ultrasonic Sensor ESP32
VCC - 5V / 3V
Trig - GPIO 33
Echo - GPIO 32
GND - GND
ESP32 Wiring With LCD Sensor
LCD Sensor ESP32
SDA and SCL - SDA and SCL OF ESP32
VCC - 5V
GND - GND
HC-SR04 MicroPython Library
Upload HC-SR04 library with uPyCraft IDE
This section shows how to upload a library using uPyCraft IDE. there are multiple ways to get the distance to an object using the HC-SR04 and the ESP32/ESP8266 boards using MicroPython firmware. We’ll use this HC-SR04 MicroPython Library that makes it straightforward to interface the sensor and get measurements.
The library we’ll use isn’t part of the standard MicroPython library by default. So, you need to upload the following library to your ESP32/ESP8266 board and save it with the name (hcsr04.py).
Click on the given link for the HC-SR04 library on Bharat-Pi github page :
> https://github.com/Bharat-Pi/MicroPython/blob/main/Bharat_Pi_HCSR04_Ultrasonic_Sensor/hcsr04.py
First, make sure you have a connection between the IDE and your board. Go to Tools > Serial and select the COM port. Go to Tools > Board and select the esp32 board.
1. Create a new file by clicking the New File button.
2. Copy the HC-SR04 library code into that file.
3. After copying the code, save this new file as hcsr04.py by pressing the Save button.
5. Click the Download and Run button.
After this, the file should be on the device folder with the name hcsr04.py.Now, you can use the library functionalities in your code by importing the library in main code.
Upload lcd library with uPyCraft IDE :
You can find the lcd libraries on Bharat-Pi github page :
> https://github.com/Bharat-Pi/MicroPython/tree/main/Bharat_Pi_LCD_Display
Code (HC-SR04 Ultrasonic Sensor)
After uploading the library to the ESP32 copy the following code to the main.py and import the library to the main code. It simply prints the distance to the closest object.
OR
If you are using Thonny IDE no need to upload the any library , you can just upload the below code and you will get the distance measurements.
CODE :
from machine import Pin, time_pulse_us
import time
# Define pins
TRIG_PIN = 33 # GPIO pin for the trigger
ECHO_PIN = 32 # GPIO pin for the echo
# Set up pins
TRIG = Pin(TRIG_PIN, Pin.OUT)
ECHO = Pin(ECHO_PIN, Pin.IN)
def measure_distance():
# Send a 10µs pulse on the trigger pin
TRIG.value(1)
time.sleep_us(10)
TRIG.value(0)
# Measure pulse duration on echo pin
pulse_duration = time_pulse_us(ECHO, 1)
# Calculate distance (divide by 58 to convert microseconds to centimeters)
distance_cm = pulse_duration / 58.0
return distance_cm
try:
while True:
distance = measure_distance()
print("Distance:", distance, "cm")
time.sleep(1)
except KeyboardInterrupt:
pass
Demonstration
After uploading the code to your board, press the reset button to run the code. The distance to the closest object should be printed on the console.
If your are not able to see the values in lcd means may be i.e due to lcd display setting you can check the lcd blog trouble shooting part at the end of the blog.
Click on the below link for the blog :
> https://www.instructables.com/LCD-Sensor-With-Micropython-Using-Bharart-Pi-Board/