Plugging in USB messes up LCD

Hi,

I have an Arduino Uno externally powered through the VIN pin, and I am sending and receiving data over USB to the PC. I typically apply power to VIN first, then plug in the USB, as I don’t want the circuit drawing power from the USB. However, on a fairly regular basis (say 40% of the time) when I plug in the USB, the LCD display gets messed up, and even clear() doesn’t correct it. Pics attached.

Pin assignments:

// LCD pins
const int	D7			= 14;   //A0
const int	D6			= 15;   //A1
const int	D5			= 16;   //A2
const int	D4			= 17;   //A3
const int	ENABLE		= 18;   //A4
const int	RS			= 19;   //A5

Code that’s interacting with the LCD when USB is plugged in (during setup()):

void displayPower()
{
	long power = (CYCLE_MICROS - gCycleDelay + 1) * 100 / (CYCLE_MICROS - HIGH_BUFFER);

        // POWERCOL = 0, POWERROW = 1
	lcd.setCursor(POWERCOL + 12, POWERROW);
	if (power < 100)
	{
		lcd.print(" ");
	}
	lcd.print(power);
}

At the end of setup():

void displayStatus(String s)
{
	//lcd.noDisplay();
	lcd.clear();
	lcd.setCursor(0, 0);
	lcd.print(s);
	delay(100);
	//lcd.display();
	#if defined DEBUG && (DEBUG > 1)
	Serial.println(s);
	#endif
}

Pic ending in 346 shows normal state. 349 shows it messed up while setup() is still running. 350 is after setup() completes (i.e. after a clear() has been issued)

Can you post all the code?
It is pretty much impossible to comment much about the code without being able to see all the code.

It could be that the Arduino is being reset when you plug it in to the USB.
It shouldn't matter (from the LCD working perspective) as setup() should run again and re-initialize everything.
But maybe there is some sequence out of order in setup() or the LCD library isn't properly doing the 4 bit LCD initialization.

It could also be that there is a big power glitch or ground bounce when the USB is plugged in and that creates an issue
for the LCD or the LCD initialization code in the library.

--- bill

ALL the code is a lot :o

Attached sketch has setup() and the routines it calls.

Fan.ino (10.3 KB)

DEBUG is set to 2.
Is the Arduino resetting when the USB is plugged in?
i.e. do you see the displayStatus("CALIBRATING FAN") message on the serial port from setup()?

Do you have a scope to be able look at the power signals?

--- bill

Interesting question! It appears to be resetting every time the PC first initiates serial comms. Unfortunately I don't have a scope...

The issue you are seeing is likely due to the host and the LCD getting out of nibble sync.
This is typically caused by the LCD "seeing" phantom E pulses, or not seeing an E pulse.
Once the host and the LCD are out of sync it will not recover until the host goes through a particular sequence to get the LCD
back into 8 bit mode, and then can put it back into 4 bit mode.
That sequence is normally done when begin() is called.

The question becomes how is the LCD seeing phantom E pulses, or "garbage" instructions?
The most common ways are power issues, like power glitches, or ground bounce issues.

I forgot that since you are using an Arduino UNO you use the USB interface to read the serial port as well so you can't be watching the serial port before the USB is plugged in.
So yeah, every time the USB serial port is opened on the host, DTR will be dropped which resets the Arduino.

I was just trying to determine if the Arduino is resetting when you plug in the USB cable to try to narrow things down a bit.
So the serial port is out from trying to watch for a reset when plugging in the USB cable.

I guess, you could simply watch the LCD, to see if you see "CALIBRATING FAN" message when you plug in the USB cable
after the Arduino is up and running.
Or possible put in an extra message or do something like blink the on board LED a few times, just to indicate that setup() has been called.

What all is pulling power from the Arduino? How much of a load?

-- bill