Problem Solving: Arduino Mega 2560 + Analog Read + LCD YwRobot LCM1602 IIC V1

Written by  on June 18, 2014 

A quick one today.

I’ve got one of the little 2 line LCDs with a ywrobot I2c interface on the back. Only requires power, SDA and SCL rather than 8 or more pins the LCD would require directly.

– One thing to know is that the display suffers from some kind of delay where pixels fade in and out over a few microseconds. What this means is that with a very fast loop you need a delay just so values have enough time to appear. Also values that change very fast will appear to be a blur.

To wire it up I followed this post:
http://arduino-info.wikispaces.com/LCD-Blue-I2C#v1

The only change was adding the potentiometer on A1 (plus power and ground for the pot, used part of a breadboard as a power bus, see photo)

LEARNING THE HARD WAY: After testing with the example code from the Arduino-info site I turned to reading an analog value direct onto the screen. I then did the usual mucking around trying to get the int value as a char, but then it turns out the LCD library supports print so it will do it for you

Kudos to terry@yourduino.com for the example code and instructions.

/* YourDuino.com Example Software Sketch
 16 character 2 line I2C Display
 Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"
 Originally by terry@yourduino.com 
 Slightly modified by Toby K to include an Analog Read.*/

/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE
// Get the 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>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

/*-----( Declare Variables )-----*/
//NONE

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);  // Used to type in characters

  lcd.begin(16,2);   // initialize the lcd for 16 chars 2 lines, turn on backlight

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

//-------- Write characters on the display ------------------
// NOTE: Cursor Position: (CHAR, LINE) start at 0  
  lcd.setCursor(0,0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("HI!YourDuino.com");
  delay(8000);  

// Wait and then tell user they can start the Serial Monitor and type in characters to
// Display. (Set Serial Monitor option to "No Line Ending")
  lcd.clear();
  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("Use Serial Mon");
  lcd.setCursor(0,1);
  lcd.print("Type to display");  


}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  {
      // clear the screen
      lcd.clear();
      
      int sensorValue = analogRead(A1);
      Serial.println(sensorValue);
      //String myString = String(sensorValue);
      
      lcd.print(sensorValue);
      
      delay(50);
      
    
  }

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


/* ( THE END ) */

Category : ArduinoUncategorized