
This project involves designing and building a smart four-wheel robot car controlled remotely using two ESP32 development boards communicating via Bluetooth.
The system uses a joystick module connected to the first ESP32 board (transmitter), allowing the user to control the robot's movements intuitively.
The joystick movements are processed by the transmitter ESP32, which sends control commands wirelessly via Bluetooth to the second ESP32 board (receiver) mounted on the robot car.
The receiver ESP32 interprets the received commands and controls the four DC motors through an L298N motor driver module. This allows the robot car to move forward, backward, turn left, turn right, and stop.
The Bluetooth communication eliminates the need for a physical connection between the joystick controller and the robot car, providing convenient wireless control.
This project demonstrates practical applications of embedded systems, wireless communication, and robotics. It helps students understand how two ESP32 boards can exchange data via Bluetooth, how joystick signals can be processed, and how a motor driver controls DC motors to achieve robot movement.

The system uses two ESP32 development boards. The first ESP32 acts as a transmitter, receiving analog signals from the joystick module and processing them to determine the desired movement of the robot car. It then sends control commands via Bluetooth to the second ESP32, which acts as a receiver. The receiver processes these commands and controls the motors through the L298N motor driver.

The L298N is a dual H-bridge motor driver used to control the four DC motors of the robot car. It receives control signals from the receiver ESP32 and regulates the direction and speed of the motors. Its two output channels allow the motors on the left side and right side of the car to be controlled independently.

The joystick module is used to control the direction and movement of the robot car. It contains two potentiometers that detect movements along the X and Y axes, as well as a push button. It sends analog signals to the transmitter ESP32, allowing the user to move the robot forward, backward, left, or right.

The 5V/3.3V power supply module provides the appropriate regulated voltage to power the receiver ESP32 board and its associated electronic components

The four-wheeled car chassis provides the mechanical structure of the robot. It consists of four DC motors, four wheels, and a supporting frame. The motors allow the car to move forward, backward, turn left, and turn right.

Connecting wires are used to establish electrical connections between the ESP32 boards, joystick, L298N motor driver, motors, and power supply. They ensure the transmission of electrical signals and power throughout the system.

The two 3.7V rechargeable batteries provide the electrical power required to operate the robotic car. They supply energy to the L298N Motor Driver Module and the four DC motors, allowing the car to move autonomously according to the commands received from the sender ESP32.

The breadboard is used to connect and assemble electronic components without soldering. It facilitates the connection of the receiver ESP32 board to the 5V/3.3V power supply module and other electronic components. It allows students to build, test, and modify electronic circuits easily.

The 9V battery serves as the electrical power source for the system. It supplies power to the 3.3V/5V power supply module, which converts the 9V input voltage into two regulated output voltages: 5V and 3.3V. These voltages are used to power the various electronic components of the system.

The joystick module is connected to the first ESP32 board to allow the user to control the movements of the robot car.
| Joystick Module | ESP32 Board | Function |
|---|---|---|
| VCC | 3V3 | Powers the joystick module |
| GND | GND | Ground connection |
| VRx | GPIO 34 | Detects horizontal joystick movements |
| VRy | GPIO 32 | Detects vertical joystick movements |
| SW | GPIO 33 | Detects joystick button presses |
The proposed pins are examples of wiring configurations for a standard ESP32 development board.
The second ESP32 board receives Bluetooth commands from the transmitter and controls the motors through the L298N motor driver module.
| L298N Module | ESP32 Board | Function |
|---|---|---|
| IN1 | GPIO 22 | Controls the first motor rotation direction |
| IN2 | GPIO 21 | Controls the opposite rotation direction |
| IN3 | GPIO 19 | Controls the third motor control input |
| IN4 | GPIO 18 | Controls the fourth motor control input |
| ENA | GPIO 23 (PWM) | Controls the speed of the left-side motors |
| ENB | GPIO 5 (PWM) | Controls the speed of the right-side motors |
| GND | GND | Common ground connection |
Connecting the Motors to the L298N Module
| L298N Outputs | Connected Components |
|---|---|
| OUT1 – OUT2 | Two motors on the left side |
| OUT3 – OUT4 | Two motors on the right side |
The two motors on each side are connected together to operate simultaneously. This configuration allows the robot car to move in different directions by changing the motors' rotation direction and speed.
| Component | Connection | Function |
|---|---|---|
| 9V Battery | Power supply module input | Main energy source |
| Power Supply Module | 5V output | Supplies power to the ESP32 board |
| Two 3.7V Batteries | L298N motor power input | Supplies power to the motors |
| GND of the different modules | Common ground | Provides a common electrical reference |
First, both ESP32 boards must be prepared and configured to run MicroPython programs using the Thonny Integrated Development Environment (IDE).
To do this, each ESP32 board is connected individually to the computer using a USB cable. The MicroPython firmware is then installed on each board, and communication with the Thonny programming environment is checked to ensure that the boards are working correctly.
Next, the necessary libraries must be installed and transferred to the memory of both ESP32 boards:
- DCMotor: This library allows the control of DC motors through the L298N motor driver module.
- ble_uart_peripheral.py: This library enables BLE UART (Bluetooth Low Energy Universal Asynchronous Receiver-Transmitter) communication.
- ble_advertising.py: This library handles the broadcasting of the information required for another Bluetooth device to detect the ESP32 board.
Once this configuration is complete, both ESP32 boards are ready to receive their respective programs to transmit and receive the robot car movement commands via Bluetooth.
This Micropython program transforms the first ESP32 board into a wireless joystick controller that sends movement commands via Bluetooth to the second ESP32 board, enabling remote control of the four-wheel robot car.
|
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 155 156 157 158 159 160 161 162 163 164 |
# Import the required libraries import bluetooth import time from micropython import const # Import modules for GPIO pins and analog-to-digital conversion from machine import Pin, ADC from time import sleep # Configure the joystick analog pins joystick_X = ADC(Pin(34)) # Joystick horizontal axis connected to GPIO34 joystick_Y = ADC(Pin(32)) # Joystick vertical axis connected to GPIO32 # Configure the joystick push button # PULL_UP enables the internal pull-up resistor button = Pin(33, Pin.IN, Pin.PULL_UP) # Configure the ADC measurement range joystick_X.atten(ADC.ATTN_11DB) joystick_Y.atten(ADC.ATTN_11DB) # Define constants corresponding to Bluetooth events _IRQ_SCAN_RESULT = const(5) # Bluetooth device scan result _IRQ_SCAN_DONE = const(6) # Bluetooth scanning completed _IRQ_PERIPHERAL_CONNECT = const(7) # Connection to a peripheral _IRQ_GATTC_SERVICE_RESULT = const(9) # Service discovery result _IRQ_GATTC_CHARACTERISTIC_RESULT = const(11) # Characteristic discovery result _IRQ_GATTC_CHARACTERISTIC_DONE = const(12) # Characteristic discovery completed # Define the UUID of the BLE UART service SERVICE_UUID = bluetooth.UUID( "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" ) # Define the UUID of the characteristic used to transmit data TX_UUID = bluetooth.UUID( "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" ) # Activate Bluetooth on the ESP32 board ble = bluetooth.BLE() ble.active(True) # Initialize Bluetooth connection variables receiver_address = None # Bluetooth address of the receiver board connection = None # Established connection identifier characteristic = None # BLE UART characteristic identifier # Bluetooth event handling function def handle_event(event, data): global receiver_address, connection, characteristic # If a Bluetooth device is detected during scanning if event == _IRQ_SCAN_RESULT: addr_type, addr, adv_type, rssi, adv_data = data # Check whether the name ESP32_RECEPTEUR is present in the received data if b"ESP32_RECEPTEUR" in adv_data: receiver_address = (addr_type, bytes(addr)) # Stop scanning after finding the receiver board ble.gap_scan(None) # If Bluetooth scanning is completed elif event == _IRQ_SCAN_DONE: # If the receiver board address has been found if receiver_address: # Establish a Bluetooth connection with this board ble.gap_connect(receiver_address[0], receiver_address[1]) # If the connection with the receiver board is established elif event == _IRQ_PERIPHERAL_CONNECT: connection, addr_type, addr = data print("Connection established") # Discover the available services on the receiver board ble.gattc_discover_services(connection) # If a Bluetooth service is detected elif event == _IRQ_GATTC_SERVICE_RESULT: conn, start, end, uuid = data # Check whether the detected service matches the BLE UART service if uuid == SERVICE_UUID: # Discover the characteristics of this service ble.gattc_discover_characteristics(conn, start, end) # If a Bluetooth characteristic is detected elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT: conn, def_handle, value_handle, properties, uuid = data # Check whether the characteristic matches TX_UUID if uuid == TX_UUID: # Store its identifier to send commands characteristic = value_handle # If characteristic discovery is completed elif event == _IRQ_GATTC_CHARACTERISTIC_DONE: print("Discovery completed") # Associate the handler function with Bluetooth events ble.irq(handle_event) # Start scanning for the receiver ESP32 board print("Searching for ESP32_RECEPTEUR...") ble.gap_scan(5000, 30000, 30000) # Wait until the Bluetooth connection is established while connection is None: time.sleep_ms(100) # Wait for Bluetooth characteristic discovery to complete time.sleep_ms(1000) # Initialize the default command: stop the car message = "stop" # Main loop: read the joystick and send commands while True: # Read the analog values of the two joystick axes x = joystick_X.read() y = joystick_Y.read() # Read the push button state sw = button.value() # Check whether the Bluetooth characteristic is available if characteristic is not None: # Detect the requested direction from the joystick values # Move the car forward if x == 0 and 2700 < y < 3000: message = "forward" # Turn the car right if y == 0 and 2700 < x < 3000: message = "right" # Turn the car left if y == 4095 and x == 4095: message = "left" # Move the car backward if x == 4095 and y < 1000: message = "backward" # Stop the car when the push button is pressed if sw == 0: message = "stop" # Convert the message into bytes and send it to the receiver board ble.gattc_write( connection, characteristic, message.encode() ) # Display the transmitted command in the Thonny console print("Message sent:", message) # Wait one second before the next joystick reading time.sleep(1) |
This MicroPython program configures the second ESP32 board as a Bluetooth Low Energy (BLE) receiver to control the movements of the four-wheel robot car. It receives movement commands from the transmitter ESP32 board and controls the DC motors through the L298N motor driver module.
|
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 |
# Import the required libraries from machine import Pin, PWM import bluetooth import struct import time from micropython import const # Import the library used to control the motors from DCMotor import DCMotor # Define the PWM frequency used to control the motors frequency = 15000 # 15 kHz frequency # Configure the ESP32 GPIO pins # These pins determine the rotation direction of the motors pin1 = Pin(22, Pin.OUT) # First control pin of motor 1 pin2 = Pin(21, Pin.OUT) # Second control pin of motor 1 pin3 = Pin(19, Pin.OUT) # First control pin of motor 2 pin4 = Pin(18, Pin.OUT) # Second control pin of motor 2 # Configure PWM pins to control motor speed enable = PWM(Pin(23), frequency) # ENA pin of the L298N module enable1 = PWM(Pin(5), frequency) # ENB pin of the L298N module # Create objects representing the two DC motors # Each motor uses two direction pins and one PWM pin dc_motor = DCMotor(pin1, pin2, enable, 350, 1023) dc_motor1 = DCMotor(pin3, pin4, enable1, 350, 1023) # Define constants corresponding to Bluetooth events _IRQ_CENTRAL_CONNECT = const(1) # Central device connection _IRQ_CENTRAL_DISCONNECT = const(2) # Central device disconnection _IRQ_GATTS_WRITE = const(3) # Bluetooth data reception # Create the Bluetooth object and activate BLE ble = bluetooth.BLE() ble.active(True) # Define the UUIDs of the BLE UART service and characteristic SERVICE_UUID = bluetooth.UUID("6E400001-B5A3-F393-E0A9-E50E24DCCA9E") TX_UUID = bluetooth.UUID("6E400003-B5A3-F393-E0A9-E50E24DCCA9E") # Define the BLE service containing a writable characteristic UART_SERVICE = ( SERVICE_UUID, ( (TX_UUID, bluetooth.FLAG_WRITE), ), ) # Register the BLE service on the ESP32 board # Get the characteristic handle used to receive messages ((tx_handle,),) = ble.gatts_register_services((UART_SERVICE,)) # Bluetooth event handling function def handle_reception(event, data): # Check whether the connection with the transmitter ESP32 is established if event == _IRQ_CENTRAL_CONNECT: print("ESP32 transmitter connected") # Check whether the transmitter board is disconnected elif event == _IRQ_CENTRAL_DISCONNECT: print("Bluetooth disconnected") # Restart Bluetooth advertising to allow a new connection start_advertising() # Check whether data has been written to the BLE characteristic elif event == _IRQ_GATTS_WRITE: # Get the connection and characteristic information conn_handle, attr_handle = data # Check whether the received data belongs to the TX characteristic if attr_handle == tx_handle: # Read the received message and convert it into a string message = ble.gatts_read(tx_handle) print("Message received:", message.decode().strip()) # If the received command is "forward" if message.decode().strip() == "forward": # Rotate both motors in the forward direction dc_motor.forward(60) dc_motor1.forward(60) # If the received command is "backward" if message.decode().strip() == "backward": # Rotate both motors in the backward direction dc_motor.backwards(60) dc_motor1.backwards(60) # If the received command is "right" if message.decode().strip() == "right": # Rotate the left motor forward # and the right motor backward to turn right dc_motor.forward(65) dc_motor1.backwards(65) # If the received command is "left" if message.decode().strip() == "left": # Rotate the left motor backward # and the right motor forward to turn left dc_motor.backwards(65) dc_motor1.forward(65) # If the received command is "stop" if message.decode().strip() == "stop": # Stop both motors dc_motor.stop() dc_motor1.stop() # Associate the reception function with Bluetooth events ble.irq(handle_reception) # Function used to broadcast the ESP32 board name # so that it can be detected by the transmitter board def start_advertising(): # Define the Bluetooth name of the receiver board device_name = "ESP32_RECEPTEUR" # Build the Bluetooth advertising data advertising_data = bytearray( b'\x02\x01\x06' + bytes((len(device_name) + 1, 0x09)) + device_name.encode() ) # Start Bluetooth advertising ble.gap_advertise(100000, advertising_data) # Start Bluetooth advertising to make the board discoverable start_advertising() # Display a message indicating that the board is ready print("ESP32 receiver ready") |
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