0
0
Iot-protocolsProgramBeginner · 2 min read

Raspberry Pi Program to Control DC Motor

Use Python with the RPi.GPIO library to control a DC motor by setting GPIO pins high or low; for example, import RPi.GPIO as GPIO, set pin mode, and use GPIO.output(pin, GPIO.HIGH) to run the motor.
📋

Examples

InputRun motor forward for 5 seconds
OutputMotor spins forward for 5 seconds then stops
InputRun motor backward for 3 seconds
OutputMotor spins backward for 3 seconds then stops
InputStop motor immediately
OutputMotor stops spinning
🧠

How to Think About It

To control a DC motor with a Raspberry Pi, you use GPIO pins to send signals that turn the motor on or off and control its direction. You connect the motor to a motor driver circuit, then write a program that sets the GPIO pins high or low to spin the motor forward, backward, or stop it.
📐

Algorithm

1
Import the GPIO library and time module
2
Set GPIO mode to BCM
3
Set motor control pins as output
4
Turn motor on by setting pins high or low to control direction
5
Wait for desired time
6
Turn motor off by setting pins low
7
Clean up GPIO settings
💻

Code

python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

motor_pin1 = 17
motor_pin2 = 18

GPIO.setup(motor_pin1, GPIO.OUT)
GPIO.setup(motor_pin2, GPIO.OUT)

print("Motor running forward")
GPIO.output(motor_pin1, GPIO.HIGH)
GPIO.output(motor_pin2, GPIO.LOW)
time.sleep(5)

print("Motor stopped")
GPIO.output(motor_pin1, GPIO.LOW)
GPIO.output(motor_pin2, GPIO.LOW)

GPIO.cleanup()
Output
Motor running forward Motor stopped
🔍

Dry Run

Let's trace running the motor forward for 5 seconds then stopping it.

1

Setup GPIO mode

GPIO mode set to BCM numbering

2

Setup motor pins

Pins 17 and 18 set as output

3

Run motor forward

Pin 17 = HIGH, Pin 18 = LOW; motor spins forward

4

Wait 5 seconds

Program pauses for 5 seconds

5

Stop motor

Pin 17 = LOW, Pin 18 = LOW; motor stops

6

Cleanup GPIO

GPIO pins reset to default state

StepPin 17Pin 18Motor State
SetupN/AN/APins configured
Run forwardHIGHLOWSpinning forward
WaitHIGHLOWSpinning forward
StopLOWLOWStopped
CleanupN/AN/AGPIO reset
💡

Why This Works

Step 1: GPIO Setup

We use GPIO.setmode(GPIO.BCM) to select the pin numbering system and set the motor pins as outputs to control voltage signals.

Step 2: Motor Direction Control

Setting one pin HIGH and the other LOW makes the motor spin in one direction; reversing these signals spins it the other way.

Step 3: Stopping the Motor

Setting both pins LOW cuts power to the motor, stopping it safely.

Step 4: Cleaning Up

Calling GPIO.cleanup() resets the pins so they don't stay active after the program ends.

🔄

Alternative Approaches

Using PWM for speed control
python
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
motor_pin = 17
GPIO.setup(motor_pin, GPIO.OUT)
pwm = GPIO.PWM(motor_pin, 100)  # 100 Hz
pwm.start(0)

print("Motor speed ramp up")
for speed in range(0, 101, 20):
    pwm.ChangeDutyCycle(speed)
    time.sleep(1)

print("Motor stop")
pwm.ChangeDutyCycle(0)
pwm.stop()
GPIO.cleanup()
This method controls motor speed by changing the power level with PWM but requires a motor driver that supports PWM input.
Using GPIO Zero library
python
from gpiozero import Motor
from time import sleep

motor = Motor(forward=17, backward=18)

print("Motor forward")
motor.forward()
sleep(5)

print("Motor stop")
motor.stop()
GPIO Zero simplifies motor control with easy methods but adds a dependency and is less flexible for complex setups.

Complexity: O(1) time, O(1) space

Time Complexity

The program runs a fixed sequence of commands without loops that depend on input size, so it runs in constant time.

Space Complexity

Uses a fixed number of variables and GPIO pins, so memory use is constant.

Which Approach is Fastest?

All approaches run quickly; using GPIO Zero adds abstraction but simplifies code, while PWM adds complexity for speed control.

ApproachTimeSpaceBest For
Basic GPIO controlO(1)O(1)Simple on/off motor control
PWM speed controlO(1)O(1)Controlling motor speed smoothly
GPIO Zero libraryO(1)O(1)Easy and readable motor control
💡
Always use a motor driver circuit between the Raspberry Pi and the motor to protect the Pi from high current.
⚠️
Connecting the motor directly to Raspberry Pi GPIO pins without a driver can damage the Pi due to high current draw.