This tutorial describes how to use an Arduino to drive an LCD screen.
Arduinos are popular microcontroller boards and a common desired functionality is to use them to drive LCD screens, usually to relay information to the user. In this tutorial, I will teach you how to use the Adafruit I2C/SPI LCD Backpack with an Arduino microcontroller board to drive a LCD.
LCDs require many connections to a driver to work. Managing all these connections all the time can become both cumbersome and annoying. Luckily, Adafruit has made an I2C/SPI LCD Backpack that works with most LCDs. This backpack conveniently reduces the number of connections between your microcontroller and the LCD to 4.
I always like to make a wiring diagram (Figure 1: Arduino-LCD Schematic) using Fritzing, an open-sources schematic capture and PCB routing software. You can download Fritzing using the following link (optional): http://fritzing.org/home/
Figure 1: Arduino-LCD Schematic
Pin 1 on the LCD goes to Pin 1 on the LCD Backpack. The rest of the pins are wired sequentially. This can be done on a breadboard or the backpack can be soldered to LCD as I have done.
I2C and SPI are two very popular serial interface buses. This tutorial covers interfacing your Arduino to the LCD Backpack using I2C, but the LCD Backpack can interface with SPI too. You can set the I2C address (A0, A1, A2) or enable SPI (SPI Enable) by jumpering the solder jumpers on the backpack (Figure 2: Solder Jumpers on LCD Backpack). The only circuit using I2C in the tutorial is the LCD Backpack, so we do not need to change the current configurations. This means the LCD Backpack will have an I2C address of 0 (0x00).
Figure 2: Solder Jumpers on LCD Backpack
To interface the LCD Backpack to the Arduino, connect 5V and a ground pin on the Arduino to the 5V and ground pin on the LCD Backpack. This will provide the LCD and LCD Backpack with power. Note: The LCD requires 5V minimum to work properly. The next two connections are serial data and serial clock. The serial clock connection (orange wire) is between the SCL pin on the Arduino and the CLK pin on the backpack. The serial data connection (blue wire) is between the SDA pin on the Arduino and the DAT pin on the backpack.
Figure 3: LCD Backpack Pinout
2. Writing the Code
The first part of the code is to include the Adafruit_LiquidCrystal header file. This allows you to use the functions in this library. Because the Adafruit_LiquidCrystal library is automatically downloaded with Arduino IDE, this tutorial doesn’t cover downloading Arduino libraries.
Next, we set our LCD Backpack address to 0 (remember, we have not jumpered any of the solder jumpers).
Finally, I make one global variable called timer that I will use in the loop section later.
Before writing to the LCD, it needs to be initialized. The “begin” function does this by telling the LCD Backpack how many characters are on the display. Since the LCD I am using has a backlight, I also turn the backlight on.
Now that the LCD is initialized, I write “Test Code” to check that everything is working. This code sets the cursor to a starting position, writes “Test” to the display, waits 2 seconds, and then clears the display.
Figure 4: Setup Code
The loop part of the code uses the millis() function and divides by 1000 to compute how long the program has been running. The code then uses the print and setCursor functions to display the program time across the LCD. The loop then waits a second before repeating.
Figure 5: Loop Code
3. Uploading and Running the Code
Upload the code to the Arduino. Make sure the Arduino is connected using the 9V Power Adapter because power over USB is not sufficient to power both the Arduino and the LCD display. Once the program begins you should see “Test” across the LCD display as the program runs through setup (Figure 2: LCD Displaying “Test”). When the program loop begins you will see the time program displayed and updating every second (Figure 3: LCD Display Program Runtime – 8(s)). Congrats! You now have an easy-to-use LCD screen for your Arduino board and can use it as a display for future projects. A tip to keep in mind: I2C is a slow bus and if you are constantly updating your LCD you will take time away from the controller performing other tasks.
Figure 6: LCD Displaying “Test”
Figure 7: LCD Display Program Runtime – 8(s)
Figure 8: LCD Displaying Program Runtime – 28(s)
Appendix: Arduino_LED Code in Text
/* Project: Arduino LCD
* Written by: Chris Marella
* Date: January 11, 2018
// include the Adafruit LCD Library:
// Connect the the LCD Backpack via I2C, default address #0 (A0-A2 not jumpered)
inttimer;//create a global variable for a timer
lcd.begin(19,3);//The begin function tells the LCD driver the size of your LCD screen (columns x rows)
lcd.setBacklight(HIGH);//setBacklight controls the LCD backlight
lcd.setCursor(0,0);//setCursor function sets the position of the character cursor (column x row)
lcd.print("Test");//Prints "Test" across the LCD
delay(2000);//Wait for 2 seconds
lcd.clear();//clear function clears the LCD screen
timer=millis()/1000;//set timer variable to how long program has been running in seconds
lcd.setCursor(0,0);//set LCD cursor to 0,0
lcd.print("Time:");//print time on LCD row 1
lcd.setCursor(0,1);//move LCD cursor to row 2
lcd.print(timer);//print the current program run time