In this example, you'll use the AHT20 temperature and humidity sensor to log temperature and humidity data to Adafruit IO.
- Board 3V to sensor VIN
- Board GND to sensor GND
- Board GP1 to sensor SCL
- Board GP0 to sensor SDA
Code the Adafruit IO Test
Once you've finished setting up your Pico W with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download as a zipped folder.
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
# SPDX-License-Identifier: MIT
import os
import time
import ssl
import wifi
import socketpool
import microcontroller
import board
import busio
import adafruit_requests
import adafruit_ahtx0
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
try:
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
except TypeError:
print("Could not find WiFi info. Check your settings.toml file!")
raise
try:
aio_username = os.getenv('ADAFRUIT_AIO_USERNAME')
aio_key = os.getenv('ADAFRUIT_AIO_KEY')
except TypeError:
print("Could not find Adafruit IO info. Check your settings.toml file!")
raise
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
# Initialize an Adafruit IO HTTP API object
io = IO_HTTP(aio_username, aio_key, requests)
print("connected to io")
# use Pico W's GP0 for SDA and GP1 for SCL
i2c = busio.I2C(board.GP1, board.GP0)
aht20 = adafruit_ahtx0.AHTx0(i2c)
try:
# get feed
picowTemp_feed = io.get_feed("pitemp")
picowHumid_feed = io.get_feed("pihumid")
except AdafruitIO_RequestError:
# if no feed exists, create one
picowTemp_feed = io.create_new_feed("pitemp")
picowHumid_feed = io.create_new_feed("pihumid")
# pack feed names into an array for the loop
feed_names = [picowTemp_feed, picowHumid_feed]
print("feeds created")
clock = 300
while True:
try:
# when the clock runs out..
if clock > 300:
# read sensor
data = [aht20.temperature, aht20.relative_humidity]
# send sensor data to respective feeds
for z in range(2):
io.send_data(feed_names[z]["key"], data[z])
print("sent %0.1f" % data[z])
time.sleep(1)
# print sensor data to the REPL
print("\nTemperature: %0.1f C" % aht20.temperature)
print("Humidity: %0.1f %%" % aht20.relative_humidity)
print()
time.sleep(1)
# reset clock
clock = 0
else:
clock += 1
# pylint: disable=broad-except
# any errors, reset Pico W
except Exception as e:
print("Error:\n", str(e))
print("Resetting microcontroller in 10 seconds")
time.sleep(10)
microcontroller.reset()
# delay
time.sleep(1)
print(clock)
Upload the Code and Libraries to the Pico W
After downloading the Project Bundle, plug your Pico W into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Pico W's CIRCUITPY drive.Â
- lib folder
- code.py
Your Pico W CIRCUITPY drive should look like this after copying the lib folder and the code.py file.
Add Your settings.toml File
Remember to add your settings.toml file as described in the Create Your settings.toml File page earlier in the guide. You'll need to include your CIRCUITPY_WIFI_SSID, CIRCUITPY_WIFI_PASSWORD, aio_username and aio_key in the file.
CIRCUITPY_WIFI_SSID = "your-ssid-here" CIRCUITPY_WIFI_PASSWORD = "your-ssid-password-here" aio_username = "your-aio-username-here" aio_key = "your-aio-key-here"
Run code.py
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
In the code, two feeds for Adafruit IO are created for each of the streams of data from the AHT20.
try:
# get feed
picowTemp_feed = io.get_feed("pitemp")
picowHumid_feed = io.get_feed("pihumid")
except AdafruitIO_RequestError:
# if no feed exists, create one
picowTemp_feed = io.create_new_feed("pitemp")
picowHumid_feed = io.create_new_feed("pihumid")
In the loop, every 5 minutes the AHT20's temperature and humidity measures are sent to Adafruit IO. When the data is sent, it is printed to the REPL.
# when the clock runs out..
if clock > 300:
# read sensor
data = [aht20.temperature, aht20.relative_humidity]
# send sensor data to respective feeds
for z in range(2):
io.send_data(feed_names[z]["key"], data[z])
print("sent %0.1f" % data[z])
time.sleep(1)
# print sensor data to the REPL
print("\nTemperature: %0.1f C" % aht20.temperature)
print("Humidity: %0.1f %%" % aht20.relative_humidity)
print()
time.sleep(1)
# reset clock
clock = 0
On Adafruit IO, you can add the feeds to your dashboard to view your data over time.
Page last edited January 22, 2025
Text editor powered by tinymce.