๐ค A comprehensive robotics communication framework for Raspberry Pi, Arduino Uno, and ESP32 with motor control via L298N driver
This project provides a complete three-tier communication system that enables a Raspberry Pi to control robotic movements through an Arduino microcontroller and ESP32 module. It's perfect for:
- ๐ Robotics enthusiasts
- ๐ Educational IoT projects
- ๐ฌ DIY home automation systems
- ๐ ๏ธ Prototyping motor control solutions
Made for everyone - fully commented code and detailed documentation for all skill levels!
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Raspberry Pi 4 โ
โ (Master Controller) โ
โ Sends Commands via โ
โ Serial (USB) โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ USB Cable @ 9600 baud
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Arduino Uno โ
โ (Bridge/Relay) โ
โ Receives commands โ
โ Forwards to ESP32 โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ Serial TX/RX @ 9600 baud
โ (SoftwareSerial Pins 2,3)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ESP32 Module โ
โ (Motor Controller) โ
โ Controls L298N Driver โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ GPIO pins 25-33
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ L298N Motor Driver โ
โ Powers DC Motors & โ
โ Servo Motors via โ
โ 12V external supply โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโ
Purpose: Master controller for the entire system
- Connection: USB serial to Arduino Uno
- Features:
- Interactive command menu
- Real-time motor control (forward, backward, left, right, stop)
- Serial communication debugging
- User-friendly interface with emoji feedback
- Graceful shutdown with motor stopping
How to use:
python3 raspberry_pi_controller.pyAvailable Commands:
f- Move FORWARDb- Move BACKWARDl- Turn LEFTr- Turn RIGHTs- STOP all motorsq- QUIT program
Requirements:
pip install pyserialPurpose: Bridge communication between Raspberry Pi and ESP32
- Receives: Commands from Pi via USB Serial (baud: 9600)
- Sends: Commands to ESP32 via SoftwareSerial (baud: 9600)
- Features:
- Bidirectional serial communication
- Command validation (only accepts f, b, l, r, s)
- Debug messages for monitoring
- Error handling
Wiring (Arduino Uno):
| Arduino Pin | Connection | Purpose |
|---|---|---|
| USB Port | Raspberry Pi USB | Serial communication |
| Pin 2 (RX) | ESP32 TX (GPIO 17) | Receive from ESP32 |
| Pin 3 (TX) | ESP32 RX (GPIO 16) | Send to ESP32 |
| GND | ESP32 GND | Common ground |
Upload Instructions:
- Connect Arduino to PC via USB
- Open Arduino IDE
- Select Board: Arduino Uno
- Select COM Port
- Click Upload
Purpose: Motor control logic - receives commands and drives motors
- Receives: Commands from Arduino via UART (baud: 9600)
- Controls: L298N motor driver for DC motors
- Features:
- PWM speed control (0-255)
- Differential speed for turning
- Real-time motor monitoring
- Comprehensive debug output
Motor Control Pins (ESP32):
| GPIO | Purpose | Motor |
|---|---|---|
| 27 | IN1 (Direction) | Motor A Forward |
| 26 | IN2 (Direction) | Motor A Backward |
| 25 | IN3 (Direction) | Motor B Forward |
| 33 | IN4 (Direction) | Motor B Backward |
| 14 | ENA (PWM Speed) | Motor A Speed |
| 32 | ENB (PWM Speed) | Motor B Speed |
Motor Speeds:
- Forward/Backward: 200/255 (Full power)
- Turning: 150/255 (Reduced power for precise turns)
L298N Motor Driver Connections:
| L298N Pin | ESP32 Pin | Purpose |
|---|---|---|
| IN1 | GPIO 27 | Motor A Forward Control |
| IN2 | GPIO 26 | Motor A Backward Control |
| IN3 | GPIO 25 | Motor B Forward Control |
| IN4 | GPIO 33 | Motor B Backward Control |
| ENA | GPIO 14 | Motor A Speed (PWM) |
| ENB | GPIO 32 | Motor B Speed (PWM) |
| GND | GND | Common Ground |
| +12V | 12V Battery | Motor Power Supply |
| OUT1/OUT2 | Motor A | DC Motor Connection |
| OUT3/OUT4 | Motor B | DC Motor Connection |
- ๐ Raspberry Pi 4 (with power supply)
- ๐ด Arduino Uno (with USB cable)
- ๐ฑ ESP32 Development Board
- ๐ L298N Dual Motor Driver Module
- โ๏ธ 2x DC Motors (or equivalent)
- ๐ 12V Battery (for motor power)
- ๐งต Breadboard and jumper wires
- ๐ USB cables (Pi power, Arduino programming)
- ๐ฆ Servo motors for robotic arms
- ๐๏ธ Potentiometer for speed adjustment
- ๐ก LED indicators for motor status
- ๐ Buzzer for audio feedback
โโ 5V/3A PSU โโโฌโโ> Raspberry Pi
โ โโโ> Arduino 5V (optional)
โ
โโ 12V Battery โโ> L298N GND + 12V
Raspberry Pi โโ[USB]โโ> Arduino Uno โโ[Serial]โโ> ESP32
/dev/ttyACM0 Pins 2,3 GPIO 16,17
(9600 baud) SoftwareSerial UART0
(9600 baud)
ESP32 GPIOs (27,26,25,33,14,32) โโ> L298N (IN1,IN2,IN3,IN4,ENA,ENB)
โ
Motors (via OUT pins)
- Connect all components according to the wiring diagrams above
- Ensure all grounds are connected
- Test USB connections
- Open Arduino IDE
- Load
arduino_bridge.ino - Upload to Arduino Uno
- Install ESP32 board in Arduino IDE
- Load
esp32_motor_controller.ino - Upload to ESP32
- Install Python dependencies:
pip install pyserial - Run:
python3 raspberry_pi_controller.py - Use interactive menu to control motors
1. User enters command in Pi interface (e.g., 'f' for forward)
โ
2. Pi sends character 'f' via USB serial to Arduino
โ
3. Arduino receives 'f', validates it, forwards to ESP32
โ
4. ESP32 receives 'f', executes moveForward() function
โ
5. Motor pins (IN1,IN2,IN3,IN4) set to forward direction
โ
6. PWM pins (ENA, ENB) set to motor speed (200/255)
โ
7. L298N driver receives signals and powers motors
โ
8. Motors rotate and robot moves forward! ๐
Forward/Backward:
- Both motors: 200/255 speed
- Result: Straight movement
Left Turn:
- Left motor: 150/255 speed (slower)
- Right motor: 200/255 speed (normal)
- Result: Rotates left
Right Turn:
- Left motor: 200/255 speed (normal)
- Right motor: 150/255 speed (slower)
- Result: Rotates right
- Solution: Check USB cable, run
ls /dev/tty*on Pi - Default port:
/dev/ttyACM0or/dev/ttyUSB0
- Solution: Check L298N power connections (12V battery)
- Verify GPIO pin connections match code
- Test motor connections directly with battery
- Solution: Ensure all baud rates are 9600
- Check GND connections between all devices
- Verify UART pins (Arduino 2,3 and ESP32 RX/TX)
Uncomment debug lines in code to see serial output:
Serial.println("Debug: Motor A speed = " + String(speed));All code files include:
- ๐ Section headers with clear descriptions
- ๐ฌ Inline comments explaining complex logic
- ๐ Function documentation with parameters
- ๐ Wiring reference diagrams
โ ๏ธ Important warnings and notes
Perfect for learning and customization!
#define MOTOR_FORWARD_SPEED 255 // Maximum (was 200)#define MOTOR_TURN_SPEED 100 // Tighter turns (was 150)Serial.begin(115200); // Faster communication (was 9600)#include <Servo.h>
Servo servo1;
servo1.attach(GPIO_PIN);
servo1.write(90); // Set position- Raspberry Pi Documentation
- Arduino Reference
- ESP32 Documentation
- L298N Motor Driver Guide
- PySerial Documentation
Contributions are welcome! Feel free to:
- ๐ Report bugs via Issues
- ๐ก Suggest improvements
- ๐ง Submit pull requests
- ๐ Improve documentation
This project is open-source and available for educational and personal use.
Q: Can I use different microcontrollers? A: Yes! Modify pin definitions to match your hardware.
Q: How many motors can I control? A: L298N supports 2 motors. Add more modules for additional motors.
Q: Can I add wireless control? A: Yes! Replace USB with Bluetooth/WiFi modules.
Q: Is this safe for beginners? A: Yes! All code is heavily commented and step-by-step guides provided.
- โญ Star this repository
- ๐ด Fork to your account
- ๐ฅ Clone to your local machine
- ๐ Follow the Quick Start Guide
- ๐ Build amazing robots!
Happy Building! ๐คโจ