Introduction
The Raspberry Pi is more than a mini computer — it can also become the “brain” of robots. With its GPIO pins (General Purpose Input/Output), the Pi can connect and control motors, read sensors, and make autonomous decisions.
In this guide, you’ll learn:
• How Raspberry Pi is used in robotics.
• Basics of interfacing motors (DC, Servo, Stepper).
• How to connect common sensors (IR, Ultrasonic, Line tracking, etc.).
• Example Python programs to get your robot moving.
By the end, you’ll see how easy it is to turn your Raspberry Pi into the control center of a DIY robot
Why Use Raspberry Pi for Robotics?
• Computing Power: Runs Linux, Python, OpenCV (computer vision), AI libraries.
• Connectivity: Wi Fi + Bluetooth → remote controlled robots.
• Flexibility: Can combine with motor drivers, Arduino boards, and cameras.
• GPIO Pins: 40 pin header → Digital I/O, PWM, I²C, SPI support.
Motor Control with Raspberry Pi
You can’t connect motors directly to Pi GPIOs (they supply too little current). Instead you use Motor Driver ICs/Modules as intermediaries.
1. DC Motors
• Use L293D or L298N motor driver modules.
• Allows forward, reverse, and speed control (PWM).
Wiring:
• Motor → L298N OUT pins.
• L298N IN1, IN2 → Pi GPIO17, GPIO18.
• Enable → Pi GPIO.
• Power: Motor driver → 5 12V depending on motor.
Python Example (DC Motor forward/backward):
Python
import RPi.GPIO as GPIO
import time
in1, in2 = 17, 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
# Forward
GPIO.output(in1, True)
GPIO.output(in2, False)
time.sleep(2)
# Backward
GPIO.output(in1, False)
GPIO.output(in2, True)
time.sleep(2)
# Stop
GPIO.output(in1, False)
GPIO.output(in2, False)
GPIO.cleanup()
2. Servo Motors
• Rotate to specific angles (0°–180°).
• Great for robotic arms, pan/tilt cameras.
Wiring:
• Servo signal → Pi GPIO (e.g., 17).
• VCC → 5V (use external supply if multiple servos).
• GND → Pi GND.
Python Example (Sweep a Servo):
Python
import RPi.GPIO as GPIO
import time
servo = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servo, GPIO.OUT)
pwm = GPIO.PWM(servo, 50) # 50 Hz
pwm.start(0)
for angle in range(0, 181, 30):
duty = 2 + (angle/18)
pwm.ChangeDutyCycle(duty)
time.sleep(0.5)
pwm.stop()
GPIO.cleanup()
Sensors with Raspberry Pi
Robots need to “sense” the world. The Pi supports analog/digital sensors (using ADC like MCP3008 for analog).
Common Robotics Sensors:
• IR Obstacle Sensor: Detects objects in path → for obstacle avoidance.
• Ultrasonic Sensor (HC SR04): Measures distance via sound waves.
• Line Follower (IR Pair): Detects black/white lines for path following.
• Accelerometer/Gyroscope (MPU6050): For balancing robots.
• Camera Module: For vision/AI robots (OpenCV, TensorFlow).
Mini Robot Example: Obstacle Avoidance Car
Combine DC motors (L298N) + Ultrasonic Sensor + ESP/Bluetooth remote → build a smart car that:
• Moves forward until an obstacle is detected.
• Stops/reverses/turns depending on sensor reading.
Example: Ultrasonic Sensor with Raspberry Pi (HC SR04)
Wiring:
• Trig → GPIO23
• Echo → GPIO24 (use voltage divider for 3.3V safety)
• VCC → 5V, GND → GND
Python Code:
Python
import RPi.GPIO as GPIO
import time
TRIG = 23
ECHO = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
def distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
StartTime = time.time()
StopTime = time.time()
while GPIO.input(ECHO) == 0:
StartTime = time.time()
while GPIO.input(ECHO) == 1:
StopTime = time.time()
TimeElapsed = StopTime – StartTime
dist = (TimeElapsed * 34300) / 2
return dist
try:
while True:
print (“Distance: %.1f cm” % distance())
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
Output: Robot measures distance in cm.
Perfect for obstacle avoidance robots.
________________________________________
Mini Robot Example: Obstacle Avoidance Car
Combine DC motors (L298N) + Ultrasonic Sensor + ESP/Bluetooth remote → build a smart car that:
• Moves forward until an obstacle is detected.
• Stops/reverses/turns depending on sensor reading.
FAQs
Q: Can I connect sensors directly to Raspberry Pi?
• Digital sensors → yes.
• Analog sensors → need an ADC (like MCP3008).
Q: Can Raspberry Pi replace Arduino for robotics?
• Yes for high level tasks (AI, camera, networking).
• For real time motor control, many combine Pi + Arduino.
Q: What’s the best Raspberry Pi model for robotics?
• Raspberry Pi 4 → For AI, Computer Vision, wireless control.
• Raspberry Pi Zero W → For lightweight, small bots.
Conclusion
The Raspberry Pi can be the brains of your robot:
• Control motors (DC, Servo, Stepper).
• Sense environment with IR, UltraSonic, Line Tracking sensors.
• Process vision/AI with camera module + Python AI libraries.
From a line following bot to an AI vision robot, Raspberry Pi lets you scale from beginner experiments to advanced projects.
Now it’s time to hook up some wires and make your first robot move