Built-in project · Arduino Uno R3

HTU21D temperature and humidity on an Arduino Uno

A GY-21 HTU21D board on the Uno’s I2C pins, read with Adafruit_HTU21DF.h: the serial monitor prints the temperature and humidity every second. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Sensors
  • School
  • Using the Temperature and humidity
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 2

How it works

A GY-21 HTU21D board on the Uno’s I2C pins, read with Adafruit_HTU21DF.h: the serial monitor prints the temperature and humidity every second.

Arduino Uno R3 runs the sketch below. The circuit around it: Temperature and humidity, HTU21D. delay waits 1000 milliseconds. Serial.begin opens the serial port at 9600 baud.

Parts list

2 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Temperature and humidity, HTU21D pin VINArduino Uno R3 pin 5V
Temperature and humidity, HTU21D pin GNDArduino Uno R3 pin GND
Temperature and humidity, HTU21D pin SDAArduino Uno R3 pin A4
Temperature and humidity, HTU21D pin SCLArduino Uno R3 pin A5

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// An HTU21D temperature and humidity sensor on the GY-21 board.
//
// The HTU21D answers at 0x40. Each reading is a command, a wait while it
// measures (up to 50 ms for a 14-bit temperature, 16 ms for a 12-bit
// humidity) and three bytes back: two of data and a CRC that
// Adafruit_HTU21DF checks for you.
//
//   GY-21  VIN -> 5V, GND -> GND, SCL -> A5, SDA -> A4
//
// The board has its own 3.3 V regulator and pull-ups, so 5 V on VIN is fine.
// Every second the sketch prints both readings on the serial monitor. Move
// the temperature and humidity sliders while it runs.
//
// THINGS TO TRY. Unplug SDA: begin() gets no answer at 0x40 and the sketch
// says so. Then work out the dew point from the two readings.

#include <Wire.h>
#include "Adafruit_HTU21DF.h"

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.begin(9600);
  if (!htu.begin()) {
    Serial.println("No HTU21D at 0x40: check SDA, SCL and power.");
    while (1) {}
  }
  Serial.println("HTU21D found at 0x40");
}

void loop() {
  float temp = htu.readTemperature();
  float humidity = htu.readHumidity();
  Serial.print("temperature ");
  Serial.print(temp, 2);
  Serial.print(" C, humidity ");
  Serial.print(humidity, 1);
  Serial.println(" %RH");
  delay(1000);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

void setup() {
  Serial.begin(9600);
  if (!htu.begin()) {
    Serial.println("No HTU21D at 0x40: check SDA, SCL and power.");
    while (1) {}
  }
  Serial.println("HTU21D found at 0x40");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

void loop() {
  float temp = htu.readTemperature();
  float humidity = htu.readHumidity();
  Serial.print("temperature ");
  Serial.print(temp, 2);
  Serial.print(" C, humidity ");
  Serial.print(humidity, 1);
  Serial.println(" %RH");
  delay(1000);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.