This is my code for one sensor, only for testingen if it works with the adress of the sensor (0x38)
- Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
void setup() {
Serial.begin(9600);
Serial.println("Adafruit AHT10/AHT20 demo!");
if (! aht.begin(&Wire, 0x38)) {
Serial.println("Could not find AHT? Check wiring");
while (1) delay(10);
}
Serial.println("AHT10 or AHT20 found");
}
void loop() {
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" degrees C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println("% rH");
delay(500);
}
but when i change the adress into something else like (0x40), the code still works...
I have looked in the libaray code <Adafruit_AHTX0.h> and i think that the following the part is the part I need, only I don't know how it works ...
- Code: Select all | TOGGLE FULL SIZE
/*!
* @brief Sets up the hardware and initializes I2C
* @param wire
* The Wire object to be used for I2C connections.
* @param sensor_id
* The unique ID to differentiate the sensors from others
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_AHTX0::begin(TwoWire *wire, int32_t sensor_id) {
delay(20); // 20 ms to power up
if (i2c_dev) {
delete i2c_dev; // remove old interface
}
i2c_dev = new Adafruit_I2CDevice(AHTX0_I2CADDR_DEFAULT, wire);
if (!i2c_dev->begin()) {
return false;
}
uint8_t cmd[3];
cmd[0] = AHTX0_CMD_SOFTRESET;
if (!i2c_dev->write(cmd, 1)) {
return false;
}
delay(20);
cmd[0] = AHTX0_CMD_CALIBRATE;
cmd[1] = 0x08;
cmd[2] = 0x00;
if (!i2c_dev->write(cmd, 3)) {
return false;
}
while (getStatus() & AHTX0_STATUS_BUSY) {
delay(10);
}
if (!(getStatus() & AHTX0_STATUS_CALIBRATED)) {
return false;
}
humidity_sensor = new Adafruit_AHTX0_Humidity(this);
temp_sensor = new Adafruit_AHTX0_Temp(this);
return true;
}