
In this project we will realize an obstacle detection system with Arduino. This system mainly uses an ultrasonic sensor, buzzer and LED.
When the ultrasonic sensor detects an obstacle at a distance <4cm, the ESP32 board gives the order for the buzzer to sound.
Connecting wires
Test plate
To perform the assembly, you can connect
For buzzer:
the terminals (+) to pin D4 of the ESP32 board
the terminals (-) to the GND pin of the ESP32 board
For HC-SR04 sensor:
the TRIG pin to pin D5 of the ESP32 board
the Echo pin to pin D18 of the ESP32 board
the VCC pin to the VCC pin of the ESP32 board
the GND pin to the GND pin of the ESP32 board
For display:
the SCL pin to pin D22 of the ESP32 board
the VCC pin to the 3.3V pin of the ESP32 board
the GND pin to the GND pin of the ESP32 board
the SDA pin the D21 pin of the ESP32 card
Here is the program of the sound alarm system:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from hcsr04 import HCSR04 from machine import Pin,I2C import ssd1306,time i2c = I2C(scl=Pin(22), sda=Pin(21)) #Init i2c oled=ssd1306.SSD1306_I2C(128,64,i2c,0x3c) buzzer=Pin(4,Pin.OUT) sensor = HCSR04(trigger_pin=5,echo_pin=18,echo_timeout_us=1000000) while True: distance = sensor.distance_cm() print(distance,' cm') time.sleep_ms(100) oled.fill(0) oled.text("Distance:",30,20) oled.text(str(distance),30,40) oled.text("cm",30,50) oled.show() #display the distance between the sensor and the object if (distance<4): buzzer.value(1) # buzzer sounds else: buzzer.value(0) #buzzer stops ringing |
Note: you must import the following two libraries: hcsr04 and ssd1306