Page 1 of 1

HTU21D temperature/humidity I2C sensor

Posted: Fri Apr 17, 2015 4:39 pm
by manitou
Here's a simple proof-of-concept class for the HTU21D temp/humidity I2C sensor
https://github.com/manitou48/pyboard/bl ... /htu21d.py
Breakout board from Sparkfun https://www.sparkfun.com/products/12064
connected to IC1 on pyboard.

Code: Select all

    >>> from htu21d import HTU21D
    >>> htu21d = HTU21D()
    >>> htu21d.readUserRegister()
    b'\x02'
    >>> htu21d.readTemperatureData()
    25.93051
    >>> htu21d.readHumidityData()
    49.52673


Re: HTU21D temperature/humidity I2C sensor

Posted: Sat May 02, 2015 2:27 pm
by cloudformdesign
Awesome! The HTU has become like the defecto sensor to be supported, mostly because of its simple to implement and ubiquitous 1 wire protocol.

It's great to have another sensor supported

Re: HTU21D temperature/humidity I2C sensor

Posted: Mon Jan 15, 2018 7:44 pm
by GDorn
I'm trying to update this code to work with the new 'machine' paradigm, but running into a problem.

[CODE]
>>> import machine
>>> i2c = machine.I2C(scl=machine.Pin(14), sda=machine.Pin(12))
>>> i2c.scan()
[64]
>>> i2c.readfrom_mem(64, 227, 3) # 227 = TRIGGER_TEMP_MEASURE_HOLD
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 110] ETIMEDOUT
>>>
[/CODE]

The error changes on each read, though; sometimes it is ETIMEDOUT, sometimes ENODEV, and incredibly rarely it returns b'\x00\x00\x00'.

I'm pretty sure my wiring is correct, as i2c.scan() does return the right address (the device is locked to 0x40, which is 64), but attempts to read from it fail.

I'm pretty new to i2c and micropython, so maybe I'm using the wrong function to read from the device?

Re: HTU21D temperature/humidity I2C sensor

Posted: Tue Jan 16, 2018 12:09 pm
by SpotlightKid
Do you have pull-up resistors on the SDA and SCL lines?

I'm guessing (from the pin numbers you use), that you are testing with an ESP8266? The default I2C bus used there is a software implementation, which can use arbitrary pins, but you have to make sure you use pull-up resistors on those.

Re: HTU21D temperature/humidity I2C sensor

Posted: Fri Jan 19, 2018 4:52 am
by GDorn
SpotlightKid wrote:
Tue Jan 16, 2018 12:09 pm
Do you have pull-up resistors on the SDA and SCL lines?

I'm guessing (from the pin numbers you use), that you are testing with an ESP8266? The default I2C bus used there is a software implementation, which can use arbitrary pins, but you have to make sure you use pull-up resistors on those.
Yeah, I'm using an ESP8266 (the NodeMCU kit using a very recent micropython binary).

The HTU21D-F is a breakout board from Adafruit. The docs don't mention the pullup resistors, but I'm lead to believe that they exist, by this forum post: https://forums.adafruit.com/viewtopic.p ... 79#p422769

If pullups were needed and not provided, would i2c.scan() still reply with the address?

Re: HTU21D temperature/humidity I2C sensor

Posted: Sun Jan 21, 2018 7:41 am
by pythoncoder
The Sparkfun page states "This breakout board has built-in 4.7KΩ pullup resistors for I2C communications."

Re: HTU21D temperature/humidity I2C sensor

Posted: Sun Jan 21, 2018 6:10 pm
by GDorn
pythoncoder wrote:
Sun Jan 21, 2018 7:41 am
The Sparkfun page states "This breakout board has built-in 4.7KΩ pullup resistors for I2C communications."
That's the Si7021, the sensor that I did get working using a driver module aimed at a more current version of micropython.

Re: HTU21D temperature/humidity I2C sensor

Posted: Wed Dec 12, 2018 8:46 pm
by telcoserf
Which driver/class ended up working for you? I'm running into the EXACT same issues you were -- hoping I can get mine working with the one that ended up working on your end. Let me know -- thanks! :D

Re: HTU21D temperature/humidity I2C sensor

Posted: Thu Dec 13, 2018 1:57 am
by GDorn
I didn't quite finish the project, but my latest source code is here:
https://gitlab.com/georgedorn/tinysnake ... ter/client

It looks like I was last using the si7021. The driver is in there, along with attribution for where I got it. I can't remember if I needed to make any changes, but I think I'd have commented those, or they'd be in the changelog.