10. Input Devices

This week I worked on testing a sensor which could be a potential sensor that I might use in my final project. The sensor I used is DHT11 (3 pins). This sensor is used to measure relative humidity and temperature.

In this tutorial, I will show you first how I connected and programmed the sensor on its own. Then I will show how I connected the lcd. And finally I will illustrate how I was able to show my readings from the dht11 on the lcd.

I. DHT11

The Wiring

I first followed the method of wiring the 3 pin DHT11 sensor. The wiring of the 3 pin differs a bit from the 4 pin sensor so I had to be careful which one is mine.

I then connected it as shown below

The Code

At first I wanted a simple code that let me read the temperature and humidity of the weather.

#include <LiquidCrystal_I2C.h>

#include <dht.h>

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}

The Readings

After connecting the sensor and uploading the code, I started getting the readings of temperature and humidity. To be able to see them you go to Tools > Serial Monitor

You can have a different way of readings by choosing Serial Plotter. Go to Tool > Serial Plotter

II. LCD

In my Arduino kit I found this lcd so I got intrigued to try to show the results on it. My LCD is a 16x2 LCD; this means that it is able to display 2 lines with 16 characters in each at once.

The Wiring

There are several ways to connect LCDs to the Arduino. However, with the model I have, I was only able to connect it through jumpers to the arduino directly. I wasn’t able to fix my LCD on the breadboard as it didn’t have pins on the back of it.

The Code and Screen Display

In order to make the LCD work, I ran two codes.

  • The first is to identify the LCD. It was as follow:

/*I2C_scanner
  This sketch tests standard 7-bit addresses.
  Devices with higher bit address might not be seen properly.*/

#include <Wire.h>
void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);
  Serial.println("\nI2C Scanner");
}
void loop() {
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for (address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);
      Serial.println("  !");
      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
  delay(5000);
}
  • The second code was to show characters on the screen.

/* I2C LCD with Arduino example code. More info: https://www.makerguides.com */
// Include the libraries:
// LiquidCrystal_I2C.h: https://github.com/johnrickman/LiquidCrystal_I2C
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,16,2) for 16x2 LCD.
void setup() {
  // Initiate the LCD:
  lcd.init();
  lcd.backlight();
}
void loop() {
  // Print 'Hello World!' on the first line of the LCD:
  lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
  lcd.print("Hello World!"); // Print the string "Hello World!"
  lcd.setCursor(2, 1); //Set the cursor on the third column and the second row (counting starts at 0!).
  lcd.print("LCD tutorial");
}

III. DHT11 with LCD

In this part, if you are reading my tutorial I highly recommend you don’t follow my steps!! This is simply because I took the long road to do it. It is due to the unavailability of some items like the female to female jumpers. Which allows me to just add the DHT11 to the Arduino immediately in addition to the previously connected LCD.

So what I did was using the breadboard, to connect the dht11 and the LCD to the Arduino.

The Wiring

Below is an illustration of how I made my connections

The Code

I used the following code to program my sensor and screen.

//infinite Xpro
// firstly need to add i2c library

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
byte degree_symbol[8] =
              {
                0b00111,
                0b00101,
                0b00111,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000
              };
int gate=11;
volatile unsigned long duration=0;
unsigned char i[5];
unsigned int j[40];
unsigned char value=0;
unsigned answer=0;
int z=0;
int b=1;
void setup()
{
 lcd.init();                       // initialize the lcd
 lcd.init();
 lcd.backlight();
 lcd.print("Temp = ");
 lcd.setCursor(0,1);
 lcd.print("Humidity = ");
 lcd.createChar(1, degree_symbol);
 lcd.setCursor(9,0);
 lcd.write(1);
 lcd.print("C");
 lcd.setCursor(13,1);
 lcd.print("%");
}

void loop()
{

 delay(1000);
 while(1)
 {
  delay(1000);
  pinMode(gate,OUTPUT);
  digitalWrite(gate,LOW);
  delay(20);
  digitalWrite(gate,HIGH);
  pinMode(gate,INPUT_PULLUP);//by default it will become high due to internal pull up
 // delayMicroseconds(40);


  duration=pulseIn(gate, LOW);
  if(duration <= 84 && duration >= 72)
  {
      while(1)
      {
        duration=pulseIn(gate, HIGH);

        if(duration <= 26 && duration >= 20){
        value=0;}

        else if(duration <= 74 && duration >= 65){
        value=1;}

        else if(z==40){
        break;}

        i[z/8]|=value<<(7- (z%8));
        j[z]=value;
        z++;
      }
    }
answer=i[0]+i[1]+i[2]+i[3];

if(answer==i[4] && answer!=0)
{
lcd.setCursor(7,0);
lcd.print(i[2]);
lcd.setCursor(11,1);
lcd.print(i[0]);
}

z=0;
i[0]=i[1]=i[2]=i[3]=i[4]=0;
 }
}

The Result

Here is the final result.