Arduino Forum

Using Arduino => Programming Questions => Topic started by: xl15 on Aug 12, 2014, 02:01 pm

Title: Trouble initializing i2c display
Post by: xl15 on Aug 12, 2014, 02:01 pm
I've made a display for my home made car to display temperatures, pressure, fuel level etc.
It works sometimes, but most of the time the display isn't initialized.
It is a large 4x20 LCD with i2c interface.
The strange thing is that if I have my laptop connected to the Arduino and upload the program again or open serial monitor, the display starts working every time.
But if I trigger a reset with the button on the Arduino it doesn't help.
Why is the reset triggered by serial monitor different from the button on the board?

The power supply is not the problem. It works with a 5 V, 500 mA USB power supply. And in the car it gets its power from a 7 V, 2 A supply.

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include "Adafruit_MAX31855.h"
#include <math.h>
#include <DallasTemperature.h>
#include <OneWire.h>

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress waterThermometer = { 0x28, 0x42, 0x4F, 0x4C, 0x05, 0x00, 0x00, 0x44 };
DeviceAddress oilThermometer = { 0x28, 0x08, 0x89, 0x4C, 0x05, 0x00, 0x00, 0x40 };
RTC_DS1307 rtc;
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
LiquidCrystal_I2C lcd(0x27 , 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
//SDA = A4
//SCL = A5

void setup() 
{
  Serial.begin(9600); 
  Wire.begin();        // join i2c bus
  rtc.begin();
  lcd.begin(20,4);   
  sensors.begin();

  lcd.noBacklight();
  delay(250);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Initializing...");
  delay(500);
  lcd.clear();
  delay(500);
}

void loop()   
{}


Bonus picture of display and car :)

(http://dl.dropbox.com/u/25591866/Sjuan/bil14_640.JPG) (http://dl.dropbox.com/u/25591866/Sjuan/bil14.JPG)

(http://dl.dropbox.com/u/25591866/Sjuan/dash02_640.JPG) (http://dl.dropbox.com/u/25591866/Sjuan/dash02.JPG)
Title: Re: Trouble initializing i2c display
Post by: Peter_n on Aug 12, 2014, 03:03 pm
I see a ribbon cable with 4 wires going to the display. Can seperate all those wires ? The SDA and SCL don't like it to be that close.

A reset via the serial monitor, or a reset via the button is the same. I think there might something else that has influence.

Can you add a delay after Wire.begin() and before you start using the I2C bus. For example 100ms should be enough, so make it 500ms.

What about pullup-resistors for the SDA and SCL ? For testing about 10k is okay, but in a noisy environment it should be 4k7 or a little less. The display and the sensors might already have pullup resistors, you have to calculate the resulting pullup impedance.

Is your MAX31855 compatible with a 5V Arduino ? It is the only device using the SPI ?

Suggestion : You could try a OLED display. That LCD display will always look cheap, but you can make a OLED display look awesome. But I don't know if they can be that big....
Title: Re: Trouble initializing i2c display
Post by: econjack on Aug 12, 2014, 03:12 pm
I haven't used the LiquidCrystal_I2C library, but the constructor call looks weird:

Code: [Select]

LiquidCrystal_I2C lcd(0x27 , 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address


For most Arduino boards, pins 0, 1 are used by the Serial object and pins 2,3 are for external interrupts. In most examples I've seen for I2C use, the constructor is:

Code: [Select]

LiquidCrystal_I2C lcd(0x27 , 16, 4);  // Set the LCD I2C address


which sets the slave/master address and the column/row count. Have you tried the simpler constructor?
Title: Re: Trouble initializing i2c display
Post by: xl15 on Aug 12, 2014, 03:32 pm
I will add the delay and try.

The ribbon cable is from the testing. Now it is about 50 cm of round cable with 6 conductors, but I guess the problem is the same.

I'm using 4,7 k pullup resistors. Dont know if the display have internal too.

I aslo have a slave arduino communicating over i2c, can that affect things? I don't think so since that is after the display initialization. That communication works great by the way, and it is with 2 meter of the same cable (6 conductors)

The MAX31855 controller works with 5 V:
http://www.adafruit.com/products/269

The display matches my Arduino speedometer ;)
(http://dl.dropbox.com/u/25591866/Sjuan/hastighetsmatare03_640.JPG) (http://dl.dropbox.com/u/25591866/Sjuan/hastighetsmatare03.JPG)
Title: Re: Trouble initializing i2c display
Post by: xl15 on Aug 12, 2014, 03:42 pm
@econjack
I think the
Code: [Select]

LiquidCrystal_I2C lcd(0x27 , 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

is for the i2c to HD44780 converter and has nothing to do with the arduino itself.
The rows and columns are set with
Code: [Select]

lcd.begin(20,4);   
Title: Re: Trouble initializing i2c display
Post by: Peter_n on Aug 12, 2014, 05:18 pm
With a good cable (that has less coupling between SDA and SCL) you can get 2 meters with I2C.
With just a bunch of loose wires you can get more. If I remember it right, someone could do 5 meters with slow I2C. But I would not even try that. I think your cables are one of the problems.

An Arduino as i2c slave is only active when it has been addressed. But the 2 meters cable to the slave will hurt the whole I2C bus.
You might use the i2c slave in a way that it starts sending data to the master. That could destroy the I2C communication.
When you use the slave only as slave, with onRequest and onReceive, it is slower, but no chance for collision.

You can try lowering the I2C speed.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 23, 2015, 10:18 am
It's been a while, but now I'm back to troubleshooting this problem.

Even with just 20 cm cable and nothing else on the i2c bus, the problem is the same.

It doesn't work standalone, but if I connect my laptop and open serial monitor it works every time.

I tried a different arduino and a bare minimum progam, still the same.

I'm confused and starting to wonder i the i2c adapter is broken.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 23, 2015, 10:25 am
Standalone
(https://dl.dropboxusercontent.com/u/25591866/forumbilder/lcd%20i2c%20standalone.jpg)


Connected to laptop and serial monitor opened
(https://dl.dropboxusercontent.com/u/25591866/forumbilder/lcd%20i2c%20laptop.jpg)
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 23, 2015, 11:02 am
You say the power supply is not the problem. But I think you should double check.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 23, 2015, 11:24 am
I'll do that. The external power supply is an ordinary switched module (cheap ebay LM2596) set to 7 V and connected to Vin.

The external power is connected when I get it to work with the "serial monitor trigger". I can then disconnect the laptop and it keeps on working fine.

I tried resetting the board with the button while connected to the laptop, thinking that the applied USB power should help. But it didn't.
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 23, 2015, 11:51 am
What happens if you remove
Code: [Select]
  Serial.begin(9600); 
:)
I know it's kind of a random move, but...
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 23, 2015, 11:57 am
That's a good question. I'll try that when I get home.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 24, 2015, 09:43 am
Still not working :)
Powered only from laptop, USB charger or powerbank (battery powered USB charger), no change.

Removed "Serial.begin(9600);"
No change. Doesn't work when powered on, but with serial monitor opened it works. No serial communication though :)

I added a lot of deleys just becouse

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27 , 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup()  
{
  delay(1000);
  Serial.begin(9600);  
  delay(1000);
  Wire.begin();        
  delay(1000);
  lcd.begin(20,4);  
  lcd.noBacklight();
  delay(250);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Initializing...");
  delay(1000);
  lcd.clear();

}
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 24, 2015, 09:47 am
Doesn't work when powered on, but with serial monitor opened it works. No serial communication though :)
You mean, it doesn't work when first powered on through the USB? But when you open the monitor window it does? Just to be clear.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 24, 2015, 09:52 am
Yes, that's exactly how it is.
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 24, 2015, 10:04 am
Maybe the reset circuit on your Arduino is defective.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 24, 2015, 10:08 am
I don't think so. I've tried two different UNO boards.
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 24, 2015, 10:23 am
Perhaps post your code in its entirety? It looks like you've omitted your loop(). Or a complete subset that reproduces the problem...
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 24, 2015, 10:29 am
The loop isn't much to see :)

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27 , 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup() 
{
  delay(1000);
  Serial.begin(9600); 
  delay(1000);
  Wire.begin();       
  delay(1000);
  lcd.begin(20,4);   
  lcd.noBacklight();
  delay(250);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Initializing...");
  delay(1000);
  lcd.clear();

}

void loop()   
{
  lcd.setCursor(0,0);
  lcd.print("Loop...");
  delay(1000);
}
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 24, 2015, 11:21 am
Try moving lcd.clear() to immediately after lcd.begin().
I looked at some of my code (but it's a 16x2 display, not 20x4), and that's how I have it. I'm using the same LCD library.
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 24, 2015, 11:26 am
OK, I'll try that.

But I might be on to something else.
I ran an i2c scanner and found two adresses I didn't think about. 0x50 and 0x68.
I'd forgot about my i2c real time clock. It's soldered directly to my homemade shield so it's still connected when I thought I removed everything except the display on the i2c bus.

I guess the 0x68 is the rtc and 0x50 is the eeprom onboard the rtc.
Title: Re: Trouble initializing i2c display
Post by: aarg on Mar 24, 2015, 11:30 am
OK, I'll try that.

But I might be on to something else.
I ran an i2c scanner and found two adresses I didn't think about. 0x50 and 0x68.
I'd forgot about my i2c real time clock. It's soldered directly to my homemade shield so it's still connected when I thought I removed everything except the display on the i2c bus.

I guess the 0x68 is the rtc and 0x50 is the eeprom onboard the rtc.
Perhaps, but I have had the same kind of RTC connected with the LCD on the same I2C bus in a digital clock experiment, and never had any conflicts. Are you sure everything is designed to run on the same voltage? 5V, I presume?
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 24, 2015, 11:33 am
No, I agree. It should work. I mean, that's what is2 is for.
I think the RTC has built in pull-up resistors for SDA and SCL.
And I have pull-ups on my breadboard.
Is it possible that several pull-up resistors is causing the issue?
Title: Re: Trouble initializing i2c display
Post by: sureview on Mar 26, 2015, 01:04 pm
you should only need one pull up on the bus, I have had odd results with the lcd lib and I2c bus myself, try adding a delay after every instruction to the lcd and see if that cures it , you can then remove the delays until it stops working again

hope this helps
Title: Re: Trouble initializing i2c display
Post by: xl15 on Mar 26, 2015, 04:27 pm
Seems like the i2c to lcd adapter was faulty.
With a new one it works.
But i destroyed my large 30€ LCD in the process :(
So now I'm testing with a small (standard) LCD.
Title: Re: Trouble initializing i2c display
Post by: jeroenb42 on Sep 14, 2016, 09:02 pm
I know it's an old topic but I have the exact same problem as the original poster, except that I am just connecting two Arduino's, I2C Master and I2C Slave. If I have my laptop connected, it works but when I disconnect the Slave and reconnect again, the Master does not see the Slave. If I press reset, nothing, if I use the reset pin, nothing, but as soon as I open the serial monitor.... It works. I have tried with 4 Arduino's (all Nano) and exact same result. I had already tried removing all Serial communication, no difference. It's driving me insane that I cannot get this to work.
Title: Re: Trouble initializing i2c display
Post by: sureview on Sep 15, 2016, 07:49 am
I have had a similar problem , everything would work after i downloaded the  sketch , but then when stand alone - no display, in the end it was the rtc module which was on the same i2c bus

I am now moving on to a nextion display, which is looking really good
Title: Re: Trouble initializing i2c display
Post by: jeroenb42 on Sep 23, 2016, 09:41 am
After fiddling around some more, I found out that if the I2C slaves are not connected via USB but through USB power only, all worked. I wrote a post on it because I was so happy it worked:
https://piandmore.wordpress.com/2016/09/15/multiple-arduino-i2c-slaves/ (https://piandmore.wordpress.com/2016/09/15/multiple-arduino-i2c-slaves/)