I2C connection randomly fails on P1 & Photon

After updating my P1 to 0.4.3, I have most things working, including I2C where I’m reading a MCP3421 ADC. However, I’ve run into a small issue with the I2C connection: Sometimes it works, and sometimes it doesn’t work.

I have my code setup to read data from the MCP3421 every second, so its well within its sampling rate limit. Sometimes when I power on the P1, the readings work fine. Other times, the connection to the MCP3421 fails until I reset the P1. Other times, it works fine for a few hundred readings, then it starts failing. And once it fails, it never reads again.

I don’t have a logic analyzer around to scope this and it appears to be quite random, so can anyone give me any pointers on how to make this I2C connection reliable? I’m using 22K pullup resistors on the I2C lines and I only have this one component connected on the line thus far.

Thanks in advance.

I’m having this issue as well on a Photon but this will require me to hook up a debugger to see which part of the code is causing problem. Will post here if i see anything. :smile:

I have also been having problems using the I2C on my Photons to communicate with an HTU21D. I can usually obtain readings from the sensor for some time but then I do not receive any more updates. I have a lot of reset routines in my code to try to reset the sensor if a problem is detected but nothing seems to work. I have also physically power cycled the sensor on the board but I am unable to receive any I2C messages until the Photon is reset.

I have connected my logic analyzer to the I2C lines. No ACKs are present in the data, though. Other times I have observed that data is not being sent at all.

I have 10k pull-ups because i didn’t have any 4.7k resistors in my office. I’m not sure if this is an issue.

The most success I have had using I2C was to space out the messages more. For example, instead of reading temperature immediately after humidity I will wait 1 second in between. This configuration seems to keep everything working.

I am using the release/photon_043 build on my Photon.

@kennethlimcp, @lige,

Thanks for the info. Its good to know this isn’t a problem only on my board.

I tried a few things today, and it seems like enabling clock stretching seems to make it more reliable. It still fails every now and then, but I think its better.

Do you guys check return values? request, write and end transmission all can give you error values if things act up.
What happens if you check for errors, and call Wire.begin again if you get an error? Does that fix the issue?

I always get a return value of 6 from Wire.endTransmission(true); after failing…

calling Wire.begin() again does make it start talking again… hmmm why didnt i think of that sooner? we shouldn’t have to do this so we need to work out why.

@peekay123, you may be interested in this development! in relation to this thread

@Hootie81, I believe the team is working on a solution and testing it as we speak.

2 Likes

wow great news! cant wait to test it out

till its ready i might as well get some sleep!

Great!

Hardware is hard, and you can’t expect it to work 100% of the time. You should always check error values and have a way to re-init the peripheral. In fact, I’m surprised the particle folks haven’t exposed a Wire.stop() or Wire.close() to turn off the I2C hardware.

That said, it looks like this is a common problem. So hopefully someone’s looking at it.

Good to know that there might be a fix soon. I’ve had problems with the Photon communicating with tsl2561 light sensor and htu21d hum./temp. sensor and the return code from wire.endtransmission() is always 4 (other error). It happens after few dozen minutes running all the time. I added a reset function for both sensors whenever an error is detected, sometimes it works, sometimes it keeps getting called indefinitely, so I just set up a max number of 5 attempts then stops everything and report the time stamp of the event.

Which HTU21D sensor would that be? Is that a ready one from Adafruit or SparkFun? I’m having major issues with the SparkFun one.

I cannot say for certain but I think there is only one type of HTU21D sensor. I ordered my batch from Taobao directly from China so I have not used the Adafruit or Sparkfun sensors. The main things to check would be your supply voltages (3.3 vs 5) as well as whether there are pull-ups on the I2C lines. What issues are you experiencing with the sensor? Do you have a Logic Analyzer handy to check what is actually being sent over I2C?

Sure I know the sensor is the same, the boards do differ a bit. I’ve checked the voltages and both work on Arduino perfectly, but on the Photon the SparkFun one doesn’t. I’m still figuring this out, it just prints nonsense. I don’t have a logic analyzer, but I’ll try to borrow a good oscilloscope. It could well be that the pull-up is too high on the SparkFun with Photon.

The breakout I am using is identical to this board:


If you’re using the Sparkfun Weather Shield (not Photon Weather Shield) it looks like the I2C lines are shifted to 5V.
Are you powering the HTU21D at 3.3V from the Photon with the I2C lines directly connected to D0 and D1?
Also, which library are you using on the Photon? I am using something very similar to the library in build.particle.io.

Yes directly to D0, D1 with 3v3 power (the power works with Arduino) is this the way you’ve connected as well. I’ve tested two libraries both which work with Adafruit one, but not with SparkFun even the one from build.particle.io works for Adafruit but not SparkFun.

Wow, that’s really strange. Everything sounds identical with your setup.

What code are you using in your loop() to read from the sensor? If you are using the build.particle.io library what type of values are you getting for htu.readHumidity() and htu.readTemperature()? Usually when my Photon fails to read data from the HTU21D I get the 998 “HTU21D_I2C_TIMEOUT” error return value.

No worries got it working now, I had accidentally taken the wrong resistors.

https://a248.e.akamai.net/f/1949/3326/8/www.blogcdn.com/massively.joystiq.com/media/2011/10/facepalm.jpg

Ok, I’m going to steer this discussion back to the I2C issue… :smile:

@mdma, @BDub - I know this is being worked on, but do you guys have an estimated ETA for the fix or at least a proper workaround? I have everything working on my board, and this is the only issue holding it back, so it would be great to get an idea of the timeline if you have any idea…

Thanks in advance.

You can give this a try and see how things go: https://github.com/spark/firmware/commit/09dd8096774c74b6680ddb47b39fc21134df49a8

Not sure if this applies, but, I had an I2C connection problem when trying to add a magnetometer to a 6DOF IMU. I made the wiring as short as possible. The problem went away.