How to Build a Raspberry Pi Zero Robot with L293D (Keyboard Controlled)
Step-by-step guide to building a Raspberry Pi Zero robot using L293D with PWM speed control and keyboard driving over SSH.
This guide explains how I built a two-wheel Raspberry Pi Zero robot using an L293D motor driver with proper voltage regulation, PWM speed control, and SSH-based keyboard driving. I share wiring details, voltage mistakes that can damage the Pi, and how to debug motor direction issues using a structured testing script before enabling real-time keyboard control.
Project at a Glance
| Item | Details |
|---|---|
| Cost | Low (depends on chassis and battery) |
| Time Required | 1–2 hours |
| Difficulty | Beginner to Intermediate |
| Controller | Raspberry Pi Zero |
| Motor Driver | L293D |
| Programming Language | Python |
| Control Method | SSH + Keyboard |
Is this a tutorial, experiment, or real-world build experience?
This is a hands-on tutorial, but more importantly, it is a structured build experience. The core information gain here is not just wiring a Raspberry Pi to an L293D. It is understanding why direct motor control from GPIO fails, how voltage regulation protects the Pi, and why testing before keyboard control prevents debugging chaos.
The undocumented issue most beginners face is unstable behavior caused by improper grounding or powering motors from the Pi’s 5V rail. That mistake alone can destroy a board. This guide avoids that.
Why did I choose Raspberry Pi Zero instead of Arduino?
The Raspberry Pi Zero gives me Linux, Wi-Fi, SSH access, and Python in one board. That means I can control the robot directly from my laptop without adding Bluetooth modules or external controllers.
Unlike Arduino, I can run structured scripts, remote-debug issues, and log errors. That flexibility becomes very powerful once sensors are added.
What robot chassis do you actually need for this build?
You only need a basic two-wheel robot chassis with two DC gear motors and one caster wheel.
You can buy a low-cost kit online or build your own frame using acrylic or plywood. The important part is having two independent motors.
By controlling the two motors, we get:
- Both motors clockwise → Forward
- Both motors counterclockwise → Backward
- Left motor ON, right motor OFF → Turn right
- Right motor ON, left motor OFF → Turn left
That’s the entire movement logic. Nothing magical.
The Raspberry Pi Robot Circuit Explained
How does the L293D motor driver really work in this circuit?
The L293D is a dual H-bridge motor driver. That means it can independently control two DC motors.
Each motor uses:
- Two input pins for direction
- One enable pin for speed control using PWM
The Raspberry Pi sends low-current logic signals to the L293D. The L293D handles the higher current required by the motors. Never connect motors directly to Raspberry Pi GPIO pins. That will damage the board immediatley.
If you want a deeper breakdown of H-bridges, I explained it in detail in my previous Arduino robot guide.
How should the power and voltage regulation be handled safely?
This is where most failures occured. The motors require higher current than the Raspberry Pi can safely provide. So we use two power paths:
- Motor Supply (Vs on L293D) comes directly from the battery.
- Logic Supply (Vss on L293D) must be regulated 5V.
I use a voltage regulator to convert the battery voltage into stable 5V. That regulated 5V powers:
- Raspberry Pi Zero
- L293D logic pin (Vss)
All grounds must be connected together. If grounds are not common, control signals behave randomly. If motors twitch randomly, check ground first.
How do I prepare Raspberry Pi Zero with Raspberry Pi OS?
Step 1: Flash Raspberry Pi OS
Use Raspberry Pi Imager to write Raspberry Pi OS to a microSD card. Create an empty file named ssh in the boot partition to enable SSH.
Step 2: Configure Wi-Fi
If running headless, add your WiFi credentials before booting. It saves hours of frustration.
Step 3: Connect via SSH
After boot, find the Pi’s IP address and connect:
ssh pi@YOUR_PI_IP
sudo apt update
sudo apt install python3-pip
How does the testing script prevent debugging chaos later?
Before enabling keyboard control, I run a structured testing loop.
#!/usr/bin/env python3
import time
import RPi.GPIO as GPIO
# BCM pin mapping from your schematic
M1_INA = 17 # In1A
M1_INB = 27 # In1B
M1_EN = 12 # En1 (GPIO12 / PWM0)
M2_INA = 23 # In2A
M2_INB = 24 # In2B
M2_EN = 13 # En2 (GPIO13 / PWM1)
STEP_SECONDS = 2
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
pins = [M1_INA, M1_INB, M1_EN, M2_INA, M2_INB, M2_EN]
for p in pins:
GPIO.setup(p, GPIO.OUT)
GPIO.output(p, GPIO.LOW)
def enable_motors(on: bool):
GPIO.output(M1_EN, GPIO.HIGH if on else GPIO.LOW)
GPIO.output(M2_EN, GPIO.HIGH if on else GPIO.LOW)
def motor_direction(clockwise: bool):
# Define "clockwise" as INA=1, INB=0 (swap if your motor spins opposite)
if clockwise:
GPIO.output(M1_INA, GPIO.HIGH); GPIO.output(M1_INB, GPIO.LOW)
GPIO.output(M2_INA, GPIO.HIGH); GPIO.output(M2_INB, GPIO.LOW)
else:
GPIO.output(M1_INA, GPIO.LOW); GPIO.output(M1_INB, GPIO.HIGH)
GPIO.output(M2_INA, GPIO.LOW); GPIO.output(M2_INB, GPIO.HIGH)
def stop_motors():
# Disable + inputs low (clean stop)
enable_motors(False)
GPIO.output(M1_INA, GPIO.LOW); GPIO.output(M1_INB, GPIO.LOW)
GPIO.output(M2_INA, GPIO.LOW); GPIO.output(M2_INB, GPIO.LOW)
def main():
setup()
try:
while True:
# Clockwise
motor_direction(clockwise=True)
enable_motors(True)
time.sleep(STEP_SECONDS)
# Stop
stop_motors()
time.sleep(STEP_SECONDS)
# Anti-clockwise
motor_direction(clockwise=False)
enable_motors(True)
time.sleep(STEP_SECONDS)
# Stop
stop_motors()
time.sleep(STEP_SECONDS)
except KeyboardInterrupt:
pass
finally:
stop_motors()
GPIO.cleanup()
if __name__ == "__main__":
main()
The test script:
- Initializes GPIO pins
- Starts PWM on GPIO12 and GPIO13
- Runs forward → stop → backward → stop
This isolates wiring issues early.
If the robot moves in the wrong direction, I swap motor input pins or reverse motor wires. That controlled approach prevents confusion later when keyboard logic is layered on top.
How does keyboard control work over SSH?
The second script uses the curses library.
#!/usr/bin/env python3
import time
import curses
import RPi.GPIO as GPIO
# --- Pins ---
M1_INA = 17
M1_INB = 27
M1_EN = 12 # Left motor PWM
M2_INA = 23
M2_INB = 24
M2_EN = 13 # Right motor PWM
# --- Global Speed ---
LEFT_SPEED = 25
RIGHT_SPEED = 30
pwm1 = None
pwm2 = None
def setup():
global pwm1, pwm2
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for p in (M1_INA, M1_INB, M1_EN, M2_INA, M2_INB, M2_EN):
GPIO.setup(p, GPIO.OUT)
GPIO.output(p, GPIO.LOW)
pwm1 = GPIO.PWM(M1_EN, 100)
pwm2 = GPIO.PWM(M2_EN, 100)
pwm1.start(0)
pwm2.start(0)
def stop():
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(0)
GPIO.output(M1_INA, 0); GPIO.output(M1_INB, 0)
GPIO.output(M2_INA, 0); GPIO.output(M2_INB, 0)
def forward():
GPIO.output(M1_INA, 1); GPIO.output(M1_INB, 0)
GPIO.output(M2_INA, 1); GPIO.output(M2_INB, 0)
pwm1.ChangeDutyCycle(LEFT_SPEED)
pwm2.ChangeDutyCycle(RIGHT_SPEED)
def backward():
GPIO.output(M1_INA, 0); GPIO.output(M1_INB, 1)
GPIO.output(M2_INA, 0); GPIO.output(M2_INB, 1)
pwm1.ChangeDutyCycle(LEFT_SPEED)
pwm2.ChangeDutyCycle(RIGHT_SPEED)
def turn_right():
GPIO.output(M1_INA, 0); GPIO.output(M1_INB, 0)
GPIO.output(M2_INA, 1); GPIO.output(M2_INB, 0)
pwm1.ChangeDutyCycle(0)
pwm2.ChangeDutyCycle(RIGHT_SPEED)
def turn_left():
GPIO.output(M2_INA, 0); GPIO.output(M2_INB, 0)
GPIO.output(M1_INA, 1); GPIO.output(M1_INB, 0)
pwm2.ChangeDutyCycle(0)
pwm1.ChangeDutyCycle(LEFT_SPEED)
# -------- Keyboard Control --------
def keyboard_control(stdscr):
stdscr.nodelay(True) # non-blocking
stdscr.clear()
stdscr.addstr("Use arrow keys to move. Press 'q' to quit.\n")
while True:
key = stdscr.getch()
if key == curses.KEY_UP:
forward()
elif key == curses.KEY_DOWN:
backward()
elif key == curses.KEY_LEFT:
turn_left()
elif key == curses.KEY_RIGHT:
turn_right()
elif key == ord('q'):
break
else:
stop()
time.sleep(0.05)
# -------- Main --------
if __name__ == "__main__":
setup()
try:
curses.wrapper(keyboard_control)
except KeyboardInterrupt:
pass
finally:
stop()
pwm1.stop()
pwm2.stop()
GPIO.cleanup()
It continuously reads arrow key inputs:
- Up → forward()
- Down → backward()
- Left → turn_left()
- Right → turn_right()
- No key → stop()
- q → exit program
The motors respond in near real-time.
Because we structured the code using clean movement functions, the keyboard logic is extremely simple. That modular structure is intentional.
What common mistakes should beginners avoid?
- Powering motors from the Raspberry Pi 5V pin.
- Forgetting common ground.
- Not testing direction before keyboard control.
- Using non-PWM pins for enable signals.
RootSaid Fix: Always isolate motor supply from logic supply.
Final Thoughts: Is Raspberry Pi Zero good for robotics?
Yes, especially if you want remote access, networking, or AI integration later.
The Pi Zero allows expansion into web control, camera streaming, and sensor integration without changing the core motor logic.
That is why I prefer this approach over basic microcontroller-only builds.
FAQ Section
Why are my motors spinning but the robot not moving straight?
Uneven motor speeds or wheel friction differences cause drift. Adjust LEFT_SPEED and RIGHT_SPEED values.
Can I power everything from a single battery?
Yes, but you must regulate voltage properly and isolate motor supply from logic supply.
Why is my Raspberry Pi rebooting when motors start?
Your motor supply is causing voltage drops. Use a separate supply or better regulator.
Can I add ultrasonic sensors to this robot?
Yes. The movement functions remain the same; you only add sensor logic on top.

One Comment