Author Topic: Weird I2C issue, ESP8266 ,MCP23017  (Read 1451 times)

0 Members and 1 Guest are viewing this topic.

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Weird I2C issue, ESP8266 ,MCP23017
« on: August 17, 2020, 09:22:58 am »
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.
 

Offline mayor

  • Regular Contributor
  • *
  • Posts: 236
  • Country: ca
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #1 on: August 17, 2020, 11:23:29 am »
You're not showing your USB connection, or MCU reset, etc?
 

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #2 on: August 17, 2020, 11:50:51 am »
The board houses a full ESP8266 board like this one https://www.e-wireless.gr/images/detailed/255/nodemcu_lua.jpg
The connections between the boards can be seen in the schematic i provided.
 

Offline st_ashcroft

  • Newbie
  • Posts: 4
  • Country: gb
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #3 on: August 17, 2020, 12:25:28 pm »
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.
 

Offline Ice-Tea

  • Super Contributor
  • ***
  • Posts: 3095
  • Country: be
    • Freelance Hardware Engineer
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #4 on: August 17, 2020, 12:28:19 pm »
Have you tried powering from a 5V power bank?

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #5 on: August 17, 2020, 01:08:00 pm »
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: [Select]
void setup()
{
 for(int i=0; i<=9; i++)
  {
    mcp.pinMode(button[i],INPUT);
    mcp.pullUp(button[i],LOW);
  }
}
void loop()
{
if((millis()-input_read_counter) >= 250)
  {
    read_inputs();
    input_read_counter = millis();
  }
}
void read_inputs()
{
  //Buttons
  for(int i=0; i <=9; i++)
  {
    if(mcp.digitalRead(button[i]) == HIGH)
    {
      delay(10);
      if(mcp.digitalRead(button[i]) == HIGH)
      {
        button_pressed[i]=true;
        Serial.print("Button ");
        Serial.print(i);
        Serial.println(" pressed"); 
      }
    }
  }
}
 

Offline SmokedComponent

  • Regular Contributor
  • *
  • Posts: 60
  • Country: si
  • Emitting smoke
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #6 on: August 17, 2020, 01:47:32 pm »
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).
« Last Edit: August 17, 2020, 02:10:39 pm by SmokedComponent »
 

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #7 on: August 17, 2020, 05:22:35 pm »
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.
 

Offline SmokedComponent

  • Regular Contributor
  • *
  • Posts: 60
  • Country: si
  • Emitting smoke
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #8 on: August 17, 2020, 08:09:26 pm »
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.
 

Offline Nusa

  • Super Contributor
  • ***
  • Posts: 2417
  • Country: us
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #9 on: August 17, 2020, 09:30:08 pm »
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.
« Last Edit: August 17, 2020, 09:32:12 pm by Nusa »
 

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #10 on: August 18, 2020, 07:52:06 pm »
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.
 

Offline d-smes

  • Regular Contributor
  • *
  • Posts: 101
  • Country: us
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #11 on: August 19, 2020, 08:33:42 am »
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.
 

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #12 on: August 19, 2020, 09:12:41 am »
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.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9914
  • Country: us
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #13 on: August 19, 2020, 04:46:17 pm »
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
 

Offline ChrisGreece52Topic starter

  • Frequent Contributor
  • **
  • Posts: 829
  • Country: gb
  • Electronics Engineer - Hacker - Nerd
Re: Weird I2C issue, ESP8266 ,MCP23017
« Reply #14 on: August 19, 2020, 05:29:24 pm »
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.
« Last Edit: August 19, 2020, 06:10:58 pm by ChrisGreece52 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf