Help with Wire + LiquidCrystal

Hey guys,
My project would like to have both an LCD and Clock.

I'm using a Basic 16x2 Character LCD - White on Black 5V LCD which uses LiquidCrystal.h.

I'm also using a Real Time Clock Module, which uses Wire.h.

Even in very simple sketches using Arduino Duemilianove (atmega 328), whenever I use Wire.begin AND lcd.begin I get bizarre erroneous results both on the serial monitor and on the LCD.

They work fine in independent sketches, but won't play well.

Any help, much appreciated!

  • Hunter

Post your code. There is no reason for the two to interfere with one another - it's probably your implementation.

I suspect you have used analog pins 4 and 5 (AKA digital pins 18 and 19) on your LCD or something else, which are the I2C pins for the RTC but help me out here with pics and code.

Every one of my clocks works fine:

Video:

scroll down to "complete projects"

Hey Guys,
Pinout is below. I am using analog pins 4 and 5 on my clock. Sorry the commenting is weak... It's pretty simple, I borrowed most of it.

Muchos G.

  • Hunter

#include <Wire.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int clockAddress = 0x68; // This is the I2C address
int command = 0; // This is the command char, in ascii form, sent from the serial port

byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
// Reset the register pointer
Wire.beginTransmission(clockAddress);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(clockAddress, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());

// Need to change this if 12 hour am/pm
hour = bcdToDec(Wire.receive() & 0x3f);
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());

Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.println(year, DEC);

}

// Sample frequency in milliseconds
const double Freq = 500;
// This constant describes the Temp pin. It won't change:
const int Temp = 1;
// How big a sample to take
const long SampleSize = 6682;

// Indices
long Sample = 0;
int Trigger;

void setup()
{

lcd.begin(16, 2);

pinMode(ledPin, OUTPUT); // sets the pin as output
analogWrite(ledPin, 75);

Wire.begin();
Serial.begin(9600);
delay(1000); //Wait a second for OpenLog to init
}

void loop()
{

lcd.print("WZDS");

Wire.beginTransmission(clockAddress);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(clockAddress, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());

// Need to change this if 12 hour am/pm
hour = bcdToDec(Wire.receive() & 0x3f);
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
Trigger = 0; //minute%59;

if (Trigger == 0)
{
Sample = 0;
getDateDs1307();

do
{
Serial.print(analogRead(Temp));
delay(Freq); // Delay for proper DFT sample Temp
Serial.print(" ");
Sample++; // Increase index
Serial.println();

} while (Sample < SampleSize); // SampleSize Sample set
getDateDs1307();
}
}

Here's the pinout:
Wiring:

Arduino Other Device
TX>1 Open Log RXI
Ground Open Log Ground
3.3V Open Log VCC

Analog 5 Clock SCL
Analog 4 Clock SDA
Ground Clock Ground
5V Clock 5V

Analog 0 Sensor Temp
5V Sensor VCC
Ground Sensor Ground

Ground Disp 1
5V Disp 2
Digital 9 Disp 3
Digital 12 Disp 4
Ground Disp 5
Digital 11 Disp 6
Digital 5 Disp 11
Digital 4 Disp 12
Digital 3 Disp 13
Digital 2 Disp 14

Something to note that may or may not help is that 'ledPin' is undefined but you proceed to use it in the setup routine:

pinMode(ledPin, OUTPUT); // sets the pin as output
analogWrite(ledPin, 75);

'ledPin' will then be a nonsensical value and may be causing problems.

Hope this helps.

There's no potentiometer? Digital pin 9 connected to potentiometer but I can't find anywhere pin 9 is ever doing anything in the code. Must have forgotten the code where you were using PWM to mimic pot. It's not a good practice. Go get a pot.

It's this line of code that throws the whole deal. I definitely did forget to identify pin 9 (fixed now). And I don't have a pot on it, but it still won't work anytime I include the following:

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

I haven't tried with a pot (I will, when I'm back home, currently on military orders away from home station).

Any assistance much appreciated!

  • Hunter

#include <Wire.h>

#include <LiquidCrystal.h>
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int ledPin = 9; // LED connected to digital pin 9

int clockAddress = 0x68; // This is the I2C address
int command = 0; // This is the command char, in ascii form, sent from the serial port
long previousMillis = 0; // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307() {
// Reset the register pointer
Wire.beginTransmission(clockAddress);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(clockAddress, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());

// Need to change this if 12 hour am/pm
hour = bcdToDec(Wire.receive() & 0x3f);
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());

Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.println(year, DEC);

}

// Sample frequency in milliseconds
const double Freq = 500;
// This constant describes the rate pin. It won't change:
const int Rate = 1;
// How big a sample to take
const long SampleSize = 6682;

// Raw Data
// Array for collecting instantaneous rate data

// Indices
long Sample = 0; // Index for IRate and Four
long PrevMic = 1000000;
long disp;
int Trigger;

void setup()
{
// lcd.begin(16, 2);
// Initialize the serial communications:
pinMode(ledPin, OUTPUT); // sets the pin as output
analogWrite(ledPin, 75);
Wire.begin();
Serial.begin(9600);
delay(1000); //Wait a second for OpenLog to init
}

void loop()
{

Wire.beginTransmission(clockAddress);
Wire.send(0x00);
Wire.endTransmission();

Wire.requestFrom(clockAddress, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());

// Need to change this if 12 hour am/pm
hour = bcdToDec(Wire.receive() & 0x3f);
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
Trigger = 0;

if ((Trigger == 0)&&(hour < 30))
{
Sample = 0;
getDateDs1307();

do
{
Serial.print(analogRead(Rate));
delay(Freq); // Delay for proper DFT sample rate
Serial.print(" ");
disp = micros() - PrevMic;
Serial.print(disp);
PrevMic = disp + PrevMic;
Sample++; // Increase index
Serial.println();

} while (Sample < SampleSize); // SampleSize Sample set
getDateDs1307();
}

}

Please use the code button "#" when inserting code.

List the program that makes the liquid crystal display work. We can compare this program with the other program you listed to see if there is any difference on the lcd code.

Even in very simple sketches using Arduino Duemilianove (atmega 328), whenever I use Wire.begin AND lcd.begin I get bizarre erroneous results both on the serial monitor and on the LCD.

The serial monitor could very well be the culprit. Why don't you disable all the code related to that and see how the wire library and the LiquidCrystal library behave. That's likely the final configuration that you are interested in.

As previously mentioned, please use the code button "#" when inserting code.

Don