Arduino is blocking

Hello, I have an Arduino Mega that is connected to the internet via an ESP8266 using a serial connection. The Mega is also connected to an UNO via an I2C connection using two P82B96 ICs, and it is located about 80 meters away. Throughout the house, there are many wires for sensors and buttons. The problem is that the Arduino Mega freezes after an indeterminate amount of time, for example, after 2 hours or 16 hours, it varies. The program on the Mega is around 5500 lines long, so it's quite large. The issue occurs when I manually turn on a light switch in a room or turn on some device in the house, and at that moment, the Arduino Mega sometimes freezes.

For power supply, I use a quality 12V laptop power adapter for the Arduino Mega.

I use LED lights in the house.

I suspect that electromagnetic interference is causing the problem.

Are there any filters for interference?

And where should these filters be placed? Perhaps on all three main power phases or near each device?

I have ordered an oscilloscope and it will arrive soon, but I still have no experience with that device to monitoring interference.

Of course there are, but so much depends on which wires are acting as antennas and bringing the noise pulses to the Arduino. The easiest to try are the snap together ferrite filters that go over each wire and the plastic housing locks closed. The wire may have to be looped several times through the filter. Most others will require the wire to be opened and the filter inserted in series with the wire.
Do it ALL at the Arduino, itself. If that does not minimize the problem, then you will have to go to the sources and fix the problem there.
Do only one wire at a time and test!

Why did you choose for a 12volt supply.
Is there something else connected to the Mega that needs 12volt?
If not, then try a 5volt cellphone charger/brick, connected to the USB socket.

Are there any relay modules connected to the Mega.
Do the relay contacts have snubber circuits or VDR/MOV/varistors across the contacts.
Are there any switches with long wires connected to the Mega.
They might need a 100n cap between pin and ground, close to the Mega.
Post a diagram.
Leo..

1 Like

Sounds like the wrong type of connection. If you get any noise on the i2c bus, it can cause the Mega and/or the Uno to freeze in a deadlock.

Interesting part. Have you read the datasheet and followed all the advices for getting 80 meters performance out of them?

Cable type and bus speed for two.

TBH I'll never think to use I2C as other than for local communication, on the board or very near by.

There are good and old methods for the distances you are trying to achieve.

a7

1 Like

Is your wiring done by two twisted pairs SDA/GND and SCL/VCC?
Why don't you replace Uno with Esp and communicate wirelessly?

Poor choice. I2C is designed for communication between chips on a single PCB.

UART serial would be much easier to implement, and works well for much larger distances.

. The issue occurs when I manually turn on a light switch in a room or turn on some device in the house, and at that moment, the Arduino Mega sometimes freezes.

That is to be expected for poorly designed, unshielded, long wire connections. You may also have ground loop problems, due to distance devices being powered from different branches of the AC wiring, for example.

As others have said, this is your problem. To get this distance I have use differential buffers.

While making this installation

I had to send I2C from a cupboard behind the desk at the bottom of the picture, up to the top most drum and cog mechanism. I knew that just sending I2C that far simply would not work. So I used some differential buffers at each end to ensure the I2C and other signals made it up that distance.
This is the basic circuit I used.

Note that the wire used was a twisted pair cable. This canceled out any common mode interference. The signals were used not only for I2C but also for motor drives, Neopixel LED strips and emergency stop buttons.

I would recommend this sort of circuit for getting signals a long way.

ARDUINO.zip (2.8 MB)
If someone tray to use program, DS3231 RTC is necessary for the code to work.
Thank you for your responses. These are the schematics I have made myself from 2017 until now. Sorry for the unclear schematics, when I started in 2017, I knew almost nothing about electronics. Most of the things in these schematics are deliberately complicated for learning electronics. For example, the relays operate at 24V, which I obtain using a DCDC converter from 12V. I couldn't get the external EEPROM and SD slot to work simultaneously due to some manufacturing defect I read about on a forum, related to the SD slot I am using. That's why there's a TC4016BP. The EEPROM and SD are working OK, but currently deactivated. Let's focus on the problem:
So, we agree that the issue is with the long, low-quality cat5 cables in the I2C network. Is it best to wait for the new oscilloscope to arrive and use it to look for interference, or should I immediately focus on replacing the P82B96? To clarify, the complete schematic and program work perfectly, but after some indeterminate time of a few hours or day, the Arduino MEGA blocks.
Thanks.

Step back a bit. What data is it you are transmitting over that distance, and what throughput is necessary? It may be that you require a different medium, and that means we need to know much more about the 'guts' of the application.

According to datasheet it should be doable if you just respect the wiring schemes using twisted pairs data/gnd, data/vcc.

My childhood dream was to live in a smart house,
controlled by a central computer.
Driven by strong desire and curiosity, I started learning electronics, and very quickly realized that pure electronics couldn’t meet my needs. So, I transitioned to digital electronics and Arduino.

Here’s a detailed description of what Arduino is supposed to do:

Arduino Mega:
The central unit that collects all the data and displays it on a screen.
It gathers data about external and internal temperatures, as well as the water temperature in the boiler.
It controls two ventilation fans for room air circulation, which I can manage via buttons or the Blynk app.
The Arduino Mega directly turns the bathroom and kitchen boilers on and off using relays.
The RTC module ensures accurate timekeeping.
Two ESP8266 modules are connected to the Arduino Mega, working in parallel mode due to the Blynk app's data stream limitations.

Arduino Uno:
Located on the building's roof, it collects weather condition data and controls one ventilation fan for air circulation.
The data exchange between the Uno and Mega is minimal. When called by the Arduino Mega (master), the Arduino Uno sends data about temperature, air pressure, and I’m still working on adding data about wind speed, rain, humidity, and air pollution.

As I progress in learning about different sensors, the Uno will send a few more pieces of information in the future. However, the amount of data is very small, and sending all the values once a minute is completely sufficient.

Misapplication of I2C. Either wireless(either change processors, or add-on), or a very simple RS485 string (conceptually, MUCH simpler, uses almost all of what you have now) would be my approach. YMMV.

1 Like

I read a little about this: RS485
Which one is better:

https://www.aliexpress.com/item/1005005702432776.html?spm=a2g0o.productlist.main.41.675eNwaANwaApj&algo_pvid=2680d919-4ceb-4ea0-974d-5bc8f0247650&algo_exp_id=2680d919-4ceb-4ea0-974d-5bc8f0247650-20&pdp_npi=4%40dis!EUR!0.95!0.64!!!0.98!0.66!%402103864c17317943362688441e32d3!12000034057036113!sea!SRB!1820342262!X&curPageLogUid=hdSrJsqnO5Hp&utparam-url=scene%3Asearch|query_from%3A
this is smaller.

or this:
https://www.aliexpress.com/item/1005006656455603.html?spm=a2g0o.productlist.main.13.675eNwaANwaApj&algo_pvid=50c5a1e3-1ebc-4853-a198-0cda97a3cb2e&algo_exp_id=50c5a1e3-1ebc-4853-a198-0cda97a3cb2e-6&pdp_npi=4%40dis!EUR!0.26!0.15!!!1.97!1.18!%402103892f17317934219421034ef2ee!12000037938905466!sea!SRB!1820342262!X&curPageLogUid=flaE76gVd751&utparam-url=scene%3Asearch|query_from%3A

It is not too complicated to switch the entire network from P82B96 to RS485. However, my board (Board 30m long cable.png) will undergo significant changes as I need to add a microcontroller to communicate with the PCF8574.

I use the lower one, but the upper one would do as well. Several of the lower one were given to me, that's all.
RS485 has proponents and detractors on this forum. I expect you'll hear from both sides.

OOPS. The upper one is 3.3V, not 5, so less convenient with Uno, Nano, Mega style boards. Not incompatible, if you're not using the onboard 3.3v for anything, it should be okay to use for this.

As they all come from aliexpress then neither are better, because they are purveyors of trash. Or at best poor quality unknown product.

It's almost 2025. Things have changed a lot since you started your project in 2017.
Buy a few XIAO ESP32C3 boards, and experiment with ESPNow for your sensor links.
Eventually replace the master "Mega, ESP8266 (obsolete) and RTC" with a single capable ESP32.
Leo..

2 Likes

Good information on I2C, and some things not to use it for by a forum member here.