Eksempel på opsætning og programmering af 20x4 og 16x2 liniers LCD display med I2C interface

Farvekode:

Blå:SCL
Grøn:SDA
Rød:5V
Sort:GND

Tilslut LCD som vist herover.
Tilslutning er identisk, uanset om det er et 20x4 (4 linier af 20 tegn) eller et 16x2 (2 linier af 16 tegn).

For at kunne bruge et LCD med I2C interface skal du først hente New-LiquidCrystal Library fra flg. adresse:
https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads

Du kan finde displays i shoppen her

16x2 eksempel:

/* Trab.dk Example Software Sketch
 20 character by 4 line I2C Display
 Backpack Interface labelled "A0 A1 A2"
 Connect Vcc and Ground, SDA to SDA, SCL to SCL on Arduino UNO R3
 kundeservice@trab.dk */

/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE

// Get the newest LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x3F for a 16 chars 2 line display
// some displays may respond on 0x27 instead!
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup()
{
  lcd.begin(16,2);         // initialize the lcd for 20 chars 4 lines, turn on backlight

  // ------- Quick 2 blinks of backlight  -------------
  for(int i = 0; i< 2; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on 

  //-------- Write characters on the display ------------------
  // NOTE: Cursor Position: Lines and Characters start at 0 
  lcd.setCursor(1,0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(4,1);
  lcd.print("Trab.dk");
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

}/* --(end main loop )-- */


/* ( THE END ) */

20x4 eksempel:

/* Trab.dk Example Software Sketch
 20 character by 4 line I2C Display
 Backpack Interface labelled "A0 A1 A2"
 Connect Vcc and Ground, SDA to SDA, SCL to SCL on Arduino UNO R3
 kundeservice@trab.dk */

/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE

// Get the newest LCD I2C Library here:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x3F for a 20 chars 4 line display
// some displays may respond on 0x27 instead!
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup()
{
  lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines, turn on backlight

  // ------- Quick 2 blinks of backlight  -------------
  for(int i = 0; i< 2; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on 

  //-------- Write characters on the display ------------------
  // NOTE: Cursor Position: Lines and Characters start at 0 
  lcd.setCursor(3,0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(6,1);
  lcd.print("Trab.dk");
  delay(500); 
  lcd.setCursor(0,2);
  lcd.print("20 by 4 Line Display");
  lcd.setCursor(1,3);
  delay(1000);  
  lcd.print("http://www.trab.dk");
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

}/* --(end main loop )-- */


/* ( THE END ) */