Pico W fan motor both ways on a motor driver
MicroPython on a Pico W: GP14 and GP15 drive a DC motor through a two-input motor driver, a second forward, a stop, a second backward. The whole build, a Raspberry Pi Pico W and 2 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $8.20. On Mokxi they're free.
What you'll learn
- Why a motor needs a driver
- Direction control with two pins
- Stopping by setting both inputs low
- Wrapping repeated steps in a function
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Raspberry Pi Pico W
- Parts: 4
How it works
The L9110S motor driver lets two small pins control a motor. GP14 goes to input AIA and GP15 goes to AIB. The motor connects to the driver's outputs OA1 and OA2.
With GP14 high and GP15 low, the motor turns one way. With them the other way around, it turns the other way. With both low, it stops. The program runs forward, stop, backward, stop, each for one second.
Parts list
4 parts, plus the jumper wires. They retail for about $8.20; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Motor driver, L9110S
- 1 × DC motor
- 1 × Power, 3.3 V
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 pin | Connects to |
|---|---|
| Motor driver, L9110S pin AIA | Raspberry Pi Pico W pin GP14 |
| Motor driver, L9110S pin AIB | Raspberry Pi Pico W pin GP15 |
| Motor driver, L9110S pin GND | Ground |
| Motor driver, L9110S pin VCC | 3.3 V |
| DC motor pin 1 | Motor driver, L9110S pin OA1 |
| DC motor pin 2 | Motor driver, L9110S pin OA2 |
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.
# Small fan: the motor driver turns the motor one way when GP14 is high
# and GP15 low, the other way the other way around, and stops it when both
# are low. A second each way, with a stop between.
from machine import Pin
import time
a = Pin(14, Pin.OUT)
b = Pin(15, Pin.OUT)
def drive(x, y, what):
a.value(x)
b.value(y)
print(what)
time.sleep(1)
while True:
drive(1, 0, "forward")
drive(0, 0, "stop")
drive(0, 1, "backward")
drive(0, 0, "stop")
Code walkthrough
The key functions of the sketch, and what each one does.
drive main.py
Takes two pin levels and a label. It sets pin a (GP14) and pin b (GP15) to those levels, prints the label, and waits one second. The main loop calls it four times to go forward, stop, go backward and stop.
def drive(x, y, what):
a.value(x)
b.value(y)
print(what)
time.sleep(1)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The motor never spins. This project
Check that the motor is on OA1 and OA2, and that the driver has VCC on 3.3 V and GND on ground. - The motor only turns one way. This project
Check both GP14 to AIA and GP15 to AIB. Both pins are needed for the two directions. - The motor does not turn or stops quickly when both inputs are the same. Motor driver
Each output follows its input. Set one input high and the other low to turn, swap them to reverse. Equal inputs brake the motor rather than coast. Use PWM on one input for speed. - The driver gets hot or the board resets. Motor driver
Power the motors from a separate supply of about 2.5 to 12 V, within the chip's rating, and share ground with the controller. Small motors only: stalled or large motors exceed its roughly 800 mA limit. - The motor does not spin, or the pin gets hot or the board resets when it is wired straight to a GPIO pin. DC motor
A pin gives about 20 mA and a motor needs far more. Drive it through a transistor or motor driver, with its own supply and a shared ground. - The transistor or pin is damaged by spikes when the motor stops. DC motor
Put a flyback diode across the motor, cathode to the positive side. A motor is a coil and kicks back voltage when switched off. Add a 0.1 uF capacitor across the motor terminals to cut noise. - Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts. - WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.