recover I2c stuck

Post here first, or if you can't find a relevant section!
Post Reply
Anz8
Posts: 10
Joined: Mon Sep 20, 2021 8:08 am

recover I2c stuck

Post 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.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: recover I2c stuck

Post by fpiSTM »

You can try to perform an end() then a begin().
Anz8
Posts: 10
Joined: Mon Sep 20, 2021 8:08 am

Re: recover I2c stuck

Post 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?
Anz8
Posts: 10
Joined: Mon Sep 20, 2021 8:08 am

Re: recover I2c stuck

Post 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)
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: recover I2c stuck

Post 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.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: recover I2c stuck

Post 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 1779 times

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

Return to “General discussion”