This library uses the i2c-bus nodeJS package to communicate with the chip over I2C.
const { HDC302x } = require('hdc302x');
(async () => {
const sensor = new HDC302x();
// Verify connection
const mfgId = await sensor.readManufacturerId();
console.log('Manufacturer ID:', mfgId.toString(16)); // Should be '3000'
while (true) {
const { temperature, humidity } = await sensor.read();
console.log(`Temperature: ${temperature.toFixed(2)}°C, Humidity: ${humidity.toFixed(2)}%`);
await new Promise((r) => setTimeout(r, 1000));
}
})();
const { HDC302x } = require('hdc302x');
(async () => {
const sensor = new HDC302x();
// Start auto measurement at 1 measurement per second
await sensor.setAutoMode('1MPS_LP0');
while (true) {
const { temperature, humidity } = await sensor.readAuto();
console.log(`Temperature: ${temperature.toFixed(2)}°C, Humidity: ${humidity.toFixed(2)}%`);
await new Promise((r) => setTimeout(r, 1000));
}
// To stop auto mode:
// await sensor.setAutoMode('EXIT_AUTO_MODE');
})();
const { HDC302x } = require('hdc302x');
(async () => {
const sensor = new HDC302x();
await sensor.setHeater('HALF_POWER');
// Check heater status
const heaterOn = await sensor.getHeater();
console.log('Heater on:', heaterOn);
// Disable heater
await sensor.setHeater('OFF');
})();
const { HDC302x } = require('hdc302x');
(async () => {
const sensor = new HDC302x();
// Set high alert at 30°C and 80% humidity
await sensor.setHighAlert(30, 80);
// Set low alert at 10°C and 20% humidity
await sensor.setLowAlert(10, 20);
// Check alert status
const highAlert = await sensor.getHighAlert();
const lowAlert = await sensor.getLowAlert();
console.log('High alert:', highAlert, 'Low alert:', lowAlert);
})();
| Option |
Default |
Description |
i2cBusNumber |
1 |
I2C bus number |
address |
0x44 |
I2C address (0x44, 0x45, 0x46, or 0x47) |
| Mode |
Description |
0.5MPS_LP0 - 0.5MPS_LP3 |
0.5 measurements per second |
1MPS_LP0 - 1MPS_LP3 |
1 measurement per second |
2MPS_LP0 - 2MPS_LP3 |
2 measurements per second |
4MPS_LP0 - 4MPS_LP3 |
4 measurements per second |
10MPS_LP0 - 10MPS_LP3 |
10 measurements per second |
EXIT_AUTO_MODE |
Exit auto measurement mode |
LP0-LP3 indicate low power modes, with LP3 being the lowest power consumption.
| Level |
Description |
OFF |
Heater disabled |
QUARTER_POWER |
25% heater power |
HALF_POWER |
50% heater power |
FULL_POWER |
100% heater power |