HTU21D temperature/humidity I2C sensor

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

HTU21D temperature/humidity I2C sensor

Post by manitou » Fri Apr 17, 2015 4:39 pm

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


cloudformdesign
Posts: 35
Joined: Wed Mar 11, 2015 7:48 pm

Re: HTU21D temperature/humidity I2C sensor

Post by cloudformdesign » Sat May 02, 2015 2:27 pm

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

GDorn
Posts: 4
Joined: Mon Jan 15, 2018 7:29 pm

Re: HTU21D temperature/humidity I2C sensor

Post by GDorn » Mon Jan 15, 2018 7:44 pm

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?

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: HTU21D temperature/humidity I2C sensor

Post by SpotlightKid » 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.

GDorn
Posts: 4
Joined: Mon Jan 15, 2018 7:29 pm

Re: HTU21D temperature/humidity I2C sensor

Post by GDorn » Fri Jan 19, 2018 4:52 am

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?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: HTU21D temperature/humidity I2C sensor

Post by pythoncoder » Sun Jan 21, 2018 7:41 am

The Sparkfun page states "This breakout board has built-in 4.7KΩ pullup resistors for I2C communications."
Peter Hinch
Index to my micropython libraries.

GDorn
Posts: 4
Joined: Mon Jan 15, 2018 7:29 pm

Re: HTU21D temperature/humidity I2C sensor

Post by GDorn » Sun Jan 21, 2018 6:10 pm

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.

telcoserf
Posts: 1
Joined: Wed Dec 12, 2018 8:44 pm

Re: HTU21D temperature/humidity I2C sensor

Post by telcoserf » Wed Dec 12, 2018 8:46 pm

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

GDorn
Posts: 4
Joined: Mon Jan 15, 2018 7:29 pm

Re: HTU21D temperature/humidity I2C sensor

Post by GDorn » Thu Dec 13, 2018 1:57 am

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.

Post Reply