LCD 20x4 - Comunication via Bus I2C problems


My project is an irrigation system for my garden. For this I used an Arduino Mega 2560, some rely boards a LCD 20x4 and one DS1307.

My problem is after some time working, the system starts to not work and I don't know why. To solve the problem, I disconnect the power and I connect again, and the system starts working again.

I have checked every part of the code and I start thinking that the problem remains with the bus I2C.

Both, LCD and DS1307 are connected in the bus I2C, and in my opinion this could cause some problems in the bus making LCD working bad and stopping the program. The reasons to think that the problem is the LCD is because some times, the LCD had some symbol that i have never programmed, as the attached picture.

Can somebody tell me some idea that what is happening in my system?

Thank's in advance!


Just a guess, because we don't know anything about the rest of your circuit, but it could be that you are you using relays and the relays are creating electrical power issues for the LCD device.
It could be power glitches or noise spikes.

We need to know more about your overall circuit.

--- bill

Well, I have three relay boards with 4 relay in each board.

These relays are used to power on the pump, the electrovalves and also some other things like lights.

Everyday, at 4 pm, the power supply for the pump and this things changes from one floor to another, because I share the costs with my neighbour. One relay of the board, conmute a contactor which is the used to change the power supply, from one house or other. The power supply for the arduino is always the same and is always connected.

The other relays are conmuted during the day for power on the pump, the lights as I told you before.

Do you think the problems with the system are in the power supply or is more focused in the I2C bus?


The actual issue you are seeing on the LCD is usually caused by the LCD and the host (the arduino) getting out of nibble sync with each other. This is what causes the garbage on the screen. The LCD is using bytes that are 4 bits from one byte and 4 bits from the next byte. So garbage shows up. Once this happens the only way to get back in sync is using a special initialization procedure which the LCD library uses when begin() or init() is called.
This loss of nibble since occurs when there is an untended pulse on the E signal on the LCD or if the LCD fails to "see" the E signal transition properly.

Either of these can happen where there are power issues. Power issues like glitches can cause the LCD supply voltage to drop too low or spike, noise on the LCD supply voltage, or momentary voltage differences between various components connecting to the LCD. When there are voltage issues, the LCD can potentially "see" phantom E signal transitions or miss actual E signal transitions.
All these kinds of things can happen when relays are are used and are switching loads if the circuitry does not properly isolate power and/or the control signals.

I didn't fully understand your situation, but it sounds like you are not only using relays but are also switching some mains power which could be large loads.
These are the kinds of things that can create power issues for power supplies which then creates power/voltage issues for the Arduino its circuitry and the LCD device.

Can you fully explain what is happing again. I didn't understand it. It sounded like the mains power was being turned on/off or switched over which I'm guessing also includes the power to Arduino?

--- bill

Hi bperrybap, I'll try to explain better.

But, before explaining the system i would like to ask something. It's possible to connect the RTC and LCD in SDA and SCL pins at same time? Or this can affect both gadgets? For example, lossing some data.

About the system, there's one relay that change the power supply every 24h. This relay commut a contactor which has the power.

This 220 VAC power supply supplies the power to the pump, the electro valves, the lights and the 24VDC power supply.

The arduino is powered with an independentment transformer.

5V used to power the RTC, LCD and relays boards is supplied by the arduino board.

I think this is all.

Thanks you!

I2C is a bus, it is designed to have multiple slaves. Connecting the RTC and the LCD should not be an issue.

I still don't understand your power switching and in particular how you are powering the Arduino which powers your other components.

You are doing some kind of power switchover and then all you said about the arduino is:

The arduino is powered with an independentment transformer.

This doesn't say where that transformer is getting its power and whether that power is also being switched.

It sounds like you may have some large power transients during these switch overs particularly if you have large loads that are on while the switchover occurs.
That can create create issues for power supply feeding the Arduino which in turn can cause issues for the components powered by the Arduino.

Can you provide a better more detailed description of the power and how the power is being cut-over and what types of devices are on during this cutover.

--- bill

As I told you,

The Arduino is always connected to the supply using an independent transformer.

On the other hand, there are another connections to power supply, which are used to power the other devices. This supply is which change every day.
During the switching, only the power of the devices switches. Arduino, lcd and rtc is permanently powered.

I don’t know whatever tell you about how the system is powered.

Thank you.

As I told you,

The Arduino is always connected to the supply using an independent transformer.

Yes you have said that,
But just because the Arduino is using an independent transformer doesn't mean it is always powerd.
You never said whether or not the power to it was being switched over as well.
It was not until just now that you said:

Arduino, lcd and rtc is permanently powered.

So far you are slowly dribbling out details. Details that matter.
For example, initially you only said that things failed after running for a while and that some relays were involved.
Then you mentioned this strange power switchover that happens at 4PM.

On the other hand, there are another connections to power supply, which are used to power the other devices. This supply is which change every day.
During the switching, only the power of the devices switches.

You have also mentioned 220 VAC and 24VDC but I still can not fully figure out how things are hooked up and controlled.
The term "power supply" and "transformer" are vague as those could mean many different things and seem to mean different things when you are describing things.
Using the words "220 VAC"or "mains power" might be more helpful if you really mean mains power.
And then there is the term "power supply" which could mean mains power, AC stepped down transformer power, 24VDC or perhaps something else.
For example, I still have no idea how your are actually powering your Arduino.
24VDC is too much voltage for the Arduino but that is all you have mentioned so far.

And I can't tell if you are just switching over the 220VAC power for the pump or are switching over all the power between the apartments. i.e one apartment loses power when the other gets power.
And then when does it switch back, or does the power switch back and forth each day and stay connected for 24 hours?

I'm having to make some assumptions about how you have things hooked up and are controlling things which is not good as it may be incorrect.
And I really don't like guessing as it wastes time.

It sounds like you are using the Arduino to control a relay that is controlling a contactor that is switching some amount of 220VAC at 4PM.
But what isn't clear is what loads are on that 220VAC when this switching is happening.

I think a drawing of how this is all hooked up would help explain things better.

From my perspective, there are still lots of unknown details,
but suspicion is that you have a fairly large load on the 220VAC power when the switching is occurring so you are getting a spike on the mains power and perhaps even across relays controlled by the Arduino or even the contactor.
This may be causing a power spike on the 220VAC power that is connected to the power supply that is powering the Arduino.

--- bill