Proyecto 30.1 - LCD 1602 I2C Interface

10 de enero de 2016

Con el siguiente proyecto inauguramos una serie dedicada al bus IIC, también llamado I2C.

Veremos como conectar al Arduino un LCD de 16x2 caracteres mediante el bus I2C. 

Como vimos en el Proyecto 11 controlar displays de texto con Arduino es muy fácil. La interfaz de conexión típica necesita de al menos seis salidas digitales del Arduino para funcionar. 

Utilizando una pequeña tarjeta basada en el CI de Philips PCF8574T, es posible conectar un LCD estándar de 16x2 caracteres mediante el bus I2C del Arduino, de esa manera sólo necesitamos los pines A4 (SDA) y A5 (SCL). Ahorramos E/S digitales que nos pueden ser muy útiles para otras funciones.

Para más información, consulta la página de tronixstuff dedicada a LCD´s.

El módulo utilizado es el que se muestra en la imagen.

Esquema

La conexión es muy sencilla. A los pines A4 y A5 solo hay que añadir la alimentación del módulo con +5V y GND.

El módulo dispone de un potenciómetro para regular el brillo del backlit. 

Para que funcione el siguiente código, necesitamos descargar la librería NewliquidCrystal para LCD´s con interfaz I2C. Gracias a F. Malpartida.

El ejemplo nº 1 muestra un mensaje cada 4 segundos.

Sketch 1

/* Demonstration sketch for PCF8574T I2C LCD Backpack 

Uses library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads GNU General Public License, version 3 (GPL-3.0) */

#include <Wire.h>

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack

void setup()

{

  // activate LCD module

  lcd.begin (16,2); // for 16 x 2 LCD module

  lcd.setBacklightPin(3,POSITIVE);

  lcd.setBacklight(HIGH);

}

void loop()

{

  lcd.home (); // set cursor to 0,0

  lcd.print("LCD 16x02");

  lcd.setCursor (0,1);   

  lcd.print("OVER I2C BUS");

  delay(4000);

  lcd.clear();

  lcd.setBacklight(LOW);      // Backlight off

  delay(100);

  lcd.setBacklight(HIGH);    // Backlight on

  lcd.clear();  

  lcd.setCursor (0,0);   

  lcd.print("Arduino Proyects");

  lcd.setCursor (0,1);   

  lcd.print("sites@angmuz");

  delay(4000);

  lcd.clear();

  lcd.setBacklight(LOW);      // Backlight off

  delay(100);

  lcd.setBacklight(HIGH);     // Backlight on

}

En el segundo ejemplo podemos hacer scroll con el texto y mostrar mensajes de más de 16 caracteres.

Sketch 2


/* Demonstration sketch for PCF8574T I2C LCD Backpack 

Uses library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads GNU General Public License, version 3 (GPL-3.0) */

#include <Wire.h>

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack

void setup()

{

  // activate LCD module

  lcd.begin (16,2); // for 16 x 2 LCD module

  lcd.setBacklightPin(3,POSITIVE);

  lcd.setBacklight(HIGH);

}

void loop()

{

  lcd.setCursor(0,1);

  lcd.print("sites@angmuz");

  lcd.setCursor(0,0);

  matrix("Testing the I2C LCD Module.");

  delay(2000);

  lcd.clear();

}

void matrix( char *text)

{

 int length = strlen(text); // the number of characters in the text

 if(length < 16)

 lcd.print(text);

 else

 {

   int pos;

   for( pos = 0; pos < 16; pos++)

   lcd.print(text[pos]);

   delay(1000); // allow time to read the first line before scrolling

   pos=1;

   while(pos <= length - 16)

 {

   lcd.setCursor(0,0);

   for( int i=0; i < 16; i++)

   lcd.print(text[pos+i]);

   delay(300);

   pos = pos + 1;

 }

}

}


Links

Francisco Malpartida: LCD I2C Library

Tronixstuff: Serial PCF8574 Backpacks