~derf / projects / ESP8266/NodeMCU SCD4x driver
dark mode

ESP8266 Lua/NodeMCU module for Sensirion SCD40/SCD41 CO₂ sensors

esp8266-nodemcu-scd4x contains an ESP8266 NodeMCU Lua module (scd4x.lua) as well as MQTT / HomeAssistant / InfluxDB integration example (init.lua) for Sensirion SCD40/SCD41 CO₂ sensors.


scd4x.lua has been tested with Lua 5.1 on NodeMCU firmware 3.0.1 (Release 202112300746, integer build). It requires the following modules.

  • i2c

The MQTT HomeAssistant integration in init.lua additionally needs the following modules.

  • gpio
  • mqtt
  • node
  • tmr
  • uart
  • wifi


Copy scd4x.lua to your NodeMCU board and set it up as follows.

scd4x = require("scd4x")
i2c.setup(0, sda_pin, scl_pin, i2c.SLOW)

-- can be called with up to 1 Hz
function some_timer_callback()
    local co2, raw_temp, raw_humi = scd4x.read()
    if co2 == nil then
        print("SCD4x error")
        -- CO₂[ppm] == co2, Temperature[°c] == raw_temp/2¹⁶ - 45, Humidity[%] == raw_humi/2¹⁶

See init.lua for an example. To use it, you need to create a config.lua file with WiFI and MQTT settings:

station_cfg.ssid = "..."
station_cfg.pwd = "..."
mqtt_host = "..."