I2C で複数のデバイスを扱う - 温湿度センサーと距離センサーの例
import { requestI2CAccess } from "node-web-i2c";
import SHT30 from "@chirimen/sht30";
import VL53L0X from "@chirimen/vl53l0x";
async function main() {
const i2cAccess = await requestI2CAccess();
const port = i2cAccess.ports.get(1);
const sht30 = new SHT30(port, 0x44);
const vl53l0x = new VL53L0X(port, 0x29);
await sht30.init();
await vl53l0x.init();
while (true) {
const { humidity, temperature } = await sht30.readData();
const distance = await vl53l0x.getRange();
const message = [
`${temperature.toFixed(2)} ℃`,
`${humidity.toFixed(2)} %`,
`${distance} mm`,
].join(", ");
console.log(message);
await sleep(500);
}
}
main();
SHT30 と VL53L0X を並列接続し、実行します