Abnormalities when using an MCP23017 as out-/input

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Abnormalities when using an MCP23017 as out-/input

#1 Post by Micha_he » 05 Jul 2021, 11:21

I've configured a ESP12F with a MCP23017, 6 input-/output-devices. All set (MCPGpio,x,0) to 0 at boot and non of my rules is executed.
At the MCP-ports is an optucupler connected, with a 1,5k resistor, second side of the optocupler is connected to ground.

But at irregular intervals the signal on the 6 devices is change to 1 for less than 1sec, always a different port alternately.

Is it a problem to use the MCP-port as output (control the valve) & input (to publish the actual state) at the same time?
Should I use another configuration ?

In my opinion, the same configuration worked with firmware from last year without any problems.
I've also already provided the inputs with an additional pull-down resistor, no changes.

Here's the part from the log:

Code: Select all

80253494: EVENT: Clock#Time=Mon,11:11
80253698: EVENT: Clock#Time=Mon,11:11 Processing time:204 milliSeconds
80282040: WD : Uptime 1338 ConnectFailures 0 FreeMem 15672 WiFiStatus WL_CONNECTED 3 ESPeasy internal wifi status: Conn. IP Init
80282933: LoopStats: shortestLoop: 922 longestLoop: 896405 avgLoopDuration: 1430.00 loopCounterMax: 32537 loopCounterLast: 20784
80282933: Scheduler stats: (called/tasks/max_length/idle%) 20784/1889/16/66.73
80288287: MCP : Port=5 State=1 Output value=1
80288300: MQTT : Bewaesserung/Ventil5/Status 1
80288306: EVENT: Ventil5#Status=1
80288515: EVENT: Ventil5#Status=1 Processing time:209 milliSeconds
80288532: MCP : Port=5 State=0 Output value=0
80288544: MQTT : Bewaesserung/Ventil5/Status 0
80288551: EVENT: Ventil5#Status=0
80288843: EVENT: Ventil5#Status=0 Processing time:292 milliSeconds
Thanks, Micha.

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#2 Post by TD-er » 05 Jul 2021, 12:08

At the MCP-ports is an optucupler connected, with a 1,5k resistor, second side of the optocupler is connected to ground.

The last part makes me wonder; "which ground?"
If it is the same GND as the ESP circuit, then why use optocouplers?

What does it control? You mention a valve, which sounds like a solenoid.
But what's inbetween the solenoid and the optocoupler?

Is the GND of the circuit switching the valves and the GND of ESP actually connected and if so, is it really needed?
Based on the logs, it looks like the logic state of the MCP does change, but with only a connected LED, that's not making sense (except for bugs in the firmware of course)
So I wonder if you may have unexpected currents in your circuit which may induce some strange spikes on the MCP inputs.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#3 Post by Micha_he » 05 Jul 2021, 13:47

Yes, ground is the same. I use this mosfet driver: https://www.pollin.de/p/bausatz-mosfet- ... 00w-810329 to control a 12v solenoid.

The state at the MCP-pin can't change! Only the led from the optocupler is connected. GND from ESP and GND from the solenoid are not connected to each other.

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#4 Post by TD-er » 05 Jul 2021, 13:51

Does this state flipping happen when a valve has switched?

User avatar
Ath
Normal user
Posts: 958
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Abnormalities when using an MCP23017 as out-/input

#5 Post by Ath » 05 Jul 2021, 14:01

What length are the wires between the MCP23017 and your mosfet board?
What other wiring is near those wires (possibly causing cross-talk spikes?)
/Ton

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#6 Post by TD-er » 05 Jul 2021, 14:07

Also do you have diodes over the solenoids? (some mosfet do have them included)

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#7 Post by Micha_he » 05 Jul 2021, 14:32

TD-er wrote: 05 Jul 2021, 13:51 Does this state flipping happen when a valve has switched?
No, possibly rather if it wasn't switched for some time.
Ath wrote: 05 Jul 2021, 14:01 What length are the wires between the MCP23017 and your mosfet board?
What other wiring is near those wires (possibly causing cross-talk spikes?)
About 8-10cm. No other wiring only the wires to these 4 inputs and maybe 1 or 2 wires from the default MCP schematic (address, SDA, SCL).
TD-er wrote: 05 Jul 2021, 14:07 Also do you have diodes over the solenoids? (some mosfet do have them included)
These are already on the MOSFET driver.

And the ESP & MCP is in standby (no solenoid is changed) while I can see this problem in the logs.

User avatar
Ath
Normal user
Posts: 958
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Abnormalities when using an MCP23017 as out-/input

#8 Post by Ath » 05 Jul 2021, 15:06

Micha_he wrote: 05 Jul 2021, 11:21 In my opinion, the same configuration worked with firmware from last year without any problems.
I've also already provided the inputs with an additional pull-down resistor, no changes.
Any version/release that you know that it is working as expected (without intermittent triggers)? There have been changes to the PCF/MCP plugins, AFAICS, though I can't validate what could be a cause of this report.
/Ton

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#9 Post by Micha_he » 05 Jul 2021, 15:23

It's a little bit complucated... I've update this device a few times, before I see this problem.
If I found some time while the next days, I flash some older versions. But these tests take some time, because this state-flip does not occur immediately.

Zodiac69
Normal user
Posts: 74
Joined: 13 Jun 2016, 17:20

Re: Abnormalities when using an MCP23017 as out-/input

#10 Post by Zodiac69 » 05 Jul 2021, 20:26

Hi Micha_he

It might be a silly question but, have you pulled your MCP23017 address lines to the same GND as your ESP?
For Address 1, all address lines of the MCP should be tied to GND - 0V

I had a case where the MCP lines were floating, it worked 100% until there was a relay switched.
The unit would then have strange behaviour and sometime even drop communications.
Any "noise" would make an output port switch on or make the unit crash.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#11 Post by Micha_he » 05 Jul 2021, 22:09

Yes, all address-lines are connected to GND, ESP-GND and MCP-GND is the same.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#12 Post by Micha_he » 06 Jul 2021, 13:10

I'm back to 20181231, in steps of ~ 1/2 year.
The problem still exists in all versions!

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#13 Post by TD-er » 06 Jul 2021, 13:19

So it is a bit more likely this is caused by something in your circuit or use case. (still possible it is a bug, but you're the first to mention it)

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#14 Post by Micha_he » 06 Jul 2021, 13:58

I'm currently create a test setup, with another ESP and MCP, but only with resistors on the ports. We'll see...

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#15 Post by TD-er » 06 Jul 2021, 14:04

The optocouplers, how are the LEDs from it being used?
Does the MCP source the power or does it connect the LED to GND?

The LED does have a voltage drop, so maybe that's the reason it may trigger something?

Maybe it is better to let the MCP drain the current of the LEDs?

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#16 Post by Micha_he » 06 Jul 2021, 14:56

I hope I haven't misunderstood you now...

Yes, that's how it's connected. MCP drive the signal and the LED is connected to GND. Look at this input-circuit:
[attachment=0]RB4-4_In.jpg[/attachment
Attachments
RB4-4_In.jpg
RB4-4_In.jpg (20.78 KiB) Viewed 977 times

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#17 Post by TD-er » 06 Jul 2021, 15:39

My electronics knowledge is just based on reading and experimenting, so I might be wrong.
Imagine the wires do pick up some noise.
When using a simple resistor, the noise induced voltage will not exceed a few mV as you need to have quite some energy to generate enough current to get a higher voltage over this resistor.
But the amount of current through a diode can be a lot less to get a voltage drop of a few Volt, depending on the diode. (the color of the LED)

So maybe it is better to have a much lower voltage drop there, by using a transistor to switch the LED in the optocoupler?
Image

Then the max. induced voltage over the IO-pin will likely be at 0.7V max.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#18 Post by Micha_he » 07 Jul 2021, 13:38

We can exclude the optocouplers as the cause, because I disconnected them and replaced it with a simple pull-down-resistor.
But the problem still exists :(

The test-setup (I mean, same handmade config, same rules,same firmware but ESP is Wroom02-1M instead of ESP12F-4M) works without errors !

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#19 Post by TD-er » 07 Jul 2021, 13:48

Could it be an issue of I2C communication which may be affected?
Have you tried to add some pull-up resistors to both I2C pins? (e.g. 4k7)

How is the ESP powered? And the MCP?

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#20 Post by Micha_he » 07 Jul 2021, 14:06

SDA & SCL have pullup's with 4,7k.

The power-supply is a Hi-Link PM03 with a 220µF cap at the outside, for ESP & MCP. ESP & MCP have additional a cap of 100nF near it.
Next week I look at the power-supply with a oscilloscope.

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#21 Post by TD-er » 07 Jul 2021, 14:35

Maybe also have a good look at the quality of the GND.

mrwee
Normal user
Posts: 217
Joined: 31 Aug 2016, 12:52

Re: Abnormalities when using an MCP23017 as out-/input

#22 Post by mrwee » 08 Jul 2021, 11:10

Just a small comment. My recent valve-project is based on a orignal Wemos D1 mini Pro and a MCP23017 used for I/O and I don't see any issues like you're describing.
Was running "ESP_Easy_mega_20210503_normal_ESP8266_4M1M", but I'm on "ESP_Easy_mega_20210615_normal_ESP8266_4M1M" now, since the Wi-Fi issues are hitting me quite bad.

Hope you find the root-cause

User avatar
ThomasB
Normal user
Posts: 829
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Abnormalities when using an MCP23017 as out-/input

#23 Post by ThomasB » 08 Jul 2021, 18:31

Try longer debounce times. And perhaps enable Safe Button to see if it helps.

- Thomas

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#24 Post by Micha_he » 08 Jul 2021, 21:51

Debounce time of 250ms (more isn't available) didn't help.
'Safe button' I'll try out... First I'll check the power supply.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#25 Post by Micha_he » 21 Jul 2021, 08:10

These are the things, which were unsuccessful:

Check Hi-Link 3,3V power supply: Interference frequency ~ 100kHz, ~80mV. No abnormalities.
Another 3,3V power supply
Additional 470µF capacitor at 3,3V power supply (interference frequency ~ 100kHz, ~80mV)
Reduce the SCL/SDA pullup from 4,7k to 1,8k, because the rise-time was to long (900ns) for 400k-I2C
Thicker wire for SCL/SDA
Another position for SCL/SDA-wiring
Reduce I2C-clock to slow

None of this brought any improvement. Now I'll test with enabled 'Safe button'...

Remark: The duration for this wrong/unclear signal change is always ~ 250ms !?

Question: If the I2C-communication isn't successful or has errors, we see this in the log?

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#26 Post by TD-er » 21 Jul 2021, 15:19

Not sure if you will see anything in the logs, but if the I2C hangs you can see this in an I2C scan which will return an error on each possible address.

Also for testing you could try to reduce the I2C frequency if you're having doubts on stability at higher frequency.

Are you using long cables if you do see a rise time of 900 ns?
Lowering the pull-up may increase the rise time of the 2 I2C lines, but the connected devices must also be capable to source the higher current and it should not affect the power supply to the chip.

It is a good practice to add 100 nF capacitors as close as possible to the power pins of each I2C chips.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#27 Post by Micha_he » 21 Jul 2021, 15:45

No long cables. The ESP and the MCP are on the same PCB. Wires (https://de.wikipedia.org/wiki/F%C3%A4deltechnik) are ~ 6-7cm.
I can't explain where the high capacity comes from. Only ESP, 6-7cm wiring and the MCP. Pullup was 4,7k and the rise-time ~900ns ???

Reduce the I2C hasn't anything improved, as I already write...
Both chips have their 100nF capacitor !

TD-er
Core team member
Posts: 5094
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Abnormalities when using an MCP23017 as out-/input

#28 Post by TD-er » 21 Jul 2021, 16:51

For 400 kHz you could still use 900 ns as the period is 2.5 usec, so you're below 50%.
What do you count as the level above which you stop measuring?
And what is the fall-time? (measured at the ESP and at the chip)

Can you experiment with a setup with thicker wires?
Capacitance is only one explanation of the slow rise time.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#29 Post by Micha_he » 21 Jul 2021, 18:13

This is a statement from the specification: The rise-time of SDA/SCL to be a maximum of 300 ns in "Fast-mode”

You mean the level to stop the measuring the rise-time? 70% of Vcc (3,3V) = ~2,3V

Rise-time with 4,7k (resulution 2µs):
rT4k7.jpg
rT4k7.jpg (2.54 MiB) Viewed 432 times
Rise-time with 1,8k (resultion 1µs):
rT1k8.jpg
rT1k8.jpg (2.42 MiB) Viewed 432 times
I've tried thicker wires. No improvement. Same rise-time.

User avatar
ThomasB
Normal user
Posts: 829
Joined: 17 Jun 2018, 20:41
Location: USA

Re: Abnormalities when using an MCP23017 as out-/input

#30 Post by ThomasB » 21 Jul 2021, 21:17

Is it a problem to use the MCP-port as output (control the valve) & input (to publish the actual state) at the same time?
Do you mean that for each optocoupler, the MCP's input and output pin is the same one? If this is how it is wired then the pin read isn't from the *latched* output state. Instead, the I/O pin becomes Hi-Z (input mode) and then it reads the "open" (un-driven) circuit logic level. So whatever circuitry is connected determines the logic level that is read, not the MCP output state.

In your case the Opto cathodes are pulled high. So even if the MCP pin was driven low, there is a chance that the level might be logic high on a read. The chance of a high level depends on the rise time of the newly open signal path. BTW, Pulldown/Pullup resistor band-aides are not a reliable solution since you are interested in the MCP's latched output state, not the logic level seen on the un-driven pin.
Should I use another configuration ?
Assuming that the same MCP pin is currently doing the reading and writing, I propose two alternate ideas:

(1) Only use the MCP to drive the outputs (no reads). Store the output states in internal vars (%v%). Instead of reading the I/O pin, read the saved system's internal var states. If you need values that are retained after a warm reset then use dummy plugin vars instead.

(2) Use separate MCP pins to read the output pins. That is to say, one set of pins for output-only, another set for input-only.

FWIW, the MCP23017 supports two different pin read modes. It can read either the logic level on the I/O pin (GPIOx registers) or the output latch state (OLATx registers). ESPEasy currently reads only the GPIOx states. Reading the OLATx would require enhancing ESPEasyGPIO.cpp code. All possible (send hugs & beers to developers), but the internal var workaround is a practical solution.

Final thoughts. I am not sure if your issue is related to my suspected fault that's explained above. But it's high on the list of reasons for the unreliable pin reading behavior.

- Thomas

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#31 Post by Micha_he » 22 Jul 2021, 08:42

With "out-/input at the same" I mean:

Out = I set the output with the MCPGPIO-command in my rules

In1 = The MCP GPA0-5 are also configured as switch-input-task, with a connection to my MQTT-broker. Here's my problem: I receive high-level for ~250ms at my broker. Always another input, and the the level at the MCP-pins are not changed.

In2 = The state of this switch-input-tasks is additional published via rules

Here's my schematic (current R4/R5 are 1k8):
Beregnungs-Steuerung_mit_MCP23017_und_ESP12F_Schaltplan.png
Beregnungs-Steuerung_mit_MCP23017_und_ESP12F_Schaltplan.png (289.77 KiB) Viewed 386 times
This is my configuration:
Tasklist.jpg
Tasklist.jpg (111.36 KiB) Viewed 352 times
Task.jpg
Task.jpg (44.38 KiB) Viewed 352 times
@ThomasB: Your idea #2 is worth considering. I'd also already thought of using the GPB for this purpose.
Last edited by Micha_he on 22 Jul 2021, 18:28, edited 1 time in total.

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#32 Post by Micha_he » 22 Jul 2021, 12:00

First results from the "Safe-Button" test:

I've change the first 3 MCP-Switch-Input's to Safe-Button activated, last 3 not. Within the last 16h, only the last 3 task's were change to 1 :P

Micha_he
Normal user
Posts: 317
Joined: 07 Feb 2018, 19:14
Location: Helmstedt, Germany

Re: Abnormalities when using an MCP23017 as out-/input

#33 Post by Micha_he » 22 Jul 2021, 17:59

I was probably too fast... :(

Now I get a state = 4 from my 3 changed MCP-switch-inputs.

Edit: Aha, now I mean I've understand the value of "4" (documentation for the "Safe-Button" doesn't point to this value). The value of "4" mean, the length of the signal was too short, or ?

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 22 guests