If you just picked up a Raspberry Pi Pico and want to connect it to the internet, you're in the right place. The RP2040 microcontroller on the Pico board costs about $4, runs MicroPython or C/C++, and has enough GPIO pins to talk to sensors, relays, and displays. But getting firmware that actually sends sensor readings to a server or responds to web requests? That's where most beginners get stuck. Working through real Raspberry Pi Pico RP2040 IoT firmware code examples for beginners is the fastest way to move from blinking an LED to building connected devices that do something useful.

What does "IoT firmware" actually mean on the Raspberry Pi Pico?

IoT firmware is the code running on your microcontroller that reads data from sensors, connects to a network, and sends or receives that data over the internet. On the Pico, this usually means using MicroPython with a network module (like the Pico W's onboard Wi-Fi) or pairing the Pico with an external ESP-01 Wi-Fi module via UART. The firmware handles three jobs: collecting data, packaging it (often as JSON), and transmitting it through a protocol like MQTT or HTTP.

The Raspberry Pi Pico W (the version with built-in Wi-Fi using the CYW43439 chip) makes this simpler because you don't need extra hardware for connectivity. If you have the original Pico without Wi-Fi, you can still build IoT projects by adding a separate Wi-Fi module we cover that approach in our ESP8266 Wi-Fi connectivity boilerplate snippets.

What hardware do you need to follow along?

Here's the minimum setup for these examples:

  • Raspberry Pi Pico W (or Pico + external Wi-Fi module)
  • Micro-USB cable for power and programming
  • A sensor a DHT22 (temperature and humidity) or a simple potentiometer works well for learning
  • Breadboard and jumper wires
  • Thonny IDE installed on your computer (free, works on Windows, Mac, and Linux)
  • A Wi-Fi network you can connect to

You'll also want MicroPython firmware flashed onto your Pico. Download the .uf2 file from the official Raspberry Pi site, hold the BOOTSEL button while plugging in the Pico, and drag the file onto the USB drive that appears.

How do you connect the Pico W to Wi-Fi with MicroPython?

Before you can send any data to the internet, the Pico needs to join your local network. Here's the basic Wi-Fi connection snippet:

import network
import time

ssid = "YourNetworkName"
password = "YourPassword"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

timeout = 10
while timeout > 0:
    if wlan.isconnected():
        break
    time.sleep(1)
    timeout -= 1

if wlan.isconnected():
    print("Connected:", wlan.ifconfig()[0])
else:
    print("Failed to connect")

This sets the Pico W into station mode, tries to connect, and prints the assigned IP address on success. Save this as boot.py or main.py on the Pico's filesystem using Thonny.

How do you read a sensor and format data as JSON?

Once online, the next step is reading sensor values and structuring them for transmission. Using a DHT22 temperature sensor wired to GPIO 15:

import dht
import machine
import json
import time

sensor = dht.DHT22(machine.Pin(15))

def read_sensor():
    sensor.measure()
    data = {
        "temperature": sensor.temperature(),
        "humidity": sensor.humidity(),
        "device": "pico-w-01"
    }
    return json.dumps(data)

while True:
    print(read_sensor())
    time.sleep(5)

This reads temperature and humidity every 5 seconds and converts the result to a JSON string. JSON is the standard format most IoT platforms and APIs expect.

How do you send sensor data over MQTT from the Pico?

MQTT is the most common protocol for IoT devices because it's lightweight and runs on low-power hardware. You'll need an MQTT broker a free public one like broker.hivemq.com works for testing, or you can run your own. Here's a complete working example:

import network
import time
import json
import dht
import machine
from umqtt.simple import MQTTClient

# Wi-Fi setup
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("YourSSID", "YourPassword")
while not wlan.isconnected():
    time.sleep(1)

# Sensor setup
sensor = dht.DHT22(machine.Pin(15))

# MQTT setup
client = MQTTClient("pico_client", "broker.hivemq.com")
client.connect()

while True:
    try:
        sensor.measure()
        payload = json.dumps({
            "temp": sensor.temperature(),
            "hum": sensor.humidity()
        })
        client.publish(b"pico/sensors", payload)
        print("Published:", payload)
    except Exception as e:
        print("Error:", e)
    time.sleep(10)

The umqtt.simple module is built into MicroPython on the Pico W no extra library installation needed. The client.publish() call sends your JSON payload to the topic pico/sensors every 10 seconds. If you want to see how similar MQTT publishing works on an ESP32, check our Arduino ESP32 MQTT publishing snippet.

Can you make the Pico respond to commands from the internet?

IoT isn't just about sending data out it's also about receiving instructions. MQTT lets you subscribe to topics and react to incoming messages. Here's how to control the Pico's onboard LED remotely:

from umqtt.simple import MQTTClient
import machine

led = machine.Pin("LED", machine.Pin.OUT)

def on_message(topic, msg):
    if msg == b"on":
        led.value(1)
    elif msg == b"off":
        led.value(0)

client = MQTTClient("pico_cmd", "broker.hivemq.com")
client.set_callback(on_message)
client.connect()
client.subscribe(b"pico/commands")

while True:
    client.check_msg()

Publish "on" or "off" to the pico/commands topic from any MQTT client (like MQTT Explorer on your desktop), and the Pico's LED responds immediately.

What are the most common mistakes beginners make?

After working with many people new to Pico IoT projects, these errors come up over and over:

  • Forgetting the Pico W uses "LED" for the onboard LED the original Pico uses Pin(25), but the Pico W's LED is on the wireless chip, addressed as "LED" as a string
  • Not handling exceptions in the main loop if the MQTT connection drops, the whole script crashes without a try/except block
  • Hard-coding Wi-Fi credentials put your SSID and password in a separate config.py file so you can share your code without leaking your network password
  • Polling sensors too fast the DHT22 needs at least 2 seconds between reads; calling it more often returns stale or error values
  • Running out of RAM the Pico has only 264 KB of SRAM. Avoid loading large JSON responses or creating too many string objects in a loop
  • Using the wrong firmware version make sure you download the Pico W-specific MicroPython firmware, not the standard Pico version, if you need the network module

How do you organize firmware code for a real project?

A single main.py file works for quick experiments, but even a small IoT project benefits from splitting code into modules:

  • config.py Wi-Fi credentials, MQTT broker address, topic names
  • connect.py reusable Wi-Fi and MQTT connection functions
  • sensors.py sensor reading and data formatting
  • main.py the main loop that ties everything together

This structure makes debugging easier and lets you reuse your networking code across different projects. You can apply the same modular approach to other microcontroller platforms too the patterns in our Pico RP2040 firmware examples follow this layout.

How do you debug IoT firmware when the Pico is headless?

When your Pico is deployed away from your computer, debugging gets harder. A few strategies that help:

  1. Use Thonny's remote shell connect over the network once the Pico has an IP address
  2. Add status LEDs blink different patterns for "connecting to Wi-Fi," "sensor read error," or "MQTT disconnected"
  3. Log to a file write error messages to log.txt on the Pico's flash storage using open("log.txt", "a")
  4. Send heartbeat messages publish a small "alive" message to MQTT every few minutes so you know the device is running
  5. Watch for watchdog resets the Pico's machine.WDT can automatically reboot the board if the firmware hangs

For styling your project documentation or dashboards, you might also want a clean monospace font like Inconsolata for displaying code snippets.

Where should you go from here?

Once you have basic sensor publishing and command receiving working, you can expand your project in several directions:

  • Add more sensors soil moisture, light level, air quality (BME680)
  • Connect to a dashboard platforms like Adafruit IO, ThingsBoard, or Node-RED can visualize your data
  • Add deep sleep use machine.deepsleep() to run on batteries for weeks
  • Secure your MQTT connection use TLS encryption and authentication on your broker
  • Build a local web server serve a simple HTML page from the Pico itself using socket

Beginner checklist for your first Pico IoT project

  • Flash MicroPython Pico W firmware onto your board
  • Connect to Wi-Fi and print the IP address
  • Read a sensor and print the values to the console
  • Format sensor data as JSON
  • Publish JSON data to an MQTT topic
  • Subscribe to a command topic and control an LED
  • Wrap your main loop in try/except for error handling
  • Move credentials into a separate config file
  • Add a heartbeat or watchdog timer for reliability

Start with the Wi-Fi connection snippet above, get it printing an IP address, and build up one piece at a time. Each small win teaches you something the next step depends on. If you get stuck on any example, break it down: test the sensor alone, test the Wi-Fi alone, then combine them. That incremental approach beats trying to write the entire firmware in one go.