Simple Temperature and Humidity Indicator with ESP8266 and HTU21D Sensor

I decide to mix up a few existing toys to make a simple temperature and humidity indicator. ESP8266 reads temperature and humidity from an HTU21D sensor, and displays the numbers on a 4-digit 7-segment LED display. Since a 4-digit display isn't wide enough for both temperature and humidity, the display cycles between temperature, humidity, and a blank state.

temperature and humidity on LED display

Hardware

Bill of materials

Wiring

peripheral peripheral pin Feather pin ESP8266 GPIO
HTU21D + 3V
HTU21D - GND
HTU21D DA SDA 4
HTU21D CL SCL 5
4-digit 5V 3V
4-digit GND GND
4-digit DIO 14 14
4-digit CLK 12 12

Breadboard placement

Although an Adafruit HUZZAH Feather is larger than a NodeMCU, the whole project can still fit on a half-size breadboard. The trick is that, the Feather is 7 columns wide on the breadboard, and does not have any pins under its LiPo battery connector on the right side. After putting the Feather closer to the left, there's just enough space to squeeze in an HTU21D board next to the LiPo connector.

board top-left pin bottom-right pin
Feather b2 h17
HTU21D j1 j4
4-digit e24 e27

Adafruit HUZZAH Feather, HTU21D breakout, and 4-digit LED display on a half-size breadboard

Arduino Code

// https://github.com/adafruit/Adafruit_HTU21DF_Library/
// commit 98e68720fe6df28d6033f9f8f54805433b0e9998
#include <Adafruit_HTU21DF.h>

// https://github.com/avishorp/TM1637/
// commit 1337247015fa3a15e7ce0aa10e2d1010b89cc3ab
#include <TM1637Display.h>

Adafruit_HTU21DF htu;
TM1637Display display(12, 14);

void
setup()
{
  Serial.begin(115200);
  Serial.println();

  if (!htu.begin()) {
    Serial.println("HTU21D not found");
    ESP.restart();
  }

  // set a low brightness to avoid disturbing sleep at night
  display.setBrightness(0);
}

void
loop()
{
  // read and display temperature
  float tem = htu.readTemperature();
  display.showNumberDecEx(static_cast<int>(tem * 100), 0x40);
  delay(500);

  // read and display humidity
  float hum = htu.readHumidity();
  display.showNumberDecEx(static_cast<int>(hum * 100), 0x40);
  delay(500);

  // print both to serial console
  Serial.printf("%2.2fC %2.2f%%\n", tem, hum);

  // display a blank state
  static uint8_t blank[4] = {0, 0, 0, 0};
  display.setSegments(blank);
  delay(500);
}

Tags: ESP8266