CO2 Sensor

The Adafruit SCD-40 is a photoacoustic CO₂ sensor that also measures relative humidity and temperature. It takes a new measurement approximately every 5 seconds. See the Adafruit learn guide for wiring and hardware details.

Connection

Call startCO2Sensor() inside preload(). The device is ready to use by the time setup() runs.

let sensor;

function preload() {
  sensor = startCO2Sensor(name?);
}

name string | false | null "co2"

Controls automatic event callback registration. With the default name:

function co2Changed(event) { /* new reading */ }

Pass false or null to disable and use addEventListener instead.

Methods

getCO2() number

Returns the last measured CO₂ concentration in ppm.

getHumidity() number

Returns the last measured relative humidity in %RH.

getTemperature() number

Returns the last measured temperature in °C.

Methods (BaseDevice)

connect() Promise<Response>

Opens the USB connection and sends the start command to the firmware. Calling it multiple times is safe — subsequent calls return the same promise. In p5.js you do not need to call this manually; startXxx() calls it for you inside preload().

Properties

Properties (BaseDevice)

id { type, address }

Identifies the device. type is the DeviceType enum value, address is the I2C address the device was started with.

isConnected boolean

true once the USB connection has been established and the firmware has acknowledged the device. In p5.js this is always true by the time setup() runs, because startXxx() waits for the connection inside preload().

Events

changed(event) changed

Fires approximately every 5 seconds when a new CO₂, humidity, and temperature measurement is available.

Event Argument

All event callbacks receive a single event argument. The event detail is available at event.detail and contains:

event.detail.co2 number — CO₂ concentration in ppm.

event.detail.humidity number — Relative humidity in %RH.

event.detail.temperature number — Temperature in °C.