Arduino Mega hangs

Hello
I use a fairly simple sketch to run a pump through a controller and while it is running I take sensor readings from a Bluerobotics Bar30 and temperature sensor. I input through the monitor the run time (short bursts of few seconds), speed and pumping direction. This usually functions as intended a few times and then hangs. I use a watchdog to reset the arduino when this happens to avoid damaging the system, but this happens too often to leave untreated. By commenting out sections of the sketch I ran both the pump and each of the sensors alone for quite a while, this way everything functions properly. You can see in the monitor log that when the full sketch is used the sensors send a lot of garbage, when used without the pump this doesn't happen.
I'm attaching the sketch in a zip file and a printscreen of the serial monitor with both successful runs and hangs.
Would be glad for any ideas.
Thanks

PumpControlLight_set_time.zip (4.47 KB)

Additions to the original post:

  1. Both of the sensors are I2C.
  2. The pump controller receives a PWM signal from the arduino which is passed through a 22Kohm / 10uF smoothing circuit (the controller requires an analog signal to function).

The Wire library can halt the sketch.
There is a low-level timeout, that is not turned on by default yet: ArduinoCore-avr/Wire.cpp at master · arduino/ArduinoCore-avr · GitHub.

However, you should first fix the problem.
Your sketch is not very long, I think you can put it in a single file.
When you show your sketch, please put it between code-tags. You get code-tags with the </> button.

Did your really connect the 3.3V I2C bus from the sensors to the 5V I2C bus of the Arduino Mega ?
The Arduino Mega has onboard pullup resistors of 10k to 5V.
The sensor specifies the SDA and SCL signals as maximum 3.6V.

There is also a mux on the I2C bus ? Is that 5V or 3.3V ? Can you show a schematic ?

Can you tell which libraries you use (please give a link to them). These libraries: TSYS01, MS5837, ping1d.
This library does not tell if it actually read real data from the sensor: GitHub - bluerobotics/BlueRobotics_MS5837_Library: Arduino library for the MS5837 pressure sensor..

Your Serial timeout is set to 10 minutes with the WatchDog off. Then you turn the WatchDog on and it is set to 1 second. I think that is not how a WatchDog should be used. If you really need a WatchDog, then it needs to be turned on all the time.
The Serial timeout is default 1 second, but 10ms would be a better value.
We never wait in a while-loop in the loop(). We prefer to let the loop() be executed as many times as possible.

How long are your wires for the I2C bus ? Do you use a cable ?
I wrote this page a week ago, can you take a look at it ? How to make a reliable I2C bus · Koepel/How-to-use-the-Arduino-Wire-library Wiki · GitHub.

Talking to a friend about my problem this morning, he suggested that I significantly raise the monitor baud rate. I was using a rate of 9600 (which was used in the sensors’ examples), I tried to change it to 57600 but that produced some garbage at the beginning of the run (where there was no text) so I elevated it to 115200. After working with the setup the whole day it didn’t hang once. So I guess my friend nailed it.
If anyone has a guess as to the reason behind this I’d be glad to hear it.
Thanks

Hi Koepel
I connected the Vcc line of the sensors to the mega's 3.3V output. The MUX is the adafruit TCA9548A I2C MUX which is powered by 5V.
The time out issue you mentioned is intentional: since the loop waits for the user's input it can sit idle for a long time (I walked away from my table once for and forgot to turn off the power for the pump and it went ON randomly when that time ran out). Since the hang only happens when the pump is on I enable the watchdog solely in the while() loop.
The wires (not a cable) are about 1 meter long.
I'll look over the material you suggested, thanks. But it seems that the problem is solved and the solution was trivial, although I don't understand why the lower baud rate caused the problem.

The Mega has 10k onboard pullup resistors to 5V for SDA and SCL. If you connect those directly to a 3.3V sensor, then the sensor might get damages.

The 115200 baud is normal. The 9600 is very slow and something from a long time ago.

When there is a lot Serial.println(), then the buffer of 64 bytes might get full. When it is full than the Serial.println() waits for each character if there is a free spot in the buffer. That may slow down the sketch hundred times.
However, I don't see that happen in your sketch. So I don't know the reason, sorry :-[

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.