The objective of this DIY speed radar project is to measure the speed of a moving object such as a car, a person, or a small robot. It uses simple and affordable components to reproduce the basic idea of real speed radars. The system calculates speed and displays it in real time, while also providing an alert if a predefined speed limit is exceeded. This makes it a useful educational project for understanding motion, timing, and sensor-based measurement systems.
The functioning of the system is based on the principle that speed equals distance divided by time. Two HC-SR04 ultrasonic sensor sensors are placed a fixed distance apart along the path of the moving object. When the object passes in front of the first sensor, it detects the object and sends a signal to the ESP8266 NodeMCU, which records the starting time. As the object continues moving, it eventually passes in front of the second sensor, which triggers another signal and allows the NodeMCU to record the ending time.
The system then calculates the time difference between the two detections. Since the distance between the sensors is known in advance, the NodeMCU computes the speed using the formula speed = distance / time. The result can be converted into more familiar units such as kilometers per hour. This simple method provides a reliable estimation of the object’s speed as long as the sensors are properly aligned and the distance between them is accurately measured.
The calculated speed is displayed on an LCD I2C display, allowing the user to see the result instantly. In addition, a buzzer electronic component is used as an alert system. If the measured speed exceeds a predefined threshold, the buzzer emits a sound to warn that the speed limit has been surpassed. This feature simulates real-world speed monitoring systems and adds an interactive aspect to the project.
1. ESP8266 NodeMCU board

The ESP8266 NodeMCU is the central controller of the project. It is a microcontroller board with built-in Wi-Fi capability that processes all the data from the sensors. It measures the time between detections, calculates the speed, controls the display, and activates the buzzer when necessary. Its compact size and ease of programming make it ideal for this type of embedded system project.
2. HC-SR04 sensor

The two HC-SR04 ultrasonic sensor modules are used to detect the presence of a moving object. Each sensor emits ultrasonic waves and measures the time it takes for the echo to return after bouncing off an object. In this project, instead of measuring distance continuously, they act as detection points. When an object passes in front of each sensor, it triggers a timing event that allows the system to calculate speed.
3- Buzzer :

The buzzer is used to notify the user through sound based on the measured speed. When the calculated speed exceeds a predefined limit, the buzzer turns on to warn that the object is moving too fast.
4. LCD Display with I2C Module

The I2C LCD screen is used to display real-time information such as the measured speed, system status, and warning messages.
5. Breadboard

A breadboard is used to assemble the circuit without soldering. It makes it easy to connect and modify the components during testing and development.
6. Jumper Wires

Jumper wires are used to connect the ESP8266 board, HC-SR04 sensors, and LCD display together. They ensure proper electrical connections between all components.


1- Connecting the first HC-SR04 sensor to the ESP8266 board
| HC-SR04 | ESP8266 board |
|---|---|
| VCC | 3 V |
| Trig | D5 |
| Echo | D6 |
| GND | GND |
2- Connecting the second HC-SR04 sensor to the ESP8266 board
| HC-SR04 | ESP8266 board |
|---|---|
| VCC | 3 V |
| Trig | D7 |
| Echo | D8 |
| GND | GND |
3- Connecting the Buzzer to the ESP8266 board
| Buzzer | ESP8266 board |
|---|---|
| (+) Terminal | D3 |
| (-) Terminal | GND |
4- Connection of LCD I2C display to ESP8266 board
| LCD I2C display | ESP8266 board |
|---|---|
| VCC | 5V of GPIO card |
| GND | GND |
| SDA | D2 |
| SCL | D1 |
This Micropython program implements a DIY speed radar system using an ESP8266 microcontroller, two HC-SR04 ultrasonic sensors, an I2C LCD display, and a buzzer. The main purpose of the system is to measure the speed of a moving object by calculating the time it takes to travel between two fixed points.
You need to install this libraries : i2c_lcd et lcd_api for I2C LCD screen
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
from machine import Pin, I2C, time_pulse_us import time from i2c_lcd import I2cLcd # ========================= # CONFIGURATION # ========================= DISTANCE = 0.20 # Distance between the two sensors (in meters) THRESHOLD = 10 # Detection threshold (in cm) SPEED_LIMIT = 1 # Speed limit (in km/h) # Sensor 1 (start detection) trig1 = Pin(14, Pin.OUT) # Trigger pin (D5) echo1 = Pin(12, Pin.IN) # Echo pin (D6) # Sensor 2 (end detection) trig2 = Pin(13, Pin.OUT) # Trigger pin (D7) echo2 = Pin(16, Pin.IN) # Echo pin (D0) # Buzzer for alert buzzer = Pin(0, Pin.OUT) # Buzzer pin (D3) # LCD I2C configuration i2c = I2C(scl=Pin(5), sda=Pin(4), freq=400000) lcd = I2cLcd(i2c, 0x27, 4, 20) # 20x4 LCD # ========================= # FUNCTIONS # ========================= def measure_distance(trig, echo): """ Measure distance using HC-SR04 sensor Returns distance in centimeters """ trig.value(0) time.sleep_us(2) # Send 10µs pulse to trigger trig.value(1) time.sleep_us(10) trig.value(0) # Measure echo pulse duration duration = time_pulse_us(echo, 1, 30000) # If timeout or error if duration < 0: return None # Convert time to distance (cm) return (duration * 0.0343) / 2 def object_detected(trig, echo): """ Check if an object is detected within threshold distance """ d = measure_distance(trig, echo) return d is not None and d < THRESHOLD def display(l1="", l2="", l3="", l4=""): """ Display 4 lines on the LCD """ lcd.clear() lcd.move_to(0, 0) lcd.putstr(l1) lcd.move_to(0, 1) lcd.putstr(l2) lcd.move_to(0, 2) lcd.putstr(l3) lcd.move_to(0, 3) lcd.putstr(l4) def beep(ms=200): """ Activate buzzer for a given duration (milliseconds) """ buzzer.value(1) time.sleep_ms(ms) buzzer.value(0) # ========================= # INITIALIZATION # ========================= # Display startup message display("ESP8266 RADAR", "Initializing...", "", "") time.sleep(2) # ========================= # MAIN LOOP # ========================= while True: # System ready message display("Radar active", "Waiting object...", "", "") # Wait until object is detected by sensor 1 while not object_detected(trig1, echo1): time.sleep_ms(5) # Record start time t1 = time.ticks_us() display("Object detected", "Measuring...", "", "") # Wait until object is detected by sensor 2 while not object_detected(trig2, echo2): time.sleep_ms(5) # Record end time t2 = time.ticks_us() # Calculate time difference (in seconds) delta_t = time.ticks_diff(t2, t1) / 1_000_000 if delta_t > 0: # Calculate speed in km/h speed = (DISTANCE / delta_t) * 3.6 # Display speed and limit display( "Speed:", "{:.2f} km/h".format(speed), "Limit: {} km/h".format(SPEED_LIMIT), "Status:" ) if speed > SPEED_LIMIT: lcd.move_to(8, 3) lcd.putstr("OVER") # Speed exceeded # Activate alarm (multiple beeps) for _ in range(15): beep(150) time.sleep_ms(100) else: lcd.move_to(8, 3) lcd.putstr("OK") # Speed within limit beep(80) else: # Error if time is invalid display("Error", "Invalid time", "", "") # Wait before next measurement time.sleep(5) |
At the beginning, the program defines key parameters such as the distance between the two sensors, the detection threshold, and the maximum allowed speed. It also configures all the hardware components: the two ultrasonic sensors (each with a trigger and echo pin), the buzzer for alerts, and the LCD screen for displaying information. When the system starts, it shows an initialization message on the LCD to indicate that the radar is ready.
The program includes several functions to organize its operation. One function measures the distance using an ultrasonic sensor by sending a pulse and calculating how long it takes for the echo to return. Another function checks whether an object is detected based on a distance threshold. Additional functions manage the LCD display and control the buzzer sound.
In the main loop, the system continuously waits for an object to pass in front of the first sensor. When this happens, it records the exact time. Then it waits for the same object to reach the second sensor and records a second timestamp. Using these two times, the program calculates the time difference, which represents how long the object took to travel between the sensors.
Once the time is known, the program calculates the speed using the formula speed = distance ÷ time and converts it into kilometers per hour. The result is displayed on the LCD screen along with the predefined speed limit. If the measured speed exceeds this limit, the system displays a warning message (“OVER”) and activates the buzzer repeatedly as an alarm. If the speed is within the limit, it displays “OK” and emits a short beep.
Finally, the program pauses briefly before restarting the process, allowing continuous monitoring of moving objects. Overall, this program demonstrates how to combine sensors, timing functions, and output devices to build a simple yet effective speed measurement system.
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.
Zaouiet Kontech-Jemmel-Monastir-Tunisia
Robotic site created by Mohamed Ali Haj Salah - Teacher info