Arduino DUE hangs due to i2c LCD

I noticed a strange behavior when using the Arduino DUE with a 20x4 LCD display that uses I2C to communicate, let me explain:

Parts included:
1 x Arduino DUE R3-E board
1 x SparkFun Logic Level Converter - Bi-Directional
1 x LCD 20x4 with i2c ( https://www.robofun.ro/lcd/lcd_20x4_i2c_negru_verde )

Software:
Arduino IDE 1.6.8
Adafruit LCD from here GitHub - adafruit/Adafruit_LiquidCrystal: A fork of my own LiquidCrystal library

My sketch ( part of ):

//LCD library //#include "Wire.h" //#include "Adafruit_LiquidCrystal.h" //LCD address is #0 //Adafruit_LiquidCrystal lcd(0);

//Other pins
const int buttonRight = 7;
const int buttonLeft = 8;
const int laserPin = 6;
const int ledPin = 10;
const int buttonPin = 9;

// Global vars
int workingMode = 0; // 0 - Normal, 1 - Laser, 2 - Delay, 3 - Serial controlled
int lastButtonPushed = 0;
int currentState = 0; // 0 - Idle, 1 - Processing, 3 - Locked

//Camera pins
const int cam1 = 22;
const int cam2 = 24;
const int cam3 = 26;
const int cam4 = 28;
const int cam5 = 30;
const int cam6 = 32;
const int cam7 = 34;
const int cam8 = 36;
const int cam9 = 38;
const int cam10 = 40;
const int cam11 = 42;
const int cam12 = 44;
const int cam13 = 46;
const int cam14 = 48;
const int cam15 = 50;
const int cam16 = 51;
const int cam17 = 49;
const int cam18 = 47;
const int cam19 = 45;
const int cam20 = 43;
const int cam21 = 41;
const int cam22 = 39;
const int cam23 = 37;
const int cam24 = 35;

const int cameras = {cam1, cam2, cam3, cam4, cam5, cam6, cam7, cam8, cam9,
cam10, cam11, cam12, cam13, cam14, cam15, cam16, cam17,
cam18, cam19, cam20, cam21, cam22, cam23, cam24 };

int lastButtonRightState = LOW;
int lastButtonLeftState = LOW;
long lastRightDebounceTime = 0;
long lastLeftDebounceTime = 0;
long debounceDelay = 500;
int buttonState;
unsigned long previousMillis = 0;
const long interval = 500;
int ledState = LOW;
bool pcAction = false;

// Buffer to store incoming commands from serial port
String serialInData;

void setup() {
Serial.begin(57600); // Init serial 9600b
// lcd.begin(20, 4); // Init LCD, 20x4 lines i2c
// lcd.setBacklight(HIGH);
// lcd.setCursor(0,0);
// lcd.print(“lcd test string”);

//Pin definitions
pinMode(laserPin, OUTPUT);
digitalWrite(laserPin, LOW);
pinMode(ledPin, OUTPUT);
pinMode(A0, INPUT); // Photoresistor pin
pinMode(buttonPin, INPUT); // Take photo pin
pinMode(buttonRight, INPUT); // LCD MENU
pinMode(buttonLeft, INPUT); // LCD MENU
for (int count=0;count<24;count++) {
pinMode(cameras[count], OUTPUT);
digitalWrite(cameras[count], LOW);
}
Serial.print(“Init finished!\n”);
}

As you can see now all the code related to LCD is commented out.

When using the LCD the Arduino sometimes boots OK, very fast and is working as expected but sometimes if I disconnect the serial port and reconnect it, it never resets. Instead it completely freeze and I have to power cycle the device in order to get it working again ( not even the onboard reset button works ). Let’s say this thing happen 5 times out of 10.

Another situation is when I power the device, sometimes is working as expected, sometimes is never boot up, or it’s booting but freezes after.

Also noticed this behavior when disconnecting from serial port and reconnecting using the Serial Monitor, sometimes is working as expected ( CPU is reset, I see the “Init finished” printed to serial ) and sometimes completely freeze the device, had to power cycle.

All those problems happen only during start-up, if the device started OK and if I don’t reset the serial port everything works as expected.

Also noticed that when using the LCD libraries, the board boots ( if it succeed ) instantly ( max 1 second ) and if not using the libs it takes about 5 to 7 seconds for the board to respond to serial.
Is this normal ? How fast should the DUE respond to serial after power cycle ?

Thanks !

I’ve had a similar problem on start-up. I got around it by adding the below code to setup(), then do the normal Wire and LCD initializations.

	// Issue 20 I2C clocks to make sure no slaves are hung in a read
	pinMode(20, OUTPUT);
	pinMode(21, OUTPUT);
	pinMode(70, OUTPUT);
	pinMode(71, OUTPUT);
	digitalWrite(20, LOW);
	digitalWrite(70, LOW);
	for (int i = 0; i < 20; i++)
	{
		digitalWrite(21, LOW);
		digitalWrite(71, LOW);
		delayMicroseconds(10);
		digitalWrite(21, HIGH);
		digitalWrite(71, HIGH);
		delayMicroseconds(10);
	}

Regards,
Ray L.

Hi Ray,

Did you have this problem on DUE and also with a i2c LCD ?

Thanks !

mihait: Hi Ray,

Did you have this problem on DUE and also with a i2c LCD ?

Thanks !

And other devices...

Thanks !

I will try that but I still cannot understand why the whole board freezes and not even the reset button works.

It is necessary to power cycle in order to get it working again.

Also very curios why the board starts instantly with the LCD/wire library loaded and without them it takes about 7 seconds.