Page 1 of 1

recover I2c stuck

Posted: Fri Sep 24, 2021 8:50 am
by Anz8
Hello,

My project controls, through an STM32F103C8, a relay module. Attached to the board there are also an LCD module and an SHT31 sensor, both connected to the I2c (both on the first i2C. the second one isn't used). I use the Arduino IDE with the wire library and the LiquidCrystal_I2c. I have issues due to interferences from the relays module. I'm working on the hardware part but I'd like to implement some checks and recover on the software side. I've already activated the watchdog but the more frequent issue is that the LCD display is stuck while the MCU keeps working and so no reset is triggered. I have programmed the sketch so that it triggers a relay when a read from the SHT31 sensor is below a given level and so when the LCD is stuck, I'm not even sure that function is working properly. The other relay is activated on a frequency base so I know that the MCU isn't stuck. I've also implemented some checks for a soft reset of the sensor if the reads are wrong. As said, I'd like to implement some check and reset of the I2c bus or, in general, to recover the proper functioning of the LCD. Even an MCU reset would be eventually fine but I don't know how to check the LCD or i2c status.

Please note that I just started out and I have no background whatsoever so if you are so kind to reply, please keep this in mind.

Re: recover I2c stuck

Posted: Fri Sep 24, 2021 9:26 am
by fpiSTM
You can try to perform an end() then a begin().

Re: recover I2c stuck

Posted: Fri Sep 24, 2021 9:52 am
by Anz8
Do you mean by simply looping that? I like the simplicity :)

Another question, this is the code I use in setup():

Wire.begin();
sht.begin(SHT31_ADDRESS);
Wire.setClock(50000);

lcd.begin();
lcd.backlight(); // Turn on the blacklight and print a welcome message.
lcd.setCursor(4,0);
lcd.print("WELCOME1");
delay(3000);
lcd.clear();

are both wire.begin and lcd.begin necessary or can this generate conflicts and or issues?

Re: recover I2c stuck

Posted: Fri Sep 24, 2021 12:35 pm
by Anz8
I tested this:

void i2cRestart(){
if((millis() - lasti2cRestart) > i2cRestartFreq){
Wire.end();
delay(500);
Wire.begin();
delay(500);
lasti2cRestart = millis();
}
}

but as soon as it triggers it resets the entire board (even with the watchdog turned OFF)

Re: recover I2c stuck

Posted: Fri Sep 24, 2021 2:23 pm
by fpiSTM
@Anz8

Please use the code formatting for your sources.
Before posting try to understand and debug your code, we could not answer to all your request each time you get stuck. ;)
Maybe the begin/end was not the right thing or is not used correctly. Hard to tell as the library you used could not support this or it have a
side effect.

Re: recover I2c stuck

Posted: Fri Sep 24, 2021 4:28 pm
by mrburnette
Anz8 wrote: Fri Sep 24, 2021 8:50 am ...
Please note that I just started out and I have no background whatsoever so if you are so kind to reply, please keep this in mind.
Sure: we all start somewhere.
But, you have a responsibility to learn how to swim before jumping into deep water; we are not lifeguards and you will drown.
I have issues due to interferences from the relays module. I'm working on the hardware part but I'd like to implement some checks and recover on the software side.
Electrical noise will ruin your day. If you have studied relays, you know they are inductors. Do you know what happens when you switch an inductor on, then off?

There are numerous procedures, but the first to try is: https://resources.altium.com/p/using-fl ... r-circuits
flyback.png
flyback.png (7.26 KiB) Viewed 1787 times

Continue with this read: https://forum.arduino.cc/t/can-emi-lead ... ing/648202