🌑️ 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 Motor1 or more
L298N Motor Driver Module1
Raspberry Pi + Breadboard1
Jumper Wires & Power SupplyAs needed

βœ… Optional: External 5V power source for motors


🌑️ Reading Sensors – DHT11/22 Example

βœ… Wiring:

Sensor PinConnects To
VCC3.3V (Pin 1)
GNDGND (Pin 6)
DATAGPIO4 (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:

PinConnects To
VCC5V (Pin 2)
GNDGND (Pin 6)
TRIGGPIO23 (Pin 16)
ECHOGPIO24 (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 PinConnects To
IN1GPIO17 (Pin 11)
IN2GPIO18 (Pin 12)
ENAJumper or GPIO PWM
VCCExternal 9V battery
GNDRaspberry 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 WireConnects To
VCC5V (Pin 2)
GNDGND (Pin 6)
SignalGPIO17 (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 FanSensor triggers motor when temp exceeds limit
Motion Detector AlarmUltrasonic or PIR sensor triggers buzzer/motor
Smart Curtain OpenerServo adjusts curtain via light sensor input
Obstacle Avoidance CarUltrasonic 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

🌑️ Raspberry Pi – Sensors & Motor Control

Or Copy Link

CONTENTS
Scroll to Top