The objective of this project is to design and implement a wireless DC motor control system using an ESP32, a Bluetooth connection, an L298N motor driver module, and a smartphone. The system enables the user to remotely start and stop the motor, change its direction of rotation, and adjust its speed through a mobile application.
Operation
The ESP32 establishes a Bluetooth connection with the smartphone. Once the connection is established, the user sends control commands from the mobile application. The ESP32 receives these commands and processes them to generate the appropriate control signals.
These signals are transmitted to the L298N motor driver module, which provides the required power to the DC motor while controlling its operating state. Depending on the received command, the L298N starts or stops the motor, changes its direction of rotation, or adjusts its speed using Pulse Width Modulation (PWM). An external battery pack supplies the power required by the motor, while the ESP32 manages the control logic.
This wireless system provides a simple and efficient way to control a DC motor remotely using a smartphone, making it suitable for robotics, automation, and educational applications.
1. ESP32 board

The ESP32 is a powerful microcontroller with built-in Bluetooth and Wi-Fi capabilities. It receives the commands sent from the smartphone via Bluetooth, processes them, and generates the control signals required to drive the DC motor.
2. L298N Motor Driver Module :

The L298N motor driver module acts as an interface between the ESP32 and the DC motor. It receives the control signals from the ESP32 and supplies the power required by the motor. It controls the motor's operating state, including start, stop, direction of rotation, and speed.
3. DC Motor (5 V)

The 5 V DC motor converts electrical energy into mechanical rotational motion. Its speed and direction are controlled by the L298N motor driver according to the commands received from the ESP32.
4. Jumper Wires

Jumper wires are used to connect the ESP32, the L298N module, the power supply, and the DC motor, ensuring reliable transmission of power and control signals.
Breadboard (Optional)

A breadboard provides a convenient platform for assembling and testing the electronic circuit without soldering. It allows components and jumper wires to be connected easily during prototyping.
5. External Power Supply (2 × 3.7 V Batteries)

The two 3.7 V rechargeable batteries provide the external power required by the L298N motor driver and the DC motor. This independent power source ensures stable motor operation while preventing excessive current draw from the ESP32.


1- L298N to Motor Connections:
OUT1 and OUT2: Connect to the two terminals of Motor A.
2- Power Connections:
12V: Connects to the motor power supply (9v battery).
GND: Common ground connection for the motor, ESP32, and power supply.
ESP32 to L298N Connections:
ENA: Connects to a PWM-capable GPIO on the ESP32 (e.g., GPIO 23).
IN1 and IN2: Connect to GPIO pins on the ESP32 (e.g., GPIO 21 and GPIO 22).
This MicroPython program enables an ESP32 to control the speed and direction of a DC motor through a Bluetooth Low Energy (BLE) connection with a smartphone. The ESP32 receives wireless commands from a mobile application and uses an L298N motor driver module to operate the motor accordingly.
Import this three libraries : ble_uart_peripheral.py , ble_advertising.py and DCMotor.
|
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 |
# ============================================================ # Importation des bibliothèques nécessaires # ============================================================ # Gestion de l'UART et des broches de l'ESP32 from machine import UART from machine import Pin, PWM # Bibliothèque de pilotage du moteur à courant continu from DCMotor import DCMotor # Fonction de temporisation from time import sleep # Gestion du Bluetooth Low Energy (BLE) import bluetooth # Service UART sur Bluetooth BLE from ble_uart_peripheral import BLEUART # Bibliothèques de gestion du temps import time import utime # ============================================================ # Configuration du moteur DC # ============================================================ # Fréquence du signal PWM utilisée pour régler la vitesse du moteur frequency = 15000 # Broches qui déterminent le sens de rotation du moteur pin1 = Pin(22, Pin.OUT) pin2 = Pin(21, Pin.OUT) # Broche PWM reliée à l'entrée ENA du module L298N # Elle permet de contrôler la vitesse du moteur enable = PWM(Pin(23), frequency) # Création d'un premier objet moteur dc_motor = DCMotor(pin1, pin2, enable) # Création de l'objet moteur avec les limites minimales et maximales # de la valeur PWM (350 à 1023) dc_motor = DCMotor(pin1, pin2, enable, 350, 1023) # ============================================================ # Configuration de la communication Bluetooth BLE # ============================================================ # Initialisation du contrôleur Bluetooth de l'ESP32 ble = bluetooth.BLE() # Création d'un port série virtuel (UART) via Bluetooth BLE uart = BLEUART(ble) # ============================================================ # Initialisation du moteur # ============================================================ # Au démarrage, le moteur tourne en marche avant à la vitesse minimale dc_motor.forward(1) # ============================================================ # Fonction exécutée à chaque réception d'une commande Bluetooth # ============================================================ def on_rx(): # Lecture des données reçues via Bluetooth uart_in = uart.read() # Conversion des données (bytes) en chaîne de caractères # Suppression des caractères invalides et des espaces inutiles data = uart_in.decode('utf-8', 'ignore').strip() # -------------------------------------------------------- # Format attendu du message : # # sensd700 # sensi450 # # sensd : rotation en marche avant # sensi : rotation en marche arrière # 700 : vitesse du moteur # -------------------------------------------------------- # Extraction de la partie correspondant à la vitesse vitesse = data[5:len(data)] # Extraction de la partie correspondant au sens sens = data[0:5] # Suppression des chiffres éventuellement présents # afin de conserver uniquement le texte "sensd" ou "sensi" sens = ''.join(c for c in sens if not c.isdigit()) # Conservation uniquement des chiffres de la vitesse vitesse = ''.join(c for c in vitesse if c.isdigit() or c == '.') # Conversion de la vitesse en entier vitesse = int(vitesse) # ======================================================== # Commande du moteur selon les données reçues # ======================================================== # Si la commande indique "sensd", # le moteur tourne en marche avant if sens == 'sensd': dc_motor.forward(vitesse) # Si la commande indique "sensi", # le moteur tourne en marche arrière if sens == 'sensi': dc_motor.backwards(vitesse) # ============================================================ # Association de la fonction on_rx à la réception Bluetooth # ============================================================ # À chaque réception de données Bluetooth, # la fonction on_rx() est automatiquement exécutée. uart.irq(handler=on_rx) # ============================================================ # Fermeture de la liaison UART # ============================================================ # Cette instruction ferme le service UART BLE. # Dans une application fonctionnant en continu, # cette ligne est généralement supprimée ou exécutée # uniquement à la fin du programme. uart.close() |

This mobile application is a simple Bluetooth control interface for a DC motor. It is designed to communicate with an ESP32 module and allows the user to control the motor remotely in real time.
At the top of the interface, there are four main control buttons:
- Scanner: used to search for available Bluetooth devices.
- Connect: establishes a Bluetooth connection with the ESP32.
- Disconnect: terminates the Bluetooth connection.
- Stop: immediately stops the motor for safety and control.
The main section of the application is titled “Control Motor” and provides the user interface for motor operation. It includes a visual representation of the motor to make the application more intuitive.
Below the image, the user can select the direction of rotation using two options:
- Clockwise (forward rotation)
- Anticlockwise (reverse rotation)
Only one direction is used at a time depending on the selected control mode.
At the bottom, a speed control slider allows the user to adjust the motor speed. The current speed value is displayed (e.g., “Speed: 0”), and the slider sends the corresponding value to the ESP32 via Bluetooth.
Overall, this application provides an easy-to-use interface for wireless control of a DC motor, combining device connection, direction selection, and speed adjustment in a single screen.
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