Universal-Solder had a year end sale. Couldn't resist grabbing a small pile of toys. One of the devices I bought was an Asair AHT21B temperature and humidity sensor. Asair says, "AHT21 is equipped with a newly designed ASIC dedicated chip, an improved MEMS semiconductor capacitive humidity sensor element and a standard on-chip temperature sensor element." and "The sensor outputs calibrated digital signals in standard I2C format." Sounds great.

In the pictures I still have the LP55231 on the breadboard. It's on the same i2c channel at address $32. The AHT21 is at $38, so they work fine together.

I wired it up and started writing some code. But the datasheet is a joke. It's poorly written and missing important information, like a list of commands, for starters. For cryin out loud, Asair! You can do better than that.

I went to download their example C source code and it would not download on my bench machine. I later tried again on another box, also running Ubuntu 20.04, and it downloaded just fine. No idea what all that was about.

But the example code still left me with questions. It does some things that are not mentioned in the datasheet, and does some things differently than described in the datasheet. My final demo code below just copies some things their code did, though I don't know what they do. You'll see those things commented as "unknown".

Some of that stuff can be left out anyway. I added the init and what seems to be calibration stuff to see if maybe that would correct what seemed to be a slightly high temperature reading. Nothing changed, but I left it in after.

The sensor works very well. It responds quickly and seems accurate, though temperature seems like it might be maybe a degree or so high. Maybe their conversion calculation isn't quite right, or maybe my Honeywell T4 Pro house thermometer isn't calibrated accurately? Later I'll grab another thermostat out of the work van and set it right beside the sensor to compare. Perhaps I'll end up tweaking the conversion calculation. We shall see...

... Next day...

I set a Honeywell Pro 1000 thermostat beside the sensor and left it for some time for temps to stabilize. The Pro 1000 reads about 1 degree higher than the AHT21B. My T4 Pro house thermostat is about two feet higher than the AHT21B and in another room, and it reads about one degree lower than the AHT21B.

So... I don't know. For now I'm going to assume that the AHT21B is dead on correct, and that the thermostats are off a little.

Simple MicroPython demo:

# ghmicro.com
# AHT21B temperature/humidity sensor - hardware I2c
# (Can't find good datasheet - the official one is missing info,
#  and the example code doesn't help)

import time
from machine import I2C, Pin

i2c = machine.I2C(1,freq=400000)
quad = bytearray(4)
six = bytearray(6)
temp = bytearray(1)

def write4(one,two,three,four):
    quad[0] = one
    quad[1] = two
    quad[2] = three
    quad[3] = four
    i2c.writeto(0x38,quad)

#init
write4(0x70,0xa8,0x00,0x00)     #unknown
pyb.delay(10)
write4(0x70,0xbe,0x08,0x00)     #calibrate sensor?
pyb.delay(10)

while 1:
    write4(0x70,0xac,0x33,0x00)    #trigger measurement (not sure the 0x70 is necessary)
    pyb.delay(80)                  #wait for measurement
    #should check status here, but delay is long enough
    temp[0] = 0x71                 #unknown
    i2c.writeto(0x38,temp)
    six = i2c.readfrom(0x38,6)     #read 6 bytes of measurement

    #convert temperature (seems just a little high)
    i = ((six[3] & 0x0f) << 16) | (six[4] << 8) | six[5]
    temperature_c = ((i / 1048576) * 200) - 50
    temperature_f = ((temperature_c * 9) / 5) + 32
    print("Temperature:",temperature_f,"F -",temperature_c,"C")

    #convert humidity
    i = ((six[1] << 16) | (six[2] << 8) | six[3]) >> 4
    humidity = (i / 1048576) * 100
    print("Humidity:",humidity,"%\n")

    pyb.delay(1000)

Next Post Previous Post