Connect Arduino to I2C Liquid Crystal Display

by Lewis Loflin

  
  

This is an Arduino version of my Raspberry Pi program. See Interface I2C LCD to Raspberry Pi in C.

Both programs do not require any external libraries or hidden code and I believe more flexible.



Arduino sketch for this project: lcd_i2c_no_library.ino


/*

By Lewis Loflin lewis@bvu.net
http://www.sullivan-county.com/main.htm
Electronics website:
http://www.bristolwatch.com/index.htm

This demonstrates using direct coding and no external
user libraries op operate a common I2C based
LCD display with Arduino.

Based on an previous Raspberry Pi Linux project.
Interface I2C LCD to Raspberry Pi in C
http://www.bristolwatch.com/rpi/i2clcd.htm

*/

// Define some device parameters
#define I2C_ADDR   0x27 // I2C device address

// Define some device constants
#define LCD_CHR  1 // Mode - Sending data
#define LCD_CMD  0 // Mode - Sending command

#define LINE1  0x80 // 1st line
#define LINE2  0xC0 // 2nd line

#define LCD_BACKLIGHT   0x08  // On
// LCD_BACKLIGHT = 0x00  # Off

#define ENABLE  0b00000100 // Enable bit

#include <Wire.h>

void lcd_init(void);
void lcd_byte(int bits, int mode);

// added by Lewis
void typeInt(int i);
void typeFloat(float myFloat);
void lcdLoc(int line); //move cursor
void ClrLcd(void); // clr LCD return home
void typeln(const char *s);

int value = 0;

void setup(void) {
  Wire.begin(); // begin I2C

}

void loop(void) {

  ClrLcd(); // defaults LINE1
  typeln("Int  ");
  // int value = analogRead(0);
  typeInt(value);

  lcdLoc(LINE2);
  typeln("Float ");
  float FloatVal = float(value) / 2 ;
  typeFloat(FloatVal);
  delay(500);

  value++;
  if (value > 1023) value = 0;
}

//  float to string
void typeFloat(float myFloat)   {
  char buffer[20];
  // ftoa() replacement
  dtostrf(myFloat, 5, 2, buffer);
  typeln(buffer);
}

// int to string
void typeInt(int i)   {
  char array1[20];
  sprintf(array1, "%d",  i);
  typeln(array1);
}

// clr lcd go home loc 0x80
void ClrLcd(void)   {
  lcd_byte(0x01, LCD_CMD);
  lcd_byte(0x02, LCD_CMD);
}

// go to location on LCD
void lcdLoc(int line)   {
  lcd_byte(line, LCD_CMD);
}

// out char to LCD at current position
void typeChar(char val)   {
  lcd_byte(val, LCD_CHR);
}

// this allows use of any size string
void typeln(const char *s)   {
  while ( *s ) lcd_byte(*(s++), LCD_CHR);
}

void lcd_byte(int bits, int mode)   {

  //Send byte to data pins
  // bits = the data
  // mode = 1 for data, 0 for command
  byte bits_high, bits_low;

  // uses the two half byte writes to LCD
  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT ;
  bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT ;

  Wire.beginTransmission(I2C_ADDR); // address device

  // High bits
  Wire.write(bits_high);
  // Toggle enable pin on LCD display
  delayMicroseconds(100);
  Wire.write(bits_high | ENABLE);
  delayMicroseconds(100);
  Wire.write(bits_high & ~ENABLE);
  delayMicroseconds(100);

  Wire.write(bits_low);
  // Toggle enable pin on LCD display
  delayMicroseconds(100);
  Wire.write(bits_low | ENABLE);
  delayMicroseconds(100);
  Wire.write(bits_low & ~ENABLE);
  delayMicroseconds(100);

  Wire.endTransmission();

}

void lcd_init()   {
  // Initialise display
  lcd_byte(0x33, LCD_CMD); // Initialise
  lcd_byte(0x32, LCD_CMD); // Initialise
  lcd_byte(0x06, LCD_CMD); // Cursor move direction
  lcd_byte(0x0C, LCD_CMD); // 0x0F cursor on  Display On,Cursor On, Blink Off
  lcd_byte(0x28, LCD_CMD); // Data length, number of lines, font size
  lcd_byte(0x01, LCD_CMD); // Clear display
  delayMicroseconds(500);
}

For earlier Arduino Projects see Arduino Projects Hobby Projects Tutorials


Videos:
My YouTube Videos on Electronics
Introduction to the Arduino Microcontroller
Part 1: Programming Arduino Output
Part 2: Programming Arduino Input
Part 3: Arduino Analog to Digital Conversion
Part 4: Using Arduino Pulse-Width-Modulation
Repost Arduino AC Power Control

arduino

See How I got into Electronics

New May 1, 2016:

I'd say my job title would be applied technologist. See Electronics Projects for Students and Hobbyists

Recent YouTube Videos: