I2C LCD conflict with Mega pins 29-41

I purchased about a dozen I2C 16x2 LCD displays with the PCF8574 controller. I had no trouble using this display with Arduino Unos. Now I am using them with the Mega and was getting unexpected results. So, I used the example sketch from the Liquidcrystal_I2C library and made a short program to highlight the problem better. Essentially, if I set ANY one or more of the Mega pins 29 through 41 as OUTPUTs, the LCD will display and then go blank (and it looks like just the backlight turns off.) The program is still running. As far as I could tell, the digital pins of the Mega shouldn’t interfere with I2C, as it is hooked up to pins 20 and 21. I’m hoping someone can shed light on this. An example sketch below. If you uncomment any of the “pinmode” lines, the display will come on and then go blank on the second loop. I have tried:

  • Different Mega
  • Different LCD
  • Confirmed pin connections
  • Sacrificial Goat Dance with blood and fur everywhere

There are some lines that are redundant on purpose because I wanted to be explicit with what it was doing and where.

If anyone has a mega and one of these LCDs, please give it a try for me. Or maybe this is something obvious I just don’t see. Thanks again.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

void setup()
pinMode(26, OUTPUT); //Works with this pin on
pinMode(27, OUTPUT); //DOESN'T WORK Uncomment any of these pin outputs and the display goes blank
pinMode(28,OUTPUT);// DOESN'T WORK
/*pinMode(29,OUTPUT); // DOESN'T WORK
pinMode(30,OUTPUT); // DOESN'T WORK
pinMode(31,OUTPUT);// DOESN'T WORK
pinMode(32,OUTPUT); // DOESN'T WORK
pinMode(33,OUTPUT); // DOESN'T WORK
pinMode(34,OUTPUT); //DOESN'T WORK
pinMode(35,OUTPUT); //DOESN'T WORK
pinMode(36,OUTPUT); //DOESN'T WORK
pinMode(37,OUTPUT); //DOESN'T WORK
pinMode(38,OUTPUT); //DOESN'T WORK
pinMode(39,OUTPUT); //DOESN'T WORK
pinMode(40,OUTPUT); //DOESN'T WORK
pinMode(41,OUTPUT); //DOESN'T WORK 
lcd.init();                      // initialize the lcd 
  // Print a message to the LCD.
lcd.print("Hello, world!");
lcd.print("In Setup");

void loop()
Serial.println("LCD in Void loop");
lcd.print("HELLO WORLD!");
lcd.print("In Loop");
Example code is attached. thanks for any input on this.

I set up my Mega (clone) with a generic I2C 16x2 LCD (1602 with the PCF8574 controller) and the code that you posted. After un-commenting all of the pinModes the display works normally. It does not go black and the backlight is on and steady.


This line has no effect on my LCD.

Is anything connected to the pins that are giving you trouble?
Is the Mega 5V power affected when the display stops working (monitor the 5V with a DMM)?

Thank you! That is very helpful because it tells me there is something external. Your suggestion of watching the 5V supply is a good one because I have seen the display dim. The Mega is currently running from a 2.5A well regulated 12V supply. The output pins are connected to 8 port relay boards with opto-isolators (5V) which get their power from a 1.5A regulator (supplied by the same 12V) with plenty of filter capacitance, as is the LCD itself. I may not have enough 5V umpf. Though it isn't happening for me on actually switching the pins, but simply defining them, one never knows. I'll disconnect external pins. I also have two other I2C devices at different addresses running. And the more I'm writing, the more I'm thinking I may be short on available 5V amperage. Thank you soo much for taking the time to do that.

With 12V supplying the 5V regulator there is less than about 150mA available from the regulator before it starts to get hot and go into shutdown. The Mega, itself, needs 30-50mA so that leaves about 100mA for the rest of the stuff powered by the regulator. Does the 5V regulator get hot?

The Mega is currently running from a 2.5A well regulated 12V supply.

The obsolete tutorials on the Arduino site and others imply that the largely ornamental "barrel jack" and "Vin" connections to the on-board regulator imply that this is a usable source of 5 V power. This is absolutely not the case. It is essentially only for demonstration use of the bare board back in the very beginning of the Arduino project when "9V" transformer-rectifier-capacitor power packs were common and this was a practical way to power a lone Arduino board for initial demonstration purposes. And even then it was limited because an unloaded 9 V transformer-rectifier-capacitor supply would generally provide over 12 V which the regulator could barely handle. Your 12 V supply may be well regulated, but this is actually somewhat irrelevant in the event.

This is because the on-board regulator is essentially capable of powering only the microcontroller itself and no more than a couple of indicator LEDs. The on-board regulator might be able to power a few other things if it had a heatsink, but on the older Arduino designs, it does not.

A practical power supply for the Mega (or UNO, Nano, Pro Mini, Leonardo etc.) is a "phone charger" with a USB output connector for 5 V, generally up to a couple of Amps though you can not feed more than 500 mA through the USB connection.

in addition to ^:

power modules directly from the power supply. do not use the leads of your MEGA, or the onboard regulator as the fuse for the LCDs.

A practical power supply for the Mega (or UNO, Nano, Pro Mini, Leonardo etc.) is a "phone charger" with a USB output connector for 5 V, generally up to a couple of Amps though you can not feed more than 500 mA through the USB connection.

Although, IMO, I think it is pretty silly of Arduino.cc to continue to use a USB type B connector on Uno and Mega over the much more common micro USB connector.

Actually, I think using the USB type B connector is pretty dumb as not only does it require a less common cable or adapter to be used but there are also some shields (LCD keypad being one) that won't work on the official UNO and Mega boards as the metal type B connector housing hits some of the pins on the shield.
This one of the reasons I won't buy those boards.

--- bill

Well, I don't think the UNO form factor - with the odd pin placements - is particularly useful at all.

If there is a shield that does what you need, concisely in one unit, then the UNO form may be a useful approach. But in all other cases, a Nano which will fit a solderless breadboard for development, or stripboard or a custom PCB as a daughter board is the better and more concise approach.

The later boards - the "101" and Due which also have proper (switchmode) regulators - may perform your whole task as-is or perhaps with one added shield, but I have not used them largely due to expense as an ESP8266 addresses the need for WiFi or a Pi for more serious computing power.

The Pi represents a practical approach for powering - it runs from 5 V, 5 V is readily provided by "Phone chargers" of various forms, so there is no attempt in the design to provide other more complex power options.

Just to clarify, I have a SEPARATE 5V regulator supplying all the off-board devices using 5V. I'm aware that the onboard 5V regulator is of minimal use for powering anything other than itself. That is capable of 1.5A. That however, may not be enough, so I'm troubleshooting amp draw.

But if you are powering other devices from a proper 5 V switchmode regulator, it makes obvious sense to power the Arduino from the same supply.

Hey guys, we (meaning you guys with all your guidance) figured it out! I have a less-than-stellar 7805 regulator that is crapping out at about 250mA and not even getting hot. (it's one that is rated for 1.5A.) So...in goes a REAL power supply and voila! No more problem. Geez...power....Thanks to everyone for the help.