@marnibrewster/scd4x-node v1.0.15
@marnibrewster/scd4x-node
This is a version of RSmeral's scd4x-node library that is actually working on a scd4x on a raspberry pi B. Through a lot of trial and error, and looking at aphotix's raspi-node-sht31 library, I realized that when using i2c-bus
, it is not possible to use the scd4x start_periodic_measurement
functionality, regardless of which i2c-bus (promises, plain i2c + buffers, asyc, or sync) were used. Therefore, this library only utilizes the getStatus
, readSensorData
, and reset
functionality, and leaves the i2c connection up to the i2c-bus library.
This is a node.js library for Sensirion SCD40 and SCD41, the CO2, temperature, and humidity sensors.
The library exposes some of the commands supported by the SCD40 and SCD41, as documented in the official Datasheet.
Uses i2c-bus for connection to the sensor.
Usage
yarn add @marnibrewster/scd4x-node
const SCD4x = require("@marnibrewster/scd4x-node");
const scd4x = new SCD4x();
scd4x
.getStatus()
.then(() => scd4x.readSensorData().then(console.log, console.log));
API Documentation
Class SCD4x
getStatus(): Promise<boolean>
aka "get_data_ready_status" in the Sensirion docs, see page 14
Indicates whether a measurement can be read from the sensor's buffer. Should be called before readSensorData
. Throws an error if the least significant 11 bits of
the response are 0.
readSensorData(): Promise<Measurement>
aka "read_measurement" in the Sensirion docs, see page 9
Read a measurement of CO2 concentration, temperature, and humidity.
reset(): void
same name in the Sensirion docs, see page 16
Performs a factory reset of the sensor.
Troubleshooting
- Make sure that you have node and npm installed on your pi: running
node -- version
andnpm --version
should output numbers. If they do not, you need to find out how to install node on your pi. This varies based on your pi version, but "Step 2" of this tutorial was most helpful for me on my very old pi B.. I found that the Unofficial Builds on Nodejs.org were the most successful in installing. - Make sure that your sensor is wired properly
Make sure that i2c is enabled on your raspberry pi.
sudo apt-get install -y i2c-tools
sudo i2cdetect -y 1
- this should output a weird looking 'picture', and there should be an "x" at the 62 location if your sensor is wired up properly. Try
sudo i2cdetect -y 0
if this doesn't work. If both return empty, your sensor isn't wired up properly. Google the pinout for your raspberry pi, shutdown your pi, rewire it, and reboot it. If that doesn't work, try different wires and or check your soldering.
Run a simple python script to make sure that you can 'talk' to your sensor: (instructions from the link in #2, run these separately):
sudo apt-get update sudo apt-get install python3-pip pip3 install adafruit-circuitpython-scd4x nano testSensor.py
copy and paste the code from the adafruit tutorial here, then save your work (control + x, y, enter). Run the file with:
python3 testSensor.py
You should see the readout from your scd4x in your terminal. Once you have confirmed that the sensor is working properly, you can move onto troubleshooting this library.
In your pi, create a directory in
home/pi
called "Projects":mkdir Projects
and go into it:
cd Projects
Make a new directory called TestSensor:
mkdir TestSensor
and go into it:
cd TestSensor
Run
npm init
to initialize a node project where you can install this library. Run
npm install @marnibrewster/scd4x-node
to install this library and its dependencies. Create and open a file called
test.js
by runningnano test.js
node test.js
You should see the output of your sensor in this shape:
{ co2: number, temperature: number, humidity: number }