Back to Step by Step Course by Robojax

Lesson 35: Using HTU21D Temperature Sensor | Arduino Step by Step Course

Please select other codes for this lecture from the links below.

Related or required files and link to this lesson

Part 4: Temperature Sensors

In this lesson we learn how to use HTU21D Temperature sensor to measure and display it Serial monitor. There are two other video lectures for this sensor 1-Using it with LCD 2--Using as bargraph

 * Robojax Arduino Step By Step Course
 * Part 4: Temperature Sensors
 * Lesson 35: HTU21D Temperature Sensor

 * Updated by Ahmad Shamshiri on July 13, 2019
 * in Ajax, Ontario, Canada

  Please watch video instruction here
 This code is available at
with over 100 lectures Free On  YouTube Watch it here
Get the code for the course:  
If you found this tutorial helpful, please support me so I can continue creating 
 make donation using PayPal

 * Code is available at

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <>. 

  This is an example for the HTU21D-F Humidity & Temp Sensor

  Designed specifically to work with the HTU21D-F sensor from Adafruit

  These displays use I2C to communicate, 2 pins are required to

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

// Connect Vin to 3-5VDC
// Connect GND to ground
// Connect SCL to I2C clock pin (A5 on UNO)
// Connect SDA to I2C data pin (A4 on UNO)

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

void setup() {
  Serial.println("HTU21D-F test");

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

void loop() {
//Get the code for the course:  

    Serial.println(" ");  


    if(getHTU('C') <81)
      //digitalWrite(5, LOW);

 * @brief returns temperature or relative humidity
 * @param "type" is character
 *     C = Celsius
 *     K = Keliven
 *     F = Fahrenheit
 *     H = Humidity
 * @return returns one of the values above
 * Usage: to get Fahrenheit type: getHTU('F')
 * to print it on serial monitor Serial.println(getHTU('F'));
 * Written by Ahmad Shamshiri on July 13, 2019
 * in Ajax, Ontario, Canada
float getHTU(char type)
	//Get the code for the course:  
  float value;
    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
   if(type =='F')
    value = temp *9/5 + 32;//convert to Fahrenheit 
   }else if(type =='K')
    value = temp + 273.15;//convert to Kelvin
   }else if(type =='H')
    value = rel_hum;//return relative humidity
    value = temp;// return Celsius
   return value;

 * @brief prints degree symbol on serial monitor
 * @param none
 * @return returns nothing
 * Written by Ahmad Shamshiri on JUly 13, 2019
 * for Robojax Tutorial
void printDegree()


The least I expect from you is to give the video a thumbs up and subscribe to my channel. I appreciate it. I have spent hundreds of hours making these lectures and writing code. You don't lose anything by subscribing to my channel. Your subscription is a stamp of approval for my videos, helping more people find them and, in turn, helping me. Thank you!

If you found this tutorial helpful, please support me so I can continue creating content like this. support me via PayPal

**** AFFILIATE PROGRAM **** We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites.

Right Side