Illustration: Raspberry Pi CO₂ sensor (SCD41) with Home Assistant and MQTT

AI-generated illustration

Updated 24 October 2026 By Emmanuel SAUVAGE

Raspberry Pi CO₂ sensor (SCD41) with Home Assistant and MQTT

Measure CO₂, temperature and humidity with SCD40/SCD41 on Raspberry Pi, publish via MQTT discovery, systemd service and ventilate alerts.

Also available in French.

High CO₂ in a closed bedroom or office means stale air. This tutorial uses a Raspberry Pi and SCD40 or SCD41 (photoacoustic CO₂—not a proxy) and sends readings to Home Assistant over MQTT so you can alert “open a window”.

Goal

  • Read CO₂ (ppm), temperature and humidity every ~30 s.
  • MQTT with Home Assistant discovery.
  • Run as a systemd service on boot.

Time, level, parts

TimeLevelParts
~1.5 hIntermediatePi 4/5, SCD4x breakout, MQTT broker (e.g. Mosquitto on HA)

Wiring (I2C 0x62)

Pi GPIO is 3.3 V—power the breakout at 3.3 V unless the board docs guarantee 5 V tolerant I2C.

SCD4xPi (physical pin)
VIN1 (3V3)
GND6 (GND)
SDA3 (GPIO2)
SCL5 (GPIO3)

Pi prep (Bookworm)

sudo apt update
sudo apt install -y python3-venv python3-gpiozero python3-lgpio i2c-tools
sudo raspi-config nonint do_i2c 0
# reboot, then:
i2cdetect -y 1    # expect 62
mkdir -p ~/capteurs && cd ~/capteurs
python3 -m venv --system-site-packages venv
source venv/bin/activate
pip install adafruit-circuitpython-scd4x paho-mqtt

Full script: download co2_scd41_mqtt.py.

Test: MQTT_HOST=192.168.x.x MQTT_USER=... MQTT_PASS=... python co2_scd41_mqtt.py — first reading ~5 s after start.

Home Assistant

Enable Mosquitto add-on and MQTT integration. Create a dedicated HA user for the Pi. Discovery creates entities under MQTT automatically.

Example alert above 1000 ppm for 5 minutes (adjust entity id):

alias: Living room CO2 alert
triggers:
  - trigger: numeric_state
    entity_id: sensor.raspberry_pi_scd41_living_room_co2
    above: 1000
    for: "00:05:00"
actions:
  - action: notify.notify
    data:
      message: "CO2 above 1000 ppm—ventilate."

Tip: add a derivative sensor on CO₂ to catch fast rises before a fixed threshold—see Sensirion guidance on auto-calibration needing regular fresh air.

systemd

/etc/systemd/system/co2-sensor.service with ExecStart=/home/pi/capteurs/venv/bin/python .../co2_scd41_mqtt.py, Environment=MQTT_HOST=..., then sudo systemctl enable --now co2-sensor.service.

Troubleshooting

  • No 62 on i2cdetect: SDA/SCL swap, I2C off, bad jumper.
  • CO₂ drift first days: sensor auto-calibration needs continuous run and periodic outdoor-level air (Sensirion SCD4x).
  • Temp high: move sensor away from Pi with a longer cable.

Sources: Adafruit SCD-40/41 learn guide · HA MQTT discovery.

Also: Install Home Assistant on Pi 5 · BME280 weather station.

All smart-home guides

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.