Other Arduino Codes and Videos by Robojax

Display Temperature from HTU21D as bargraph on LCD

دروس آردوینو به فارسی

Display Temperature from HTU21D as bargraph on LCD1602-I2C with Arduino

This displays temperature as bargraph on LCD1602-I2C or LCD2004-I2C

Related HTU21DF Videos

-Basic code using HTU21DF Humidity and Temperature Sensor with Arduino
-Custom code using HTU21DF Humidity and Temperature Sensor with Arduino
-Using 2 more HTU21DF Humidity and Temperature Sensor with Arduino
-LCD1602 or LCD2004 with HTU21DF Humidity and Temperature Sensor with Arduino
-Display Temperature from HTU21D as bargraph on LCD with Arduino

Resources for this sketch


  /* 
 *  Display Temperature from HTU21D(F) on LCD as bargraph
 *  Written and Update by Ahmad Shamshiri on July 18, 2019
 *  for Robojax in Ajax, Ontario, Canada
  Watch Video tutorial for this code :https://youtu.be/kpGvzNWLHuk

  Must watch: 
  1- Introduction to HTU21DF
  https://youtu.be/Q5y18rgTAhA
  2- LCD1602-I2C video: https://youtu.be/q9YC_GVHy5A
  
 *  Original Sources: 
*  LCD library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
 *  Bargraph library from https://playground.arduino.cc/Code/LcdBarGraph/
 *  
 *  


* Robojax Arduino Course on Udemy where  you get Schematic Diagram of this sketch 
 * and many other robotics related lectures and codes. Purchase the course here: http://bit.ly/rj-udemy


- (GND) to GND
+ (VDD) to 3.3V

(WARNING: do not connect + to 5V or the sensor will be damaged!)

*/

#include <Wire.h>
#include "Adafruit_HTU21DF.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();

#define maxValue 160 // is the value in celsius or farenheit set in line above

// LCD settings
#include <LiquidCrystal_I2C.h>
byte lcdNumCols = 16; // -- number of columns in the LCD
byte lcdLine = 2; // -- number of line in the LCD

LiquidCrystal_I2C lcd(0x3f,lcdNumCols,lcdLine); //0x3f is address for I2C
                  // to get I2C address, run I2C Scanner. 
                  //Link is provided (in same page as this code) at http://robojax.com/learn/arduino
// bargraph settings
#include <LcdBarGraphRobojax.h>
LcdBarGraphRobojax robojax(&lcd, 16, 0, 0);  // -- creating 16 character long bargraph starting at char 0 of line 0 (see video)



void setup()
{
  Serial.begin(9600);//initialize serial monitor
 // -- initializing the LCD
  lcd.begin();
  lcd.clear();
  lcd.print("Robojax"); 
   
 
  // Initialize the sensor (it is important to get calibration values stored on the device).

  if (htu.begin())
  {
   lcd.setCursor (0,1); //  
    lcd.print("HTU21DF Bargraph");  
  }else {
    lcd.setCursor (0,1); //  
    lcd.print("missing HTU21DF"); 
    while(1); // Pause forever.
  }
  // -- give use some time to read the text above
  delay(2000);
  lcd.clear();  
}// setup

void loop()
{
 // Robojax HTU21DF Bargraph main loop

 robojax.clearLine(1);// clear line 1 to display fresh temperature
 float T = getHTU('H');// get the temperature
 float Tgraph=T;

     if( Tgraph > maxValue){   
        Tgraph =0;
     }
        robojax.drawValue( Tgraph, maxValue);// draw the bargraph     
      // Print out the measurement:
      Serial.print("temperature: ");
      Serial.print(T,2);


      lcd.setCursor (0,1); //
      if(T< maxValue){       
       lcd.print("Temp.:"); 
       //lcd.print("Humi.:"); 
      }else{
       lcd.print("Max.:"); 
      }
      lcd.setCursor (7,1); //
      lcd.print(T); // print  
      lcd.print((char)223);// prints degree symbol
      //lcd.print("%");// prints degree symbol      
      lcd.print("C");//

 
delay(500);
}


/*
 * @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)
{
  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')
   {
    if(rel_hum<0){rel_hum =0;}//prevents it from negative value
    value = rel_hum;//return relative humidity
   }else{
    value = temp;// return Celsius
   }
   return value;
}//

   

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