Controlling a two-wheeled ESP32 robot via a smartphone using Bluetooth, an L298N motor driver, and a custom app built in MIT App Inventor involves the following process:
1- ESP32 Microcontroller:
Acts as the brain of the robot.
Receives Bluetooth commands from the smartphone app and processes them to control the motors.
2- L298N Motor Driver Module:
Drives the two DC motors of the robot based on signals from the ESP32.
Converts low-power control signals into higher power for the motors.
3- DC Motors and Wheels:
Provide forward, backward, and turning movements for the robot.
4- Smartphone App:
Sends movement commands via Bluetooth to the ESP32.
Built using MIT App Inventor for a graphical control interface.
ESP32:
Microcontroller used to process data from the HC-SR04 sensor and control the L298D motor driver.
L298D Motor Driver Module:
Interfaces between the ESP32 and the DC motors.
Controls the speed and direction of the motors based on commands from the ESP32.
DC Motors and Wheels:
Provide locomotion for the robot.
Power Supply:
Batteries power the ESP32, motor driver, and motors (e.g., 7.4V or 12V battery for motors).
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.
IN1 (Motor A Forward): GPIO 22.
IN2 (Motor A Backward): GPIO 21.
IN3 (Motor B Forward): GPIO 19.
IN4 (Motor B Backward): GPIO 18.
ENA : GPIO 23
ENB : GPIO 5
Enable Pins (EN1/EN2): Connected to 5V or PWM pins for speed control.
VCC (Motor Supply): Connect to battery.
GND: Common ground with ESP32.
Motor A: Connect one DC motor to OUT1 and OUT2.
Motor B: Connect the other DC motor to OUT3 and OUT4
Here is the micropython program which allows to control the car by the Smartphone.
1- Flash your ESP32 with MicroPython using this file esp32-20210902-v1.17.bin.
2- import this libraries :
ble_uart_peripheral.py and ble_advertising.py for Bluetooth communication
DCMotor.py library to control the two motors.
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 |
from machine import UART from DCMotor import DCMotor from machine import Pin, PWM from time import sleep import bluetooth from ble_uart_peripheral import BLEUART import time import utime frequency = 15000 pin1 = Pin(22, Pin.OUT) pin2 = Pin(21, Pin.OUT) pin3 = Pin(19, Pin.OUT) pin4 = Pin(18, Pin.OUT) enable = PWM(Pin(23), frequency) enable1 = PWM(Pin(5), frequency) dc_motor = DCMotor(pin1, pin2, enable) dc_motor = DCMotor(pin1, pin2, enable, 350, 1023) dc_motor1 = DCMotor(pin3, pin4, enable1) dc_motor1 = DCMotor(pin3, pin4, enable1, 350, 1023) # Create BLE object ble = bluetooth.BLE() # Open UART session for BLE uart = BLEUART(ble) # Define ISR for an UART input on BLE connection def on_rx(): # Read UART string, AppInventor sends raw bytes uart_in = uart.read() # lire le message recu du Smartphone via Bluetooth print("UART IN: ", uart_in.decode()) # afficher le message recu du Smartphone sur le console de Thonny if (uart_in.decode().find('forward')==0): dc_motor.forward(100) # the car moves forward dc_motor1.forward(100) if (uart_in.decode().find(right')==0): dc_motor.forward(100) # the car turns right dc_motor1.forward(10) if (uart_in.decode().find('left')==0): dc_motor.forward(10) # the car turns left dc_motor1.forward(100) if (uart_in.decode().find('backward')==0): dc_motor.backwards(100) # the car backs up dc_motor1.backwards(100) if (uart_in.decode().find('stop')==0): dc_motor.stop() # la voiture tourne a droite dc_motor1.stop() # Map ISR to UART read interrupt uart.irq(handler=on_rx) uart.close() |
1. Creating the application with App Inventor:
Create a mobile app with App Inventor.
Use the available Bluetooth components to establish a connection with the ESP32.
- Add button Scanner to search ESP32 board.
- Add button Stop to stop the search.
- Add button Connect to connect to ESP32 board.
- Add button Disconnect to disconnect from ESP32 board.
- Create buttons for Forward (F), Backward (B), Left (L), and Right (R).
Here is the Designer part of the application with App Inventor :
2. Programming the application with App Inventor:
a- Use the available blocks in App Inventor to establish a Bluetooth connection with the ESP32.
- Starting with Android 12, Bluetooth permissions have been enhanced to improve security and user data protection. This is why we must declare the authorizations that your application needs in the AndroidManifest.xml file. For Bluetooth, you'll need to include ACCESS_FINE_LOCATION, BLUETOOTH_SCAN, and possibly BLUETOOTH_CONNECT permissions, depending on the features you're using.
b- Use these programming blocks to send commands when buttons are pressed.
Download project Download application
How It Works
1- Pair the ESP32:
Pair your smartphone with the ESP32 via Bluetooth (name: ESP32_Robot
).
2- Control the Robot:
Open the MIT App Inventor app and connect to the ESP32.
Press buttons to send commands:
F: Move forward.
B: Move backward.
L: Turn left.
R: Turn right.
3- Robot Response:
The ESP32 receives the command, processes it, and activates the L298N to control motor movement.
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
+216 92 886 231
medaliprof@gmail.com
Robotic site created by MedAli-Teacher info