Programming The Arduino I2C LCD

The I2C LCD is a truncated version of the traditional LCD in that the six digital pins typically assigned to the Arduino are eliminated in favor of just two analog pins. In this project we will assemble an Arduino circuit that displays the current day and time on an I2C LCD screen. Use this link to be briefed on assembling the DS3231 RTC as this section focuses on the LCD only. Note that multiple I2C units can be used provided the SCL and SDA pins are connected in parallel as depicted in the sketch below.

Required Items:

I2C LCD        DS3231RTC

 

Fritzing Sketch:

 

I2C LCD Rear View

Note that the Fritzing sketch makes it appear like you have to solder many connections to the LCM1602. This is false as it already comes soldered onto the LCD as seen in the above picture. The blue item you see is the contrast adjustment that requires using a phillips head screw driver to see the text on screen. To connect, link the SDA to the Arduino A4 pin and SCL to the Arduino A5 pin, VCC to 5v and ground to the Arduino ground pin. Ensure that the SCL and SDA pins of your DS3231 are connected in the same way.

The following is the library for the I2C screen, be sure to include this in order to use I2C connectors, which are a lot easier to use than usual connections:

 

LCD/RTC Code:

Show Code

#include <Wire.h>
#include “RTClib.h”
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”};
int currentYear;
int currentMonth;
int currentDay;
int currentHour;
int currentMinute;
int currentSecond;

void setup () {
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println(“Couldn’t find RTC”);
while (1);
}
if (rtc.lostPower()) {
Serial.println(“RTC Time Reset”);
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(2018, 8, 8, 3, 25, 00));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}

void loop () {
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print(‘/’);
Serial.print(now.month(), DEC);
Serial.print(‘/’);
Serial.print(now.day(), DEC);
Serial.print(” (“);
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(“) “);
Serial.print(now.hour(), DEC);
Serial.print(‘:’);
Serial.print(now.minute(), DEC);
Serial.print(‘:’);
Serial.print(now.second(), DEC);
Serial.println();
currentYear = now.year();
currentMonth = now.month();
currentDay = now.day();
currentHour = now.hour();
currentMinute = now.minute();
currentSecond = now.second();
lcd.print(“Date:”);
lcd.print(currentMonth);
lcd.print(“/”);
lcd.print(currentDay);
lcd.print(“/”);
lcd.print(currentYear);
lcd.setCursor(0, 1);
lcd.print(“Time:”);
lcd.print(currentHour);
lcd.print(“:”);
lcd.print(currentMinute);
lcd.print(“:”);
lcd.print(currentSecond);
delay(1000);
lcd.clear();
}

 

Copy/paste this code into your IDE, verify port, board, and programmer, then upload.

Be Advised! There has been a recent update distinguishing the old boot loader from the new one as seen in the Tools>Processor menu. If your code doesn’t upload despite verifying configuration, select either the new or old bootloader, then upload.

One Reply to “Programming The Arduino I2C LCD”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.