Project 1.8.8: Online Motor Controller
Beginner Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Pico 2 W Diagram
Overview
Build a browser-controlled DC motor project using a relay module and external power.
This project demonstrates safe switching of a motor from a web page.
The final result should open a web page with START and STOP buttons that control the motor state.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() 1-channel relay module |
![]() Small DC motor |
![]() Breadboard |
![]() 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 |
| External motor supply positive | Relay COM | Not a GPIO pin | Motor power input |
| Relay NO | Motor positive terminal | Not a GPIO pin | Motor gets 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

Step 2: Place the Relay Module and Motor
Keep the motor and external supply separate from the Pico wiring.

Step 3: Connect Relay Power
Connect relay VCC to 5V / VSYS and relay GND to GND.

Step 4: Connect the Relay IN Pin
Connect relay IN to GPIO 0.

Step 5: Wire the Motor Power Through the Relay
Connect external motor supply positive to relay COM, relay NO to motor positive, and motor negative to external supply negative.

Testing Individual Components
Relay Click Test
from machine import Pin
import time
relay = Pin(0, Pin.OUT)
relay.value(1)
time.sleep(1)
relay.value(0)
print('Relay ON if active-low')
time.sleep(1)
relay.value(1)
print('Relay OFF')
Wi-Fi Connection Test
import network
import time
SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(15):
if wlan.isconnected():
break
print('Connecting...')
time.sleep(1)
print('Connected:', wlan.isconnected())
if wlan.isconnected():
print('IP address:', wlan.ifconfig()[0])
Full Project Code
import network
import socket
import time
from machine import Pin
SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'
relay = Pin(0, Pin.OUT)
relay.value(1)
motor_on = False
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print('Connecting to Wi-Fi...')
for _ in range(15):
if wlan.isconnected():
break
time.sleep(1)
if not wlan.isconnected():
raise RuntimeError('Wi-Fi connection failed')
ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))
def web_page(is_on):
state = 'RUNNING' if is_on else 'STOPPED'
return '''<!DOCTYPE html>
<html>
<head><meta name='viewport' content='width=device-width, initial-scale=1'><title>Motor Control</title></head>
<body style='font-family:Arial;text-align:center;padding:40px'>
<h1>Online Motor Control</h1>
<p>Motor state: STATE_TEXT</p>
<a href='/start'><button style='padding:16px 28px;font-size:20px'>START</button></a>
<a href='/stop'><button style='padding:16px 28px;font-size:20px'>STOP</button></a>
<p>Use only when the motor area is clear.</p>
</body>
</html>'''.replace('STATE_TEXT', state)
address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)
while True:
client, client_address = server.accept()
print('Client connected from', client_address)
request = client.recv(1024).decode()
if 'GET /start' in request:
motor_on = True
relay.value(0)
print('Motor STARTED')
elif 'GET /stop' in request:
motor_on = False
relay.value(1)
print('Motor STOPPED')
response = web_page(motor_on)
client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
client.sendall(response.encode())
client.close()
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
relay.value(1) |
Starts relay OFF for most active-low modules | Motor should not start unexpectedly |
motor_on |
Stores current motor state | Page shows whether motor is running |
/start and /stop |
Browser commands change relay state | Controls motor remotely |
| Relay hardware | Switches external motor power | Protects Pico from motor current |
Expected Result
Opening the Pico IP address in a browser shows START and STOP buttons that control the motor through the relay.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Motor does not spin | External supply or load wiring issue | Check motor supply, relay COM, relay NO, and motor wires |
| Motor runs all the time | Active-low logic misunderstood | Confirm relay.value(0) means ON for your module |
| Relay clicks but motor does not move | Motor wiring or supply current issue | Test the motor with the external supply |




