π‘οΈ Raspberry Pi β Sensors & Motor Control (2025 Hands-On Guide for GPIO Projects)
π§² Introduction β Control the Physical World with Sensors & Motors
Interfacing sensors and motors with Raspberry Pi brings your projects to life. From measuring temperature to controlling servo motors, this guide shows you how to connect, power, and program essential hardware components using Raspberry Piβs GPIO.
π― In this guide, youβll learn:
- How to connect and read data from sensors
- How to control DC motors, servos, and relays
- Sample Python code using GPIO and
gpiozero
- Real-world examples: motion detection, automation, robotics
π§° Components Required
π§ͺ Component | π§ Quantity |
---|---|
Temperature Sensor (e.g., DHT11/22) | 1 |
Ultrasonic Sensor (HC-SR04) | 1 |
DC Motor / Servo Motor | 1 or more |
L298N Motor Driver Module | 1 |
Raspberry Pi + Breadboard | 1 |
Jumper Wires & Power Supply | As needed |
β Optional: External 5V power source for motors
π‘οΈ Reading Sensors β DHT11/22 Example
β Wiring:
Sensor Pin | Connects To |
---|---|
VCC | 3.3V (Pin 1) |
GND | GND (Pin 6) |
DATA | GPIO4 (Pin 7) |
β Install Library:
pip3 install Adafruit_DHT
β Python Code:
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22
pin = 4 # GPIO4
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print(f'Temp: {temperature:.1f}Β°C, Humidity: {humidity:.1f}%')
else:
print('Failed to read sensor')
β Use this setup to monitor room temperature or trigger fans.
π Distance Sensor β HC-SR04 (Ultrasonic)
β Wiring:
Pin | Connects To |
---|---|
VCC | 5V (Pin 2) |
GND | GND (Pin 6) |
TRIG | GPIO23 (Pin 16) |
ECHO | GPIO24 (Pin 18) |
β Python Code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24
GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)
GPIO.output(TRIG, False)
time.sleep(2)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO) == 0:
pulse_start = time.time()
while GPIO.input(ECHO) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
print(f"Distance: {distance} cm")
GPIO.cleanup()
π§ Great for robotics, obstacle detection, and automation triggers.
π§ Motor Control with L298N Driver
β Connect L298N to Raspberry Pi:
L298N Pin | Connects To |
---|---|
IN1 | GPIO17 (Pin 11) |
IN2 | GPIO18 (Pin 12) |
ENA | Jumper or GPIO PWM |
VCC | External 9V battery |
GND | Raspberry Pi GND |
β Python Code:
import RPi.GPIO as GPIO
import time
IN1 = 17
IN2 = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
# Motor Forward
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
time.sleep(2)
# Motor Reverse
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
time.sleep(2)
GPIO.cleanup()
β
This controls direction. Use PWM
for speed control.
π Servo Motor Control
β Connect Servo:
Servo Wire | Connects To |
---|---|
VCC | 5V (Pin 2) |
GND | GND (Pin 6) |
Signal | GPIO17 (Pin 11) |
β Python Code:
from gpiozero import Servo
from time import sleep
servo = Servo(17)
while True:
servo.min()
sleep(1)
servo.max()
sleep(1)
β Ideal for robotic arms, pan/tilt cams, and automation projects.
π§ͺ Sensor + Motor Use Cases
π€ Project Idea | π Description |
---|---|
Temperature-Controlled Fan | Sensor triggers motor when temp exceeds limit |
Motion Detector Alarm | Ultrasonic or PIR sensor triggers buzzer/motor |
Smart Curtain Opener | Servo adjusts curtain via light sensor input |
Obstacle Avoidance Car | Ultrasonic sensor controls motor directions |
π Summary β Recap & Next Steps
Interfacing sensors and motors makes Raspberry Pi a powerful tool for real-world interaction. With GPIO, Python, and a few components, you can automate, detect, and move things intelligently.
π Key takeaways:
- Use GPIO to connect sensors like DHT22, HC-SR04, etc.
- Control DC motors or servos using motor drivers and Python
- Combine input (sensors) and output (motors) for dynamic projects
- Use libraries like
gpiozero
,RPi.GPIO
,Adafruit_DHT
for ease
βοΈ Real-world relevance: Perfect for home automation, smart farming, robotics, and interactive systems.
β FAQs β Raspberry Pi Sensors & Motors
β Do I need an external power source for motors?
β Yes, for DC motors and servos, itβs best to use a separate 5β12V supply to avoid stressing the Pi.
β Can I read multiple sensors at once?
β Yes. Just assign each sensor to a different GPIO pin and manage them with Python loops or threads.
β Is it safe to connect a 5V sensor to GPIO?
β Only if it outputs 3.3V logic or uses a voltage divider or logic level converter.
β Whatβs the difference between PWM and GPIO output?
β GPIO output is on/off, while PWM can simulate analog values (e.g., motor speed, servo angle).
β Which library is best for GPIO motor control?
β
Use RPi.GPIO
for full control or gpiozero
for beginner-friendly syntax.
Share Now :