HTU21D problem with I2C

Hello forum,

It seems I have a problem with the HTU21D when trying to read the values with the particle example. It can not communicate with the HTU, this is the code

#include "HTU21D/HTU21D.h"



HTU21D htu = HTU21D();

void setup()
{
	Serial.begin(9600);

	Serial.println("HTU21D test");

 htu.begin();
	    Serial.println("HTU21D not found");
	    delay(1000);
	
	Serial.println("HTU21D OK");
	
}

void loop()
{
	Serial.println("===================");
	Serial.print("Hum:"); Serial.println(htu.readHumidity());
	Serial.print("Temp:"); Serial.println(htu.readTemperature());
	Serial.println();

	delay(1000);
}

Although when I use the Adafruit HDC100X everything works fine

Do you have any answer for that?

Regards
John

Hi @johnyrobot5

Do you have pull-up resistors (4.7k is ideal) on the i2c lines?

1 Like

No I am just connecting directly to the pins of photon and sparkfun module.

Do I have to put pull up?

Yes, I2C needs pull-up resistors, but no, you don’t need to add them because the SparkFun HTU21D includes them on the breakout board. Maybe post a picture of your wiring?

Hi ,

Thanks for the fast reply, I have triple check the wiring and it is OK. I suppose it must be something with the library.

Have you used it before?

I have not used the library. You could try the Sparkfun sample code for Arduino. Just comment out the #include Wire.h and it compiles on the Particle. I did not test the operation, but it compiles.

And, of course, you need to use the Particle pins for I2C, D0=SDA, D1=SCL, not the Arduino ones in the comment at the top of the file!

1 Like

We’d still like to see it :wink:
There were other people who triple checked and then had Photons just sitting on header pins - not soldered!

Or if the jumper between the two resistors wasn’t bridged, you’d need to do that

Hi ,

I have just checked it with an Arduino board and everything works fine. Maybe there is something wrong with the library of particle?

Can anyone check it and tell me?

I am using a photon 0.4.9 firmware.

This doesn’t prove a lot since the Arduinos tend to have I2C pull-ups on-board, but Particles haven’t.
So if it were about missing pull-ups that test would not make you any wiser.

But there are some threads already about HTU21 and some of them are marked as solved, so you might find something there (even in the ones about Adafruit boards)
https://community.particle.io/search?q=htu21%20solved

You could also check if the I2C address in the library is correct by use of the i2c_scanner sketch in this thread
Photon flashed once, now can't flash again

The thing is that I have tried in the past this sensor with photon and everything worked fine before some months with the same wiring in the same photon with the same firmware.

Also the official PCB schematic from sparkfun says that the pull up resistors are on it in the module.

So I think it must be something internally in particle photon .Can anyone from particle just tell us if the libraries are OK?

Okay? Why should then all out of a sudden the library (which is part of the firmware once it’s flashed to the device) have got something wrong with it?
Unless you had used the old version 0.0.1 back then (which would then not be the same firmware).
But this can easily be tested, if you just use that then (hit the info icon (i) next to the library name and select 0.0.1 and try it out).

BTW: This may have been an interesting information for the initial post.

I do know that, hence my comment in a previous post

The big red blob in the schematics marked SJ1 (and framed green on the image I posted) are two cut-open jumpers that are by default closed. And since we don’t know what you did or didn’t do to your device I just intended to make sure that the resistors (clearly present on the board) are actually active which they wouldn’t be if the jumper was open(ed).


BTW: Why are you not trying to use the unchanged sample code, which seems to have a while() in setup for some reason I’d guess?

No they are active , I have checked it with a multi-meter .I now have seen that the library has changed to a new version 0.0.2

I was using the old library 0.0.1

How can I use the old one back?

The only difference between these two versions is in HTU21D::begin()

  // v0.0.1
  Wire.begin();

  // v0.0.2
  if (!Wire.isEnabled())
    Wire.begin();

so if you just add Wire.begin(); before htu.begin() you have the same effect as if you went back to 0.0.1.

But I’d also put back the while() loop in setup().

And if this all doesn’t work, try out the i2c_scanner to see if your device is detectable at all and which address it has (expected is 0x40).

I have changed the library to 0.0.1

But still nothing happens . I can find the device in 0x40 because I can read it from an Arduino board.

I dont know what is wrong here. I am confused…

Just give the scanner a try.
The original SparkFun library has a comment next to the #define which states // unshifted address, so if we need the shifted one here, you’d find out with the scanner.

And what about the while()?

OK this is what I get from I2C scanner

Scanning…
I2C device found at address 0x40 !
done

What to do with the While?

The original sample has this in setup()

void setup()
{
	Serial.begin(9600);
	Serial.println("HTU21D test");

        while (!htu.begin())
        {
	    Serial.println("HTU21D not found");
	    delay(1000);
	}

	Serial.println("HTU21D OK");
}

Maybe the sensor is slow in responding and hence the original contributor had this implemented.