Raspberry Pi Pico W and SCD4X sensor
Connecting a SCD4X sensor (temperature, humidity, CO2)* to a Rpi Pico 2W and publish the measurements to an MQTT broker. I used the MicroPython_SCD4X driver from peter-l5, which is based on the adafruit_scd4x.py.
Steps:
- Download the .uf2 firmware from MicroPython
- Connect the Pico via USB and drag .uf2 to RPI-RP2 drive (flashes the firmware and reboots the Pico)
- Access Pico via CLI (or use an IDE like Thonny).
- On Linux OS:
apt install picocom picocom /dev/ttyACM0 -b 115200to start the MicroPython REPL on the Pico- Check if install works:
>>> import machine >>> led = machine.Pin("LED", machine.Pin.OUT) >>> led.toggle()
- On Linux OS:
- Upload the MicroPython MQTT client library simple.py and robust.py
- via Thonny:
- Open the Files pane (View → Files).
- Navigate to the Pico device.
- Right-click → New Directory → name it: "umqtt"
- Upload simple.py and robust.py into that folder
- via Thonny:
- Add a configuration file (config.py) and create variables for Wifi and MQTT connections:
wifi_ssid= local WIFI namewifi_password= local WIFI passwordmqtt_server= the server address running the MQTT brokermqtt_topic= the MQTT topic to publish the sensor readings tomqtt_username= MQTT user (optional, depending on MQTT setup)mqtt_password= MQTT password -"-
- Connect the sensor to the Pico and upload the main.py to the device in order to read/publish the sensor data (via MQTT)
Useful resources:
- Rpi Pico W-Series Documentation
- Rpi Pico GPIO-Connections diagram
- SCD4X sensor datasheets
- Mosquitto (Open-source MQTT broker)
Additional helper scripts:
- I2Cscan.py checks if sensor can be found on I2C bus
- sample_read_sensor_data.py simple script to test read sensor data
* I used an SCD41 sensor from SEED-Studio