Thermal sensor and I2C lcd

Da raspibo.
Jump to navigation Jump to search

Simple temperature + humidity sensor displayed by a 16x2 lcd screen. Both are controlled via I2C.

This is the temperature sensor: http://www.farnell.com/datasheets/1780639.pdf

This is the I2C lcd controller: https://opencircuit.nl/ProductInfo/1000061/I2C-LCD-interface.pdf Normally any lcd display has a parallel communication protocol which requires 15 connections. By using an I2C controller we only need 4 pins, significantly reducing the circuit setup.

Since the two devices (temperature sensor and lcd controller) have different operating voltages (3V3 and 5V, respectively) we will power them with the two different power sources from the Arduino. Note: I initially connected them both to the 5V pin, and everything worked normally. Still, the datasheet for the SHT21 indicates an operating current ranging from 2.1V to 3.6V.

By default, the Arduino Nano uses A4 for I2C clock and A5 for I2C data. They can be relocated if needed.

The circuit is simple: power each device with its source and connect their I2C pins to the Arduino. Having a different address, he will be able to distinguish when communicating.

TempNano.png

The sketch is little more than an hello world. We use two specific libraries, HTU21D.h for the temperature sensor and LiquidCrystal_I2C for the I2C controller. The temperature library already knows its device's address. For the I2C controller, we know it's 0x27.

#include <Arduino.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <HTU21D.h>

HTU21D myHTU21D(HTU21D_RES_RH12_TEMP14);

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
{
     
     int counter = 0;
  while (myHTU21D.begin() != true && counter++ < 5)
  {
    delay(1000);
  }
  lcd.init();                      // initialize the lcd 
  lcd.backlight();
}


void loop()
{
  lcd.setCursor(0,0);
  lcd.print("Temp.   : ");
  lcd.print(myHTU21D.readTemperature());
  lcd.setCursor(0,1);
  lcd.print("Humidity: ");
  lcd.print(myHTU21D.readHumidity());
  delay(500);
}


End result:

Experiment m.jpg