Topic 5. Arduino LCD screen

An Arduino 1602 LCD screen, also known as a 16×2 LCD (Liquid Crystal Display), is a common display module used with Arduino microcontrollers. It features a display area of 16 characters in each of the two rows, hence the name “16×2”.

Each character is typically a 5×8 dot matrix, allowing the display of alphanumeric characters, symbols, and custom characters. The LCD screen is usually interfaced with the Arduino using parallel communication. This means it requires several digital pins (usually 6 to 8 pins) for data and control signals. However, there are also I2C/SPI backpack modules available that reduce the pin count by using serial communication protocols.

Building

What do you need

1 x LCD 1602
2 x 220 Ω Resistor
1 x Temperature Sensor [TMP36]
1 x 250 kΩ Potentiometer

Tinkercad

Our LCD display will show three functions (function can be changed with potentiometer):

  1. My site ad
  2. Current room temperature
  3. Current date and time
Arduino code
#include <LiquidCrystal.h>
#include <TimeLib.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte housechar1[8] = {B00000, B00001, B00011, B00011, B00111, B01111, B01111, B11111};
byte housechar2[8] = {B11111, B11111, B11100, B11100, B11100, B11100, B11100, B11100};
byte housechar3[8] = {B00000, B10010, B11010, B11010, B11110, B11110, B11110, B11111};
byte housechar4[8] = {B11111, B11111, B11111, B10001, B10001, B10001, B11111, B11111};

byte tempchar1[8] = {B00000, B00001, B00010, B00100, B00100, B00100, B00100, B00111};
byte tempchar2[8] = {B00111, B00111, B00111, B01111, B11111, B11111, B01111, B00011};
byte tempchar3[8] = {B00000, B10000, B01011, B00100, B00111, B00100, B00111, B11100};
byte tempchar4[8] = {B11111, B11100, B11100, B11110, B11111, B11111, B11110, B11000};

const int temperaturePin = A0;

int sensorPote = A5;
int sensorValue = 0;

byte charP[8] = {0b11111, 0b10001, 0b10001, 0b10001, 0b11111, 0b10000, 0b10000, 0b10000};
byte charM[8] = {0b10001, 0b11011, 0b10101, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001};
byte charB[8] = {0b11110, 0b10001, 0b10001, 0b11110, 0b11111, 0b10001, 0b10001, 0b11110};
byte char1[8] = {0b00000, 0b00100, 0b01100, 0b10100, 0b00100, 0b00100, 0b00100, 0b00100};
byte charX[8] = {0b00000, 0b00000, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b00000};
byte charE[8] = {0b11111, 0b10000, 0b10000, 0b10000, 0b11111, 0b10000, 0b10000, 0b11111};
byte charT[8] = {0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100};

float readTemperature() {
    // Read the analog value from the temperature sensor
    int sensorValue = analogRead(temperaturePin);
    
    // Convert the analog value to temperature in Celsius
    float voltage = sensorValue * (5.0 / 1023.0); // Convert the sensor value to voltage
    float temperatureC = (voltage - 0.5) * 100; // Convert the voltage to temperature in Celsius
    
    return temperatureC;
}

void setup() {
  lcd.begin(16, 2);

  lcd.createChar(1, charP);
  lcd.createChar(2, charM);
  lcd.createChar(3, charB);
  lcd.createChar(4, char1);
  lcd.createChar(5, charX);
  lcd.createChar(6, charE);
  lcd.createChar(7, charT);

  pinMode(sensorPote, INPUT);
  pinMode(temperaturePin, INPUT);
}

int hours() {
  int hrs = now();
  hrs = hrs / 3600;
  hrs = hrs % 24;
  return hrs;
}

int mins() {
  int minutes = now();
  minutes = minutes / 60;
  minutes = minutes % 60;
  return minutes;
}

int secs() {
  int sec = now();
  sec = sec % 60;
  return sec;
}

void loop() {
  sensorValue = analogRead(sensorPote);

  if (sensorValue <= 200) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Visit xid.ee!");
    lcd.setCursor(3, 1);
    lcd.print("10% OFF!");
    delay(2000);
    for (int koht = 12; koht < 13; koht++) {
      lcd.setCursor(koht, 1);
      lcd.write(1);
      lcd.write(2);
      delay(2000);
    }
  } 
  else if (sensorValue > 200 && sensorValue <= 400) {
    lcd.clear();
    lcd.createChar(1, tempchar1);
    lcd.createChar(2, tempchar2);
    lcd.createChar(3, tempchar3);
    lcd.createChar(4, tempchar4);
    lcd.setCursor(0, 0);
    lcd.write(1);
    lcd.setCursor(0, 1);
    lcd.write(2);
    lcd.setCursor(1, 0);
    lcd.write(3);
    lcd.setCursor(1, 1);
    lcd.write(4);
    lcd.setCursor(3, 0);
    lcd.print("Temperature:");
    lcd.setCursor(3, 1);
    lcd.print(readTemperature()); // Display the current temperature
    lcd.print(" C");
    delay(3000);
  }
  else if (sensorValue > 400 && sensorValue <= 900) {
    lcd.clear();
    lcd.createChar(1, housechar1);
    lcd.createChar(2, housechar2);
    lcd.createChar(3, housechar3);
    lcd.createChar(4, housechar4);
    lcd.setCursor(0, 0);
    lcd.write(1);
    lcd.setCursor(0, 1);
    lcd.write(2);
    lcd.setCursor(1, 0);
    lcd.write(3);
    lcd.setCursor(1, 1);
    lcd.write(4);
    displayTimeAndDate();
  }
}

void displayTimeAndDate() {
  lcd.setCursor(3, 0);
  lcd.print("Time: ");
  lcd.print(formatDigits(hour()));
  lcd.print(":");
  lcd.print(formatDigits(minute()));
  lcd.print(":");
  lcd.print(formatDigits(second()));
  lcd.setCursor(3, 1);
  lcd.print("Date: ");
  lcd.print(year());
  lcd.print("-");
  lcd.print(formatDigits(month()));
  lcd.print("-");
  lcd.print(formatDigits(day()));
  delay(10000);
}

String formatDigits(int digits) {
  // Add leading zero if the number is less than 10
  if (digits < 10) {
    return "0" + String(digits);
  }
  return String(digits);
}