Before connecting the Arduino Nano to the USB port, check for any shorts (direct positive voltage to ground connection) as this could damage both the Nano board and the USB/PC.
The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so the following examples shows how to control a 16x2 LCD in 4-bit mode as this uses the least number of port pins on the Nano.
As generally the number of available I/O ports is at a premium, how to use an I2C connection via a PCF8574 port expander, to decrease the number of required I/O pins on the Nano to only two (2) to connect a LCD is also shown below.
The Photographs Section shows the circuit on breadboard, whereas, the Videos Section shows the circuit in operation.
LCD using 4-bit mode (6 x I/O pins)
The Circuit Details and Schematic Diagram Sections discuss and display the required physical connections and circuitry required for connection of the LCD to the Arduino Nano.
The actual process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library (1) simplifies this for you so you don't need to know the low-level instructions. The LiquidCrystal Library provides a wide range of functions that enable features such as scrolling text, blinking cursors and custom character generation, see the following reference for details (2).
The following code Snippet 1 gives an example of the necessary code to display text on a connected LCD display. The code displays a 'static text' on the first line of the LCD display, with the second line displaying the letters of the alphabet in a continous loop.
Code Snippet 1: LCD display (4-bit mode)
#include
const int rs=9, en=8, d4=7, d5=6, d6=5, d7=4;
LiquidCrystal myLCD(rs, en, d4, d5, d6, d7);
char theChar = 65; //ASCII character code
void setup() {
myLCD.begin(16,2); // initialize the lcd
myLCD.setCursor(0,0); //Defining positon to write from first row, first column .
myLCD.print("LCD Display");
myLCD.setCursor(0,1); //Second row, first column
myLCD.print("Test:");
}
// the loop function runs over and over again forever
void loop() {
myLCD.setCursor(6,1); //2nd line, 6th col
myLCD.print(theChar); //print incrementing alphabetic character
theChar = theChar + 1;
if (theChar > 90) {theChar = 65;}; // reset to 'A' if reached 'Z'
delay(1000);
}
LCD using PCF8574 (2 x I/O pins on the Nano)
The Circuit Details and Schematic Diagram Sections discuss and display the required physical connections and circuitry required for connection of the LCD to the Arduino Nano via I2C using the PCF8574 port expander.
The I2C device (the PCF8574A in this case) needs to be connected via the SDA and SCK pins (with pullup resistors, and obviously connect Gnd and Vcc) to the Arduino Nano. The Serial Monitor on the Arduino IDE is used so output can be directed via USB to a PC Com serial port.
Using a PCF8574A and PCF8574 pins 1 through 3 (ie A0 to A2) tied to ground produces an I2C device address of 0+0111+0+0+0 = 0x38, so the code below should report a single device found with address of 0x38 hexadecimal. Altering which of the PCF8574A pins 1 through 3 are tied to ground or Vcc will change the reported address (the datasheet gives the table showing what the resultant addresses should be).
Initial testing of a I2C interfaced device (and if the I2C interface library is working on the Arduino Nano) is to use the I2C scanner sketch.
Once it is determined that I2C communication is working with the Arduino Nano and the PCF8574A, the next step is to use the HD44780 Library (3) (which is written for interfacing LCDs via I2C) with the test in Code Snippet 2 below.
The Photographs Section shows the circuit on breadboard, whereas, the Videos Section shows the circuit in operation.
Code Snippet 2: LCD using I2C/PCF8574 (2 x I/O pins)
#include
#include // main hd44780 header
#include // i2c expander i/o class header
#define LCD_ADDR 0x38
hd44780_I2Cexp myLCD(LCD_ADDR); // specify a specific i2c address
const int LCD_COLS = 16; // LCD geometry
const int LCD_ROWS = 2;
char theChar = 65; //ASCII character code
void setup() {
int status;
status = myLCD.begin(LCD_COLS, LCD_ROWS); // begin() will automatically turn on the backlight
myLCD.print("LCD Display");
myLCD.setCursor(0,1); //Second row, first column
myLCD.print("Test:");
}
void loop() {
myLCD.setCursor(6,1); //2nd line, 6th col
myLCD.print(theChar); //print incrementing alphabetic character
theChar = theChar + 1;
if (theChar > 90) {theChar = 65;};
delay(1000);
}
Only Logged-In Members can add comments