RC snubber for relays module

Thanks. Yeah, this won't fix the problem because I had exactly this module with this wiring previously and it was still bugged. Still, I change it back (the wiring, not the module that I keep with 2 relays only) just to avoid unnecessary interference.

image

Is there some protection I can put on the LCD module? Sometimes it's the only thing that crashes while the micro is still working. I have the classic LCD 16x2 module connected with I2C

As it is, the exact cause of the crashes is still not entirely clear to me. But in general:

Not really, because it's not clear if the module itself crashes, or (more likely IMO) a fault condition emerges on the I2C bus that apparently isn't recovered from. Perhaps @Koepel would chime in on this as he has done extensive work demystifying the I2C bus and also can suggest a way to recover from a fault condition on I2C (although not with 100% success guarantee).

In any case, trying to patch this up on the display end would indeed be just that - a patch. It's preferable to attack the actual cause.

Hi,

How far is it between your MCU and display?

How have you wired it with the SHT module on the I2C bus?
What model Arduino is your MCU.

Tom... :smiley: :+1: :coffee: :australia:

I think the crashes are due to the relay module becuase I tried letting it work without loads on a pretty high freuency of activation/deactivation of the relays (every minute) and it didn't crash for more than a hour. That said, maybe I've been lucky but with the loads it happens sooner than after a hour. Another thing that comes to my mind is that the project is programmed to start one relay on a given time frequence (= 1 min on and 4 off), the other relay is activated when the sensor signal the humidity is below a given level and it runs until it reaches another higher level. I tried let it run just on a time table (3 cycles of 1 min on and 4 off for the fan and 1 cycle for the humidifier and the stability seems higher, still crashes after x cycles though)

As @anon35827816 said, there could be some problem with the I2C since the 2 things that most often crash are the LCD module and the SHT sensor, that are on the same I2C connection.

@TomGeorge I'm using a bluepill F103C8. The display is close to the board.

This is the wiring:

The relays module pins were initially near the SHT pins, then I moved them away from the matrix board. I'm not sure it helped. The power is given to the MCU through the USB.

Hi,
Are your wires between the BluePill and the relay module running alongside the mains power wires?

Can you please post some images of your project please?
The MCU end and the relay module end.

The BluePill is 3v3 logic isn't it?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Yes there is. A 100 µF capacitor directly across the driver 5 V supply - pins 1 and 2 of the 1602 module - may help.


I frequently stress the absolute need to keep the wiring from one part to another together, power wire with ground return; control wire with its return (which return for many common relay modules, is not the ground). The idea is to avoid loops which permit inductive coupling; we are dealing with devices which are operating at HF (3 to 30 MHz) radio frequencies here, electromagnetic transmission is a very real matter.

You always need to keep the high (mains) voltage physically and clearly separate from the low voltage for safety, but it is also most important to avoid the switching impulses that can cause the arcing of relay contacts, from being coupled back into the microprocessor circuitry and causing code and hardware such as displays to malfunction and crash.

Coupling can be both electrostatic over the stray capacitance between one wire and another, and electromagnetic where a loop of wire in which the current transient occurs, inductively couples an another nearby loop of wire in the signal and control circuits. This is why it is important to keep both wires of any given circuit together so that the electric field from any one wire tends to be cancelled by the adjacent ground (and supply) return and they do not form any open loops which can produce or pick up a magnetic field.

There are 3.3V LCD modules, but most are 5V. They really need 5.0V and not 4.5V. The internal circuit inside is high impedance, so electrical noise can go right through the liquid crystal screen into the circuit behind it.

A 5V LCD display with a 3.3V STM32F103C8 will not work.
A flat ribbon cable with SDA next to SCL will cause trouble.
I suggest to lower the I2C impedance with pullup resistors to make the sink current close to 3mA. The sink current is the current that is required to make SDA or SCL low.

Can you show a photo of the wires and the I2C modules ?
Can you give links to the modules on the I2C bus (a link to where you bought them).

The Wire library for the AVR family boards has since recently a timeout in case of a I2C bus error. I don't know about the Wire library for the STM32 and I don't know how the overall compatibility with the Arduino Wire library is.

This is the wiring. The power is all 5V given by the board except for the 3.3v given to the sensor. Initially it was 5v that too but I tried to use the 3.3V because it was crashing. The relays pins were near the LCD and sensor pins and then I moved them on a separate board. From there, 3 1.5mt long wires go to the relay module which is far 60-70cm on a different level (ground) while I keep the board on a chair. This is the fron view:

This is the sensor module:

and this is the LCD: https://www.amazon.it/gp/product/B01E4YUT3K/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1

it's not clear from the drawing, the first yellow circle below the SCL is the 3.3v for the sensor (4 pins, not 3)

Don't run a 3V3 sensor in a 5V system without the necessary level shifters. You now have a mix of 5V and 3.3V devices on the I2C bus and that means somethings got to give. Get that sensor back to 5V if it supports it (check the datasheet for its maximum supply voltage).

Then there is indeed a voltage mismatch on the I2C bus.

  • The STM32 processor is 3.3V (there is a LDO voltage regulator on the Blue Pill to make 3.3V from 5V).
  • The SHT31 sensor is 2.15 to 5.5V, but its I2C bus must match its power.
  • The display is 5V.

The display is the odd one. It is expecting a 5V I2C bus, but it only gets 3.3V levels for SDA and SCL.
The display seems to have 4k7 pullup resistors that try to pull SDA and SCL towards 5V, possibly damaging the processor.

I would not expect this to run reliable for 1 minute. Regardless if this is the cause of your problem or not, you have to fix the I2C bus. A level shifter for the display is enough to fix it, but a level shifter also makes the signal a little weaker.

@anon35827816 it is a 3.3V system because of the STM32 processor (the sensor can do both).

Ok; I see. The presence of a 5V LCD confused me. I see you also have your doubts about this. Btw, I often find that 5V I2C devices work with a 3.3V bus - but it's sensible that this would bring reliability issues.

If the display does not get 5.0V power, but for example 4.7V, then the 3.3V signal levels come more in range. The STM32 has pins that can be 5V tolerant, so it would be possible to make a 5V I2C bus and everything should be more or less fine.

But I don't want to get too funky with this. If there is trouble, then fall back to something that is straightforward. A 3.3V processor with a 3.3V I2C bus is straightforward.

When the contacts are open, will there not be a small AC current flowing constantly?

Let's say R=220 and C=100nF (the latter is a guess; you can take 22nF or so if you prefer). -3dB point of that RC combination is a bit over 7kHz. AC grid frequency is 50Hz or 60Hz depending on where your bed sleeps.

So, next to wiring back the sensor to the 5v, the only possible solution would be buying a 3.3V module? or buying a level shifter but the price, considering amazon and multiple pieces order is basically the same

Why not? The HD44780 works perfectly with 3.3 V as it does in the 3.3 V version.

The contrast system requires (slightly) more than 4.5 V; the 3.3 V version of the module includes an ICL7660 charge pump purely to drive the contrast logic, not the HD44780.

So you drive a display with 3.3 V logic but power it with 5.

I expect it to work well and no damage to the processor may happen:
The I/O pins have protection diodes to Vcc. When the 5 V pull-up tries to pull an I2C line over Vcc the diodes will clamp the voltage to Vcc + diode drop. About 3.9 V in this case. This also means the HIGH on the bus won't be marginal 3.3 V but more reliable 3.9 V. And the "injection current" is mere (5 - 3.9)/4k7 ~ 200
uA. The pins of "blue pill" are specified for 5 mA. Should be OK even considering slightly worse performance of a possible clone.

Of course there must be something that uses this injected 400 uA on the 3V3 rail. Unless some very low power sleep mode is used I don't think it is an issue.

I don't want to say the I2C bus is OK. I2C + noise = problems! I just think the noise susceptibility is the only problem of the setup that won't be fixed by level shifters/change of devices. I would consider SW fix to recover from I2C errors.

There is a I2C backpack.
Thanks for the information, I had trouble with the cheapest 5V displays at a slightly lower voltage. Good to know that the HD44780 can operate at lower voltages.

I agree, but I first want to make a good match on the I2C bus, and then lower the impedance of the I2C bus and get closer to the maximum of 3mA sink current.
I don't know the Wire library of the STM32 branch and how it can recover from I2C bus errors.