In this Arduino Nano I2C LCD tutorial, it is shown how to communicate between Arduino and LCD using the I2C protocol. It is shown how to connect the SDA SCL Arduino Nano pins to the I2C LCD. Here128x32 OLED I2C LCD(Liquid Crystal Display) will be used. Arduino Nano program is provided which performs the I2C communication between Arduino nano and the OLED LCD.
What is I2C?
I2C
is abbreviation for Inter-Integrated Circuit which is a serial communication protocol developed by Philips Semiconductor in 1982. It is also known as Two Wire Interface (TWI) as this serial communication requires only two wire. I2C protocol is designed to interface Microcontrollers to other I2C capable devices such as I2C LCD, I2C EEPROM,
I2C sensors such as temperature sensor, pressure sensors,
accelerometers and others. There are 3 popular serial communication standards- SPI(Serial Peripheral Interface), USART and I2C.
How I2C works?
I2C is a serial communication standard which uses Master Slave system. The master sends commands and information request to I2C slave device and slave device replies with information required and keeps quit until another information is requested by the I2C master. The information exchange between Master and Slave happens with two just I2C communication wires called SDA(Serial Data) and SCL(Serial Clock) and a common ground wire. All the slave devices are connected to master device on these SDA and SCL wires as illustrated below.
The master SDA and SCL lines are connected to devices SDA and SCL respectively. The wires is open drain which means that the slave devices can provide low value but not high. So pull up resistor like 4.7KOhm are required so that the wires have power or active high. When using Arduino with the I2C wire library the I2C lines are automatically puts it into high state and so the external pull-up resistor are not required.
I2C protocol
The I2C protocol works on master slave networking. The slave devices have address and the master communicates with a specific slave with the slave address. The original address bits was 7 bits and later 10 bits address are also used. In 7 bit addressing, the 7 bits are for address and the last bit is for read or write. This 7 bit addressing(from A6 to A0) is shown below.
In 10 bit addressing, two bytes which is 16-bits are required. The first 5 bits is 11110 and is used to tell that next coming byte contains address bits. The next 2 bits are address bits A9 and A8. The next bit is Read/Write bit. Then the next byte contains other address bits which are A7 to A0. This is shown below.
Most of the device manufacturer specifies I2C address. In I2C network two sensors can have the same address and therefore to avoid addressing conflict, manufacturer also allows developers to change the address by allowing connection to +5V or ground. An example of this is shown below where the temperature sensor LM75A device pins can be connected in several ways to give it specific address.
Once the address of the slave is fixed, the master sends information request from slaves devices connected to the I2C network. Each of the slave receives compares the slave address with its own address. The slave which has the same address then replies to the information request by the master.The slave cannot initiate information exchange. Only when master device request information, slave can respond. Also the I2C communication is not full duplex meaning that request and respond from slave cannot happen at the same time otherwise there is collision. The slave device such as temperature sensor stores their values in register and master device request the information stored in that register.
All Arduino boards have I2C pins which are listed below for some Arduino boards.
Interfacing Arduino Nano and I2C LCD
The following circuit diagram shows how to interface Arduino Nano I2C pins to the OLED LCD I2C pins.
The following picture shows 128x32 OLED I2C LCD.
The 128x32 OLED I2C LCD has four pins: SDA, SCL, VCC, and GND The SDA, SCL, VCC, and GND pin of the OLED LCD is connected to SDA, SCL Arduino Nano pins A4, A5, 5V pin and GND pin respectively as shown in the above Arduino OLED I2C LCD interfacing circuit diagram above. These SDA and SCL pins for I2C communication are the same as for Arduino Nano and Arduino Uno(see Arduino I2C OLED LCD Interfacing).
Arduino Nano Program for I2C LCD Communication
To enable I2C communication between OLED LCD and Arduino program we need to use two libraries which are as follows.
1. Adafruit SSD1306 library(adafruit_SSD1306.h)
2. Adafruit GFX library(adafruit GFX.h)
The libraries is installed using the Arduino library manager in Arduino IDE. In Arduino IDE, go to Tools > Manage Libraries.
From the Library Manager which opens search and install the Adafruit SSD1306 library as illustrated below.
The Adafruit SSD1306 library provides driver support for SSD1206 OLED LCD driver
Then again search and install the Adafruit GFX library which is illustrated below.
The Adafruit GFX library provides graphics primitives such as lines, circles, text, etc.
Program code for Arduino OLED I2C LCD
The following is Arduino Nano programming code that writes the text "ee-diary.com" to the I2C OLED LCD, and scroll the text left, right and, diagonally left and diagonally right.
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//create Adafruit SSD1306 display object
Adafruit_SSD1306 OLEDLCD(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 600000, 200000, OLED_RESET);
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!OLEDLCD.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
OLEDLCD.clearDisplay(); // Clear the buffer
OLEDLCD.setTextSize(1);
OLEDLCD.setTextColor(WHITE);
OLEDLCD.setCursor(22, 0);
// Display initial static text
OLEDLCD.println("ee-diary.com");
OLEDLCD.display();
delay(100);
}
void loop() {
// Scroll in various directions, pausing in-between:
OLEDLCD.startscrollright(0x00, 0x0F);
delay(500);
OLEDLCD.stopscroll();
delay(50);
OLEDLCD.startscrollleft(0x00, 0x0F);
delay(800);
OLEDLCD.stopscroll();
delay(50);
OLEDLCD.startscrolldiagright(0x00, 0x07);
delay(180);
OLEDLCD.stopscroll();
delay(50);
OLEDLCD.startscrolldiagleft(0x00, 0x07);
delay(150);
OLEDLCD.stopscroll();
delay(50);
}
For I2C communication and the OLED LCD, we have used the Wire.h, SPI.h,
Adafruit SSD1306, and Adafruit GFX libraries. Constant alias names are used which define the width and height of the LCD. Since the size of the OLED LCD is 128x32 we have to use a 0x3C I2C address. Then an
instance OLEDLCD of Adafruit_SSD1306 object constructor is created with the display screen size, set to two-wire communication, and reset value constant.
In the setup() function we start with the initialization of the serial communication which here is used for debugging purposes only. We have used the if statement with OLEDLCD.begin() method to verify that the SSD1206 OLED LCD driver configuration is configured and is OK. If there is any misconfiguration or the I2C device is not detected we get the message on the serial monitor. The OLEDLCD.begin() method is then used to allocate RAM for the image buffer, and initialize peripherals and pins. The clearDisplay() method is used to clear the contents of the display buffer. Then the setTextSize() and setTextColor() methods are used that set the string text size and the color of the text. The text to be displayed on OLED 128x32 LCD is "ee-diary.com" which is sent and gets printed on the LCD. The OLEDLCD.display() will perform a writing operation pushing the data present in RAM to SSD1306 LCD driver.
In the loop()
function we use the startscrollright(), startscrollleft(),
startscrolldiagleft() and startscrolldiagright() methods(functions) to scroll the text to the left,
right, lef diagonal, and right diagonal. The delay() function is used here to set the
scrolling duration in each direction.
Video demonstration
In the following video, it is shown how the above Arduino Nano program code for the OLED 128x32 LCD I2C communication works. It shows how the text gets scrolled in different directions.
So this Arduino nano I2C LCD tutorial showed how to interface the SDA SCL Arduino Nano pins with OLED 128x32 LCD I2C pins. Arduino Nano program code was provided to illustrate the I2C communication.
I2C communication is used in interfacing Arduino with other kinds of devices from sensors to EEPROM. The LCD I2C NodeMCU tutorial illustrates how to interface OLED 128x32 I2C LCD with NodeMCU. And in the External EEPROM with Arduino tutorial, it is shown how to use I2C communication between Arduino Uno and 24LC256
external EEPROM to log LM35 temperature sensor data into the EEPROM.