Raspberry Pi Pico W MQTT Remote Control: Wiring, Code and Simulator
To build this Raspberry Pi Pico W MQTT Remote Control project you need a Raspberry Pi Pico W and a 220 Ω resistor, a green LED and a potentiometer. Publishes the knob to a public MQTT broker every 2 s and switches the LED on GP15 when "on" or "off" arrives on its topic. Make the 6 connections in the wiring table, upload the MicroPython code, and open the HiveMQ web client (hivemq.com/demos/websocket-client), subscribe to the pot topic, and publish "on" to the led topic.
Open this circuit in the free Raspberry Pi Pico W simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Raspberry Pi Pico W | the microcontroller board |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | green LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
| 1 | Potentiometer | VCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Raspberry Pi Pico W GP15 | 220 Ω resistor leg 1 |
| 2 | 220 Ω resistor leg 2 | green LED anode (A, long leg) |
| 3 | Raspberry Pi Pico W GND | green LED cathode (C, short leg) |
| 4 | Raspberry Pi Pico W 3V3 | Potentiometer VCC |
| 5 | Raspberry Pi Pico W AGND | Potentiometer GND |
| 6 | Raspberry Pi Pico W GP26 | Potentiometer SIG |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Raspberry Pi Pico W. No install or account is needed.
- Add the parts. Add a 220 Ω resistor, a green LED and a potentiometer from the parts palette.
- Wire 1. Connect Raspberry Pi Pico W GP15 to 220 Ω resistor leg 1.
- Wire 2. Connect 220 Ω resistor leg 2 to green LED anode (A, long leg).
- Wire 3. Connect Raspberry Pi Pico W GND to green LED cathode (C, short leg).
- Wire 4. Connect Raspberry Pi Pico W 3V3 to Potentiometer VCC.
- Wire 5. Connect Raspberry Pi Pico W AGND to Potentiometer GND.
- Wire 6. Connect Raspberry Pi Pico W GP26 to Potentiometer SIG.
- Paste the MicroPython code. Paste the MicroPython code below into the code editor.
- Run it. Press Run. Open the HiveMQ web client (hivemq.com/demos/websocket-client), subscribe to the pot topic, and publish "on" to the led topic.
MicroPython code
# Pico W + MQTT: publish the knob, and let anyone switch the LED from the internet.
# Broker: broker.hivemq.com (public; pick your own topic so it is yours).
# Try it: in the HiveMQ web client, publish "on" or "off" to bfb/<your topic>/led
import network, time
from machine import Pin, ADC
from umqtt.simple import MQTTClient
TOPIC = "bfb/demo-%d" % time.ticks_ms() # change to something only you use
led = Pin(15, Pin.OUT)
pot = ADC(26) # GP26 = ADC0, read_u16() gives 0-65535
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("BFB-Lab", "")
while not wlan.isconnected():
time.sleep_ms(200)
def on_message(topic, msg):
print("got", topic, msg)
led.value(msg == b"on")
client = MQTTClient("picow-" + TOPIC[-6:], "broker.hivemq.com")
client.set_callback(on_message)
client.connect()
client.subscribe(TOPIC + "/led")
print("publishing to", TOPIC + "/pot", "- listening on", TOPIC + "/led")
last = 0
while True:
client.check_msg()
if time.ticks_diff(time.ticks_ms(), last) > 2000:
last = time.ticks_ms()
client.publish(TOPIC + "/pot", str(pot.read_u16()))
time.sleep_ms(100)
What to try
Open the HiveMQ web client (hivemq.com/demos/websocket-client), subscribe to the pot topic, and publish "on" to the led topic.
Frequently asked questions
How do I wire the MQTT Remote Control project on a Raspberry Pi Pico W?
Make 6 connections: Raspberry Pi Pico W GP15 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to green LED anode (A, long leg); Raspberry Pi Pico W GND to green LED cathode (C, short leg); Raspberry Pi Pico W 3V3 to Potentiometer VCC; Raspberry Pi Pico W AGND to Potentiometer GND; Raspberry Pi Pico W GP26 to Potentiometer SIG.
What parts do I need for this Raspberry Pi Pico W project?
A Raspberry Pi Pico W and a 220 Ω resistor, a green LED and a potentiometer.
Can I simulate this Raspberry Pi Pico W project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and MicroPython code in your browser for free, with no install and no hardware. The Pico W’s RP2040 is emulated and runs the official MicroPython firmware; its Wi-Fi and Bluetooth chip is stood in for by a virtual network that reaches the real internet.
What does the code do?
Publishes the knob to a public MQTT broker every 2 s and switches the LED on GP15 when "on" or "off" arrives on its topic.