Built-in project · Raspberry Pi Pico

MicroPython relay and motor on a Pico: a button toggles it

MicroPython on a Raspberry Pi Pico: each press of the button on GP15 flips a relay through a transistor on GP14, and the relay runs a DC motor. The whole build, a Raspberry Pi Pico and 12 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $11.05. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Toggling an output on each button press
  • Switching a relay coil with a transistor
  • A flyback diode across a coil
  • Debouncing a button in software
  • Powering a motor from a separate supply
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Raspberry Pi Pico
  • Parts: 14

How it works

A Raspberry Pi Pico runs MicroPython. Each press of the button on GP15 flips a relay, and the relay's contact starts or stops a motor. The button has a 10k pull-up to 3.3 V, so the pin reads 1 until you press it.

GP14 cannot drive a relay coil directly. It goes through a 1k resistor to a transistor, and the transistor switches the coil. A diode across the coil absorbs the kick when it lets go, and an LED lights with the coil. The relay and motor run from a separate battery and MB102 power module.

The program prints motor on or motor off at each press.

Kits that can build it

This starter kit has every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 1k Ω pin 1Raspberry Pi Pico pin GP14
Resistor, 10k Ω (2) pin 1Raspberry Pi Pico pin GP15
Transistor (NPN or PNP) pin BResistor, 1k Ω pin 2
DPDT relay pin COIL2Transistor (NPN or PNP) pin C
Diode pin ATransistor (NPN or PNP) pin C
LED, red pin CTransistor (NPN or PNP) pin C
Transistor (NPN or PNP) pin EGround
Resistor, 10k Ω (1) pin 13.3 V
Pushbutton pin 1aResistor, 10k Ω (1) pin 2
Resistor, 10k Ω (2) pin 2Resistor, 10k Ω (1) pin 2
Pushbutton pin 2aGround
Breadboard power supply, MB102 pin VINBattery: AA, coin cell, LiPo pin V+
Battery: AA, coin cell, LiPo pin V-Ground
Breadboard power supply, MB102 pin GNDGround
DPDT relay pin COIL1Breadboard power supply, MB102 pin 5V
DPDT relay pin COM1Breadboard power supply, MB102 pin 5V
Diode pin CBreadboard power supply, MB102 pin 5V
Resistor, 220 Ω pin 1Breadboard power supply, MB102 pin 5V
Breadboard power supply, MB102 pin BOT+Breadboard power supply, MB102 pin TOP+
Breadboard power supply, MB102 pin BOT-Breadboard power supply, MB102 pin TOP-
DC motor pin 1DPDT relay pin NO1
DPDT relay pin COM2DPDT relay pin NC1
DPDT relay pin NO2DPDT relay pin NC1
DPDT relay pin NC2DPDT relay pin NC1
LED, red pin AResistor, 220 Ω pin 2
DC motor pin 2Ground

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# A relay and a motor: each press of the button on GP15 flips the relay,
# which GP14 drives through an S8050, and the relay's contact runs the
# motor from the power module's 5 V. The LED lights with the relay's coil.
from machine import Pin
import time

relay = Pin(14, Pin.OUT, value=0)
button = Pin(15, Pin.IN)

while True:
    if button.value() == 0:
        time.sleep_ms(20)
        if button.value() == 0:
            relay.toggle()
            print("motor", "on" if relay.value() else "off")
            while button.value() == 0:
                time.sleep_ms(10)
    time.sleep_ms(10)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The relay clicks twice or flickers on one press This project
    Mechanical buttons bounce. The code checks again after 20 ms and then waits for release, so check that the button's wiring is sound.
  • Nothing happens This project
    The transistor's emitter must go to ground and the Pico's ground must be shared with the power module.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
    Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong.
  • The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
    Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.