Skip to content

Project 1.4.2: Relay Controlled DC Motor Driver


Overview

Build a relay-switched DC motor project with a flyback diode.

This project demonstrates safe switching of an inductive load.

The final result is a motor that turns on and off when the relay changes state.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
1-channel relay module
1-channel relay module
DC motor
DC motor
Push button
Push button
External motor supply
External motor supply
Breadboard
Breadboard
Jumper wires
Jumper wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
Relay VCC 5V / VSYS Physical pin 40 Module power
Relay GND GND Physical pin 38
Relay IN GPIO 0 GPIO 0 / physical pin 1 Usually active-low
Button leg 1 GPIO 1 GPIO 1 / physical pin 2 Use internal pull-up
Button opposite leg GND Physical pin 38
External motor supply positive Relay COM Not a GPIO pin Motor power input
Relay NO Motor positive terminal Not a GPIO pin Motor receives power
Motor negative terminal External motor supply negative Not a GPIO pin

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2 W

Place the Raspberry Pi Pico 2 W on the breadboard so it sits across the center gap.

Step 1


Step 2: Place the Push Button

Insert the push button across the breadboard center gap.

Step 2


Step 3: Connect the Push Button to GPIO 1

Connect one side of the push button to GPIO 1.

Step 3


Step 4: Connect the Push Button to GND

Connect the opposite side of the push button to GND.

Step 4


Step 5: Connect the Relay Module Power Pins

Connect relay VCC to the correct relay supply and relay GND to ground.

Step 5


Step 6: Connect Relay IN to GPIO 0

Connect the relay module IN pin to GPIO 0.

Step 6


Step 7: Connect Motor Supply Positive to Relay COM

Connect the positive wire from the external motor supply to relay COM.

Step 7


Step 8: Connect Relay NO to Motor Positive

Connect relay NO to the motor positive terminal.

Step 8


Step 9: Connect Motor Negative to External Supply Negative

Connect the motor negative terminal to the external motor supply negative.

Step 9


Step 10: Connect the Grounds Together

Connect Pico GND, relay GND, and external motor supply negative / GND together.

Step 10


Wiring Check

  • Relay VCC connects to the correct relay supply voltage.
  • Relay GND connects to ground.
  • Relay IN connects to GPIO 0.
  • Button connects between GPIO 1 and GND.
  • External motor supply positive connects to relay COM.
  • Relay NO connects to motor positive terminal.
  • Motor negative terminal connects to external supply negative.
  • Motor can spin freely before power is applied.

Testing Individual Components

Relay Test

from machine import Pin
import time

relay = Pin(0, Pin.OUT)
relay.value(1)
time.sleep(1)
relay.value(0)
time.sleep(1)
relay.value(1)

Motor Power Test

Briefly connect the motor directly to the correct external supply voltage to make sure it spins.


Full Project Code

from machine import Pin
import time

relay = Pin(0, Pin.OUT)
button = Pin(1, Pin.IN, Pin.PULL_UP)
relay.value(1)
last_button = 1

print('Motor relay control ready')

while True:
    current_button = button.value()

    if current_button == 0 and last_button == 1:
        relay.value(0 if relay.value() else 1)
        print('Motor ON' if relay.value() == 0 else 'Motor OFF')
        time.sleep(0.2)

    last_button = current_button
    time.sleep(0.02)

How the Code Works

Code Section What It Does Why It Matters
Relay setup Prepares the relay control pin The relay switches motor power
Button input Reads the control button Lets the user toggle the motor
Toggle logic Changes relay state on each press Turns the motor on and off
Printed status Reports motor state Makes troubleshooting easier

Expected Result

Each button press toggles the relay. The motor starts spinning when the relay turns on and stops when the relay turns off.


Troubleshooting

Problem Possible Cause Solution
Motor does not spin External supply missing or wrong voltage Check motor supply and relay load path
Pico resets or acts strangely Motor noise or wiring mistake Recheck supply wiring and separate motor power
Relay works but motor is weak Supply current too low Use a supply that matches motor requirements