Use of HC-SR04 ultrasonic sensor and I2C LCD screen by ESP32

Robotique Site
Robotique Site
412 بار بازدید - 6 ماه پیش - For more details you can
For more details you can read this tutorial :
https://www.robotique.site/tutorial/u...

The components required to use HC-SR04 ultrasonic sensor and I2C LCD screen by ESP32 board
ESP32 board:
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
The HC-SR04 is an ultrasonic distance measuring sensor module.
I2C LCD Display (usually 16x2)
This display is used for displaying distance between the HC-SR04 sensor and an object.
Jumper Wires:
For making temporary connections and wiring between components.
Breadboard:
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 and I2C LCD screen
Attaching the I2C LCD Display :
- connect the VCC pin of the display to 5V pin of the ESP32
- connect the GND pin of the display to GND pin of the ESP32
- connect the SDA pin of the display to GPIO21 pin of the ESP32
- connect the SCL pin of the display to GPIO22 pin of the ESP32
Attaching the HC-SR04 sensor :
- 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.

To program the ESP32 board with MicroPython to calculate and display the distance between the HC-SR04 sensor and an object on an I2C LCD screen, you can follow the steps below.
1- Flash your ESP32 with MicroPython using this file esp32-20210902-v1.17.bin.
2- import this library : hc-sr04 for HC-SR04 sensor
3- import this two libraries : i2c_lcd and lcd_apifor I2C LCD screen
4- Create a new Python script and write the following code:

import machine
from machine import Pin, SoftI2C
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
from hcsr04 import HCSR04
import time

I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
Configure I2C for LCD screen
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000)    
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
Configure HC-SR04 pins
sensor = HCSR04(trigger_pin=16,echo_pin=17,echo_timeout_us=1000000)

while True:
#calculate distance between the HC-SR04 sensor and an object
distance = sensor.distance_cm()
print('distance= ',distance,' cm')
show distance on LCD I2C display
lcd.putstr("distance= ")
lcd.putstr(str(int(distance)))
lcd.putstr(" cm")
time.sleep_ms(2000)
lcd.clear()

This code continuously measures the distance using the HC-SR04 sensor and displays it on the I2C LCD screen.
6 ماه پیش در تاریخ 1402/11/17 منتشر شده است.
412 بـار بازدید شده
... بیشتر