Use HTU21D Temperature & Humidity Sensor

Preparation

  • Ameba x 1
  • HTU21D Sensor x 1
  • 4.7KΩ Resistancex 1

Example

HTU21D is a sensor provides digital outputs for humidity and temperature. The temperature operating range from -40℃ to 105℃ and the humidity operating range from 10%RH to 95%RH with HTU21D±2%RH. And it uses I2C interface to communicate with Ameba.

Download HTU21DF Library

Library installation can refer to the manual from Arduino official website and then add the library (.zip) to Ameba:
https://www.arduino.cc/en/Guide/Libraries#toc4

HTU21D needs to connect to Pull High4.7KΩ resistance and RTL8195 Wiring is as follows:

1

RTL8710 Wiring is as follows:

1

Open “File” -> “Examples” -> “Adafruit HTU21DF Library” -> “HTU21DFtest”, compile and upload to Ameba. Then, click “Reset” button to reboot and you can see the outputs on Serial Monitor as follows:

1

Code Reference

This example is simple. First, declare Adafruit_HTU21DF class.

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

Next, use htu.begin() in setup() to check if the HTU21DF starts up successfully.

if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
}

Now you can read the temperature and humidity data via readTemperature() and readHumidity() respectively from htu in loop().

void loop() {
  Serial.print("Temp: "); 
  Serial.print(htu.readTemperature());
  Serial.print("\t\tHum: "); 
  Serial.println(htu.readHumidity());
  delay(500);
}
Please confirm that QQ communication software is installed