Lesson 35: Using HTU21D Temperature Sensor | Arduino Step by Step Course
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
- Adafruit HTU21DF Library (Robojax.com) (zip)
- Adafruit HTU21DF Library (getHub)
- HTU21DF Datasheet (PDF)
- Difference between HTU21DF and HTU21D (image)
- Display Temperature from HTU21D as bargraph on LCD with Arduino
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 https://youtu.be/LyA0yAKlf9E
This code is available at http://robojax.com/course1/?vid=lecture35
with over 100 lectures Free On YouTube Watch it here http://robojax.com/L/?id=338
Get the code for the course: http://robojax.com/L/?id=339
If you found this tutorial helpful, please support me so I can continue creating
make donation using PayPal http://robojax.com/L/?id=64
* Code is available at http://robojax.com/learn/arduino
* 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 Robojax.com
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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 <https://www.gnu.org/licenses/>.
*/
/*
**************************************************
*
This is an example for the HTU21D-F Humidity & Temp Sensor
Designed specifically to work with the HTU21D-F sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate, 2 pins are required to
interface
****************************************************/
#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.begin(9600);
Serial.println("Robojax.com");
Serial.println("HTU21D-F test");
if (!htu.begin()) {
Serial.println("Couldn't find sensor!");
while (1);
}
}
void loop() {
//Get the code for the course: http://robojax.com/L/?id=339
Serial.print(getHTU('C'));
printDegree();
Serial.println("C");
Serial.print(getHTU('F'));
printDegree();
Serial.println("F");
Serial.print(getHTU('K'));
Serial.println("K");
Serial.println(" ");
Serial.print("Humidity:");
Serial.print(getHTU('H'));
Serial.println("%");
if(getHTU('C') <81)
{
//digitalWrite(5, LOW);
}
delay(1000);
}
/*
* @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
* www.Robojax.com
*/
float getHTU(char type)
{
//Get the code for the course: http://robojax.com/L/?id=339
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
}else{
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 Robojax.com
*/
void printDegree()
{
Serial.print("\xC2");
Serial.print("\xB0");
}