Wiring the QT Py ESP32-S2 CO2 Monitor

Use a STEMMA QT cable to connect a STEMMA SCD40 board to the QT Py ESP32-S2.

3D Printed Case

You can refer to the 3D Printing page in the Disconnected CO2 Data Logger guide to print a case with mounting holes for the SCD40.

Coding the QT Py ESP32-S2 CO2 Monitor

First, setup your QT Py ESP32-S2 with CircuitPython. Then, 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 time
import json
import digitalio
import supervisor
import board
import rtc
import socketpool
import wifi
import adafruit_ntp
from adafruit_azureiot import IoTHubDevice
import adafruit_scd4x

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

print("Connecting to WiFi...")
wifi.radio.connect(secrets["ssid"], secrets["password"])

print("Connected to WiFi!")

#  ntp clock - update tz_offset to your timezone
pool = socketpool.SocketPool(wifi.radio)
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
rtc.RTC().datetime = ntp.datetime

if time.localtime().tm_year < 2022:
    print("Setting System Time in UTC")
    rtc.RTC().datetime = ntp.datetime

    print("Year seems good, skipping set time.")

esp = None
pool = socketpool.SocketPool(wifi.radio)
# Create an IoT Hub device client and connect
device = IoTHubDevice(pool, esp, secrets["device_connection_string"])

print("Connecting to Azure IoT Hub...")

# Connect to IoT Central

print("Connected to Azure IoT Hub!")

#  setup for I2C
i2c = board.STEMMA_I2C()
#  setup for SCD40
scd4x = adafruit_scd4x.SCD4X(i2c)

#  start measuring co2 with SCD40
co2 = scd4x.CO2

#  setup boot button as input
button = digitalio.DigitalInOut(board.BUTTON)

#  clock to count down to sending data to Azure
azure_clock = 500

#  button debounce state
button_pressed = False

while True:
		#  button debouncing
        if button.value and button_pressed:
            button_pressed = False
		#  if you press boot
        if not button.value and not button_pressed:
            #  pack message
            message = {"CO2": co2,
                       "QT Connected": 1}
            #  send co2 measurement
		#  measure co2
        co2 = scd4x.CO2
		#  when the azure clock runs out
        if azure_clock > 500:
            print("getting msg")
			#  pack message
            message = {"CO2": co2,
                       "QT Connected": 1}
            print("sending json")
            print("data sent")
			#  reset azure clock
            azure_clock = 0
		#  if no clocks are running out
		#  increase counts by 1
            azure_clock += 1
		#  ping azure
    #  if something disrupts the loop, reconnect
    # pylint: disable=broad-except
    except (ValueError, RuntimeError, OSError, ConnectionError) as e:
        print("Network error, reconnecting\n", str(e))
	#  delay

Upload the Code and Libraries to the QT Py ESP32-S2

After downloading the Project Bundle, plug your QT Py ESP32-S2 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 QT Py ESP32-S2's CIRCUITPY drive. 

  • lib folder
  • code.py

Your QT Py ESP32-S2 CIRCUITPY drive should look like this after copying the lib folder and the code.py file.



You will need to create and add a secrets.py file to your CIRCUITPY drive. Your secrets.py file will need to include the following information:

secrets = {
    'ssid' : 'YOUR-SSID-HERE',
    'password' : 'YOUR-SSID-PASSWORD-HERE',
    'device_connection_string' : 'YOUR-DEVICE-CONNECTION-STRING-HERE'

You'll locate your Primary Connection String from your devices page in your IoT Hub. Make sure to refer to the Add IoT Hub Devices page in this guide to see the process for accessing the Primary Connection String.

This guide was first published on Aug 23, 2022. It was last updated on May 28, 2024.

This page (QT Py ESP32-S2 CO2 Monitor Setup) was last updated on May 28, 2024.

Text editor powered by tinymce.