Weird I2C issue, ESP8266 ,MCP23017 - Page 1
Forum section: electronics / projects

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 17, 2020, 09:22:58 am
content: Hello, I have completed my "smart" thermostat project and i am facing some weird issue with the MCU i am using.Issue : Since the on board MCU cannot supply all the components on its own, the board is always powered using mains (see schematics below). When i have the board connected to my PC via USB everything works fine.The moment i unplug the USB cable and i reset the MCU things get wild.The MCP23017 used to control the state of two relays and read button inputs, reads random inputs and turns on and off the relays. I have no control over the buttons once the USB cable is unplugged.Troubleshooting steps i followed :1) I tried disabling Serial communication and every serial.print/println statement, to make sure the problem was not caused by the MCU trying to relay messages to the Serial Console.2) I tried removing the SCL and SDA pull-up resistors.3) I tried enabling and disabling the internal pull-ups of the MCP23017.I am kinda lost on what to do or what to measure. Since the MCU does not communicate via USB and the MCP works fine when connected via USB i think this might be a supply issue.The power supply on my board utilizes a 220VAC to 5VDC module , more specifically the HI-LINK HLK-20M05 which is more than capable of powering everything on the board ( the ESP8266, 20X4 LCD Display and 2 Relays are the most power hungry components on there).Since i have no idea on how to proceed i posted the schematics for the board below. I am so sorry if i missed something and i thank you in advance.If there is anything i could clarify please inform me.

author: mayor
author_rank: Regular Contributor
timestamp: August 17, 2020, 11:23:29 am
content: You're not showing your USB connection, or MCU reset, etc?

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 17, 2020, 11:50:51 am
content: The board houses a full ESP8266 board like this onehttps://www.e-wireless.gr/images/detailed/255/nodemcu_lua.jpgThe connections between the boards can be seen in the schematic i provided.

author: st_ashcroft
author_rank: Newbie
timestamp: August 17, 2020, 12:25:28 pm
content: I'd be looking at the following:Running MCP23017 on 5V but interfacing to 3.3V I2C bus of ESP8266.No decoupling cap on MCP23017.I expect the USB 5V rail is much smoother than the output of the PSU and so you got away with things being a bit marginal.

author: Ice-Tea
author_rank: Super Contributor
timestamp: August 17, 2020, 12:28:19 pm
content: Have you tried powering from a 5V power bank?

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 17, 2020, 01:08:00 pm
content: Thank you for your input.The board cannot be powered using just the USB connection on the MCU due to the plethora of the peripherals.The problem occurs when i unplug the ESP from the PC. I added an additional 100uf capacitor on the VIN and GND pins of the ESP board, that seems to help a bit but still the problem persists.The MCP is powered by the 5V power supply as the ESP (it is getting powered from the VIN pin that then goes to the regulator to supply the 3.3V for the ESP module)I already have a 100uf and a 100nf capacitors on the power supply output.I am starting to suspect the code after some tests i did.The code is about 2000 lines so i will post the parts responsible for reading the inputs using the MCP23017.Code:void setup(){for(int i=0; i<=9; i++){mcp.pinMode(button,INPUT);mcp.pullUp(button,LOW);}}void loop(){if((millis()-input_read_counter) >= 250){read_inputs();input_read_counter = millis();}}void read_inputs(){//Buttonsfor(int i=0; i <=9; i++){if(mcp.digitalRead(button) == HIGH){delay(10);if(mcp.digitalRead(button) == HIGH){button_pressed=true;Serial.print("Button ");Serial.print(i);Serial.println(" pressed");}}}}

author: SmokedComponent
author_rank: Regular Contributor
timestamp: August 17, 2020, 01:47:32 pm
content: Is it possible, that, when MCU doesn't have to intialize serial/USB, it boots earlier into setup() than IO extender is fully powered, out of reset and ready to receive first I2C command? Try adding some short delay into the beginning of setup(). Easy to try if nothing else.Also where do the IO extender's address pins A0-A2 go? They should remain fixed (hard-wired to VDD/VSS).

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 17, 2020, 05:22:35 pm
content: The address pins are floating, i have placed some solder pads on the board so i can use the extra pins if something was to be added)From the code you can see that i am actually using 3 extra pins of the MCP23017 for buttons.I will read the datasheet of the MCP to ensure that floating address pins cannot cause it to misbehave.As for the delay i will try adding different values to see how the MCU behaves.

author: SmokedComponent
author_rank: Regular Contributor
timestamp: August 17, 2020, 08:09:26 pm
content: From what I recall (I used the SPI version of the IC), datasheet does state the address pins must be externally biased and not left floating.

author: Nusa
author_rank: Super Contributor
timestamp: August 17, 2020, 09:30:08 pm
content: From your description, the first thing I'd try is plugging in a USB cord that has the +5V line cut. If the behavior changes, then the issue is hardware power-related. If it stays the same, it's something else (likely boot-related behavior).The other thing was suggested above, but not sure you heard him. Try an alternate (non-mains) 5V power source for the main 5V rail. If that changes the behavior, then your 5V supply is too noisy/unstable.

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 18, 2020, 07:52:06 pm
content: I modified a micro USB cable i had around and cut the +5V cable. The board is functioning normally. Problem is now that i disconnected the cable fully the problem does not appear. I have no idea why this is happening but it seems like the problem is solved. I am starting to think that it was a software issue but still i am not sure.As for the address pins A0,A1 and A2 i now saw in the MCP23017 datasheet that they must be externally biased. I will do a bit more research and bias the crap out of those address pins to make sure they do not cause an issue.

author: d-smes
author_rank: Regular Contributor
timestamp: August 19, 2020, 08:33:42 am
content: It could be the secondary circuits (5V stuff) need an Earth ground reference which is what the USB cable is providing. Try a direct connection from 5V GND signal to Earth / PE ground. Instead of a direct connection, a few nF of capacitance to Earth may also do the trick.

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 19, 2020, 09:12:41 am
content: That's another issue, this board works as a thermostat, the cables that come out of the wall for the connections include only L,N and two wires for the burner and the water heater. No earth cable is included.Since i am powering the thermostat via a wall outlet at the moment for troubleshooting i connected the shell of the USB type A to earth while the micro USB end was connected to the MCU. No difference at all.

author: rstofer
author_rank: Super Contributor
timestamp: August 19, 2020, 04:46:17 pm
content: If you are still using I2C, the pull-up resistors are required and should be on the order of 2.2K. You will see schematics where the author uses 10K or even higher but the circuit is unlikely to comply with the standards in terms of rise time. NXP wrote the specifications for I2C:Section 7, Page 55, goes into the calculation. Generally, 4.7K will work but 2.2K works better when the capacitance is just a guess.You're working in the very bottom left corner of the graph and it is mostly useless because a lot is happening within one grid line of the origin.https://www.nxp.com/docs/en/user-guide/UM10204.pdf

author: ChrisGreece52
author_rank: Frequent Contributor
timestamp: August 19, 2020, 05:29:24 pm
content: I am currently using 1k pull-ups since i had no bigger value available at the moment. From what i read and considering the layout of the board i do not think that would cause an issue.