Adafruit IO is an internet of things platform (designed by Adafruit!) to help connect your project to the internet. You're going to send the Pico's CPU temperature to Adafruit IO and display it on a dashboard page. You'll add a toggle block to turn on or off the Pico's LED from the internet.

Secrets File Setup for Adafruit IO

To connect to Adafruit IO, you will need to start by modifying your secrets file to include your Adafruit IO credentials.

Navigate to and click My Key to obtain your Adafruit IO username and Active Key. Add these credentials into the secrets file, which will now look something like the following:

# This file is where you keep secret settings, passwords, and tokens!
# If you put them in the code you risk committing that info or sharing it

secrets = {
    'ssid' : 'home ssid',
    'password' : 'my password',
    'timezone' : "America/New_York", #
    'aio_username': 'MY_ADAFRUIT_IO_USERNAME',
    'aio_password': 'MY_ADAFRUIT_IO_PASSWORD'

Create Adafruit IO Feeds

Next, set up two new Adafruit IO feeds - temperature and led. The temperature feed will store the value of the Pico's CPU temperature and the LED feed will store the state of a toggle switch block on an Adafruit IO dashboard.

Create an Adafruit IO Dashboard

Navigate to the Adafruit IO Dashboards page and create a new dashboard named Pico RP2040.

Click the cog in the upper right hand corner of the dashboard to bring up the Dashboard Settings.

From the Dashboard settings, click Create New Block

Add a toggle block to turn the Pico's LED on or off.

  • From the Create a New Block screen, select the Toggle Block.
  • Connect the led feed to the block by ticking the checkbox.
  • In the final screen, click Create Block.

Next, create a gauge block to display the Pico's CPU temperature.

  • Create a new gauge block.
  • Connect the temperature feed to the block by ticking the checkbox.
  • Under Block Settings, you may change the block's look and feel.
  • When you've finished customizing the block, click Create Block.

Once you've finished creating both blocks, your dashboard should look something like the following image:


Copy the code below into Mu and save as a file named on your CIRCUITPY drive.

# SPDX-FileCopyrightText: Brent Rubell for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
from microcontroller import cpu
import board
import busio
from digitalio import DigitalInOut
from adafruit_esp32spi import adafruit_esp32spi
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from adafruit_io.adafruit_io import IO_MQTT

### WiFi ###

# Get wifi details and more from a file
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in, please add them there!")

# Raspberry Pi RP2040
esp32_cs = DigitalInOut(board.GP13)
esp32_ready = DigitalInOut(board.GP14)
esp32_reset = DigitalInOut(board.GP15)

spi = busio.SPI(board.GP10, board.GP11, board.GP12)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets)

# Configure the RP2040 Pico LED Pin as an output
led_pin = DigitalInOut(board.LED)

# Define callback functions which will be called when certain events happen.
# pylint: disable=unused-argument
def connected(client):
    # Connected function will be called when the client is connected to Adafruit IO.
    print("Connected to Adafruit IO! ")

def subscribe(client, userdata, topic, granted_qos):
    # This method is called when the client subscribes to a new feed.
    print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))

# pylint: disable=unused-argument
def disconnected(client):
    # Disconnected function will be called when the client disconnects.
    print("Disconnected from Adafruit IO!")

def on_led_msg(client, topic, message):
    # Method called whenever user/feeds/led has a new value
    print("New message on topic {0}: {1} ".format(topic, message))
    if message == "ON":
        led_pin.value = True
    elif message == "OFF":
        led_pin.value = False
        print("Unexpected message on LED feed.")

# Connect to WiFi
print("Connecting to WiFi...")

# Initialize MQTT interface with the esp interface
MQTT.set_socket(socket, esp)

# Initialize a new MQTT Client object
mqtt_client = MQTT.MQTT(

# Initialize an Adafruit IO MQTT Client
io = IO_MQTT(mqtt_client)

# Connect the callback methods defined above to Adafruit IO
io.on_connect = connected
io.on_disconnect = disconnected
io.on_subscribe = subscribe

# Set up a callback for the led feed
io.add_feed_callback("led", on_led_msg)

# Connect to Adafruit IO
print("Connecting to Adafruit IO...")

# Subscribe to all messages on the led feed

prv_refresh_time = 0.0
while True:
    # Poll for incoming messages
    except (ValueError, RuntimeError) as e:
        print("Failed to get data, retrying\n", e)
    # Send a new temperature reading to IO every 30 seconds
    if (time.monotonic() - prv_refresh_time) > 30:
        # take the cpu's temperature
        cpu_temp = cpu.temperature
        # truncate to two decimal points
        cpu_temp = str(cpu_temp)[:5]
        print("CPU temperature is %s degrees C" % cpu_temp)
        # publish it to io
        print("Publishing %s to temperature feed..." % cpu_temp)
        io.publish("temperature", cpu_temp)
        prv_refresh_time = time.monotonic()

Once all the files are copied from your computer to the Pico, you should have the following files on your CIRCUITPY drive.

Code Usage

The code should connect to your wireless network and Adafruit IO. 

Connecting to WiFi...
Connecting to Adafruit IO...
Connected to Adafruit IO!
Subscribed to brubell/f/led with QOS level 0
CPU temperature is 24.32 degrees C
Publishing 24.32 to temperature feed...

Navigate to your Adafruit IO Dashboard and you should see a new value in the gauge block. Every 30 seconds, the Pico reads its internal CPU temperature and sends it to Adafruit IO. 

Toggle the switch block to turn the Pico's LED on or off:

This guide was first published on Mar 05, 2021. It was last updated on 2021-03-05 17:17:51 -0500.

This page (Usage with Adafruit IO) was last updated on Apr 20, 2021.

Text editor powered by tinymce.