Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 21:25 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : AHT10 Humidity and Temperature Sensor

Author Message
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 04:57am 04 Oct 2023
Copy link to clipboard 
Print this post

The AHT10 Humidity and Temperature Sensor is marketed as a more accurate and cheaper substitute for the DHT sensors.
It uses I2C for communication so so does not require a firmware driver or CSub to use on MMBasic.

Here is some test code for the PicoMite.
It can be daisy-chained from the top of an RTC module, though the pads don't align so the leads must cross over.
'Read AHT10 using System I2C

Dim Integer  dat(6), addr, bytes5, n
Dim Float humid, temp
'Find address
I2C write &h38, 0, 1, 0  ' write zero to that adress
 If MM.I2C=0 Then             ' check for errors
   addr = &H38
  Else
   addr = &H39
 EndIf
Print ;" AHT10 address ";Hex$(addr,2);" Hex"
Print
Print "dat()"
Print "0    1  2  3  4  5      40 bit word     humid   temp     rel. humidity    temp"
Print

Do
  I2C write addr, 0, 3, &b10101100,&b00110011,0  'trigger measurement
  Pause 75                   'wait for data
  I2C read addr, 1, 6, dat() 'read 6 data bytes
  Print Hex$(dat(0),2);"   "; ' "State" byte = &H1C
  bytes5 = 0

  For n=1 To 5
   bytes5 = bytes5  + (dat(6-n)<<8*(n-1)) 'Assemble 40 bit word
   Print Hex$(dat(n),2);" "; 'print data bytes
  Next

 Print ,Hex$(bytes5,10),Hex$(bytes5>>20,5), Hex$(bytes5 And (&Hfffff),5),
'   humid = (bytes5>>20) / 2^20 * 100 'extract humidity
  humid = bytes5 / &H10000000000 * 100 'equivalent result to the line above
'   temp = (bytes5 And (2^20 - 1)) / (2^20) * 200 - 50 'extract temperature
  temp = (bytes5 And &Hfffff)/&H100000*200-50 'equivalent result to the line above
  temp = Int(temp*10)*0.1 'truncate temp
  Print  humid; "%", temp;" C"
  Pause 2000 ' read every 2S
Loop
End

And the output
Saved 1277 bytes
AHT10 address 38 Hex

dat()
0    1  2  3  4  5      40 bit word     humid   temp     rel. humidity    temp

1C   B6 C0 45 02 1F     B6C045021F      B6C04   5021F    71.38713007%    12.6 C
1C   B6 47 55 02 05     B647550205      B6475   50205    71.20259409%    12.5 C
1C   B6 4B 05 02 1A     B64B05021A      B64B0   5021A    71.20822077%    12.6 C
1C   B6 58 05 02 00     B658050200      B6580   50200    71.22805719%    12.5 C
1C   E6 BB E5 0B F9     E6BBE50BF9      E6BBE   50BF9    90.13045458%    13 C
1C   FF FF F5 25 C2     FFFFF525C2      FFFFF   525C2    99.99993531%    14.3 C
1C   FF FF F5 3E 12     FFFFF53E12      FFFFF   53E12    99.99993588%    15.5 C

The humidity readings differ significantly from a DHT11, breathing on it rapidly takes it to 99.9%.
Temp. response is faster too.
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 06:29am 04 Oct 2023
Copy link to clipboard 
Print this post

It will be interesting to see how well it recovers from prolonged high humidity.
I now have a couple on the slow boat from China.

Jim
VK7JH
MMedit   MMBasic Help
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 06:55am 04 Oct 2023
Copy link to clipboard 
Print this post

Have had success reviving a DHT11 in a jar with some silica gel (salvaged from many pill bottles) and sitting it on the wood heater for a few hours.
The AHT10 datasheet says 10 hours at 80 - 85 C.

Two addresses are available so 2 can use the same I2C. A solder bridge at the top selects the alternate - &H39.
Edited 2023-10-04 17:04 by phil99
 
Amnesie
Guru

Joined: 30/06/2020
Location: Germany
Posts: 396
Posted: 08:53am 04 Oct 2023
Copy link to clipboard 
Print this post

Thank you @ phil99 for another option of temp & humidity sensors!    Sounds promising!

Greetings
Daniel
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 11:03am 04 Oct 2023
Copy link to clipboard 
Print this post

Thanks for the post. If you know, what are the differences between the AHT10, AHT20, AHT21, and AHT30?
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 12:03pm 04 Oct 2023
Copy link to clipboard 
Print this post

I think the others have slightly higher accuracy than the AHT10, but haven't looked into the datasheets very closely.

To avoid a delay between initiating a read and outputting the data many sensors do it back to front. When you query them they output data from the previous read then get new data and save it for the next read.

The AHT10 has separate initiate and read commands so can work either way.

The Sub in this program avoids the 75mS pause by using the reverse method.
> LIST
'Read AHT10 using System I2C

Dim Integer  dat(6), addr = &H38, bytes5, n
Dim Float humid, temp

Print "rel. humidity    temp"
I2C write addr, 0, 3, 172, 51, 0  'trigger first measurement
Pause 75                   'wait for data

Sub AHT10
  I2C read addr, 1, 6, dat() 'read 6 data bytes
  bytes5 = 0

  For n=1 To 5
   bytes5 = bytes5  + (dat(6-n)<<8*(n-1)) 'Assemble 40 bit word
  Next

  humid = bytes5 / &H10000000000 * 100 'extract humidity
  temp = (bytes5 And &Hfffff) / &H100000 * 200 - 50 'extract temperature
  humid = CInt(humid)          'round humid
  temp = CInt(temp * 10) * 0.1 'round temp

  I2C write addr, 0, 3, 172, 51, 0  'trigger measurement for next read
End Sub

Do
  AHT10
  Print  humid;" %",, temp;" C"
  Pause 2000 ' read every 2S
Loop

End
>


Edit.
The AHT10 provides 20 bits each for humidity and temperature, which greatly exceeds the accuracy of it.
This streamlined version uses 8 bits for humidity and 12 bits for temperature.
'Read AHT10 using System I2C

Dim Integer  dat(4), addr = &H38
Dim Float humid, temp

Print "rel. humidity    temp"
I2C write addr, 0, 3, 172, 51, 0  'trigger first measurement
Pause 75                   'wait for data

Sub AHT10
  I2C read addr, 1, 5, dat() 'read first 5 data bytes - last one not needed
  humid = Cint(dat(1) * 0.390625)
  temp = Cint((((dat(3) And 15) << 8) + dat(4)) * 0.48828125 - 500) * 0.1

  I2C write addr, 0, 3, 172, 51, 0  'trigger measurement for next read
End Sub

Do
  AHT10
  Print  humid;" %",, temp;" C"
  Pause 2000 ' read every 2S
Loop

End

> RUN
rel. humidity    temp
63 %            14 C
63 %            14.1 C
63 %            14 C
63 %            14 C
>

Edit 2
Was curious to know what the first byte - the "State" byte (= &H1C) was for. The data sheet doesn't say so ran a loop continually reading the device and printing dat(0) and the timer.
Between initiating a read and getting new data dat(0) = &H9C then changes to &H1C. This takes about 42mS on my unit so the recommended pause of 75mS is longer than needed.
If you want the shortest possible read time keep reading until dat(0) changes then you have new data.


Sub AHT10
  I2C write AHaddr, 0, 3, 172, 51, 0  'trigger measurement for next read
  Timer =0:Do :I2C read AHaddr,1,5,dat():Loop Until dat(0)=28:Print Timer,,
  humid = Cint(dat(1) * 0.390625)
  temp = Cint((((dat(3) And 15) << 8) + dat(4)) * 0.48828125 - 500) * 0.1
   'read first 5 data bytes - last one not needed.
End Sub

Timer           rel. humidity    temp
41.548          71 %            13 C
41.655          71 %            13 C
41.694          71 %            13.1 C
41.682          76 %            13.1 C
41.724          77 %            13.1 C
41.562          77 %            13.1 C
41.679          77 %            13.1 C

Edited 2023-10-16 14:28 by phil99
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 01:04am 05 Dec 2023
Copy link to clipboard 
Print this post

Have been comparing the AHT10 with a DHT11 and a DS3231 RTC chip sensor for a few weeks.
To ensure they are all at the same temperature they are all sealed in a jar, close together.
According to the datasheets their temperature accuracies are:-
AHT10 +/- 0.3C
DHT11 +/- 2C
DS3231 +/- 3C

However in practice the DHT11 and DS3231 are usually within 0.3 C of the AHT10
If more people get similar results it may be worth asking Peter if he can add tenths of degrees to the DEVICE HUMID command output.
The data sheet Peter was using indicated only whole degrees were available and with +/- 2C accuracy that seemed reasonable.
A different translation of the datasheet showed tenths were there but for the above reason there seemed no reason to change it at the time.

The hack below uses the DHT12 command and an extra pin to get the tenths from byte 4 of the output.
' AHT10 DHT11 DS3231 and VL53LOX .bas
' PicoMite V5.08.08b2
'Read AHT10, DS3231, and VL53LOX using System I2C and DHT11 using GPO and GP1

'=======================================================================
'AHT10

Dim Float humid, temp
Const AHaddr = &H38 ' AHT10 I2C address
I2C write AHaddr, 0, 3, 172, 51, 0  'trigger first AHT10 measurement
Pause 45               'wait for data

Sub AHT10
  I2C write AHaddr, 0, 3, 172, 51, 0  'trigger measurement for next read
  Pause 45 'wait for data
  I2C read AHaddr,1,5,dat() 'read first 5 data bytes - last one not needed.
  humid = Cint(dat(1) * 0.390625)
  temp = Cint((((dat(3) And 15) << 8) + dat(4)) * 0.48828125 - 500) / 10
End Sub

'=======================================================================
' DHT11

SetPin gp1, dout 'provides extended "wake" pulse for DHT11
Pin(GP1) = 1     'via diode to GP0 (anode to GP0, cathode to GP1)
Dim n%, a(9) : a(9) = 255  ' = "array not full" flag, BASE 0
Dim Float T, H

Sub DHT11  'get DHT11 reading with tenths of degrees
  Pulse GP1, 25  'provide an extended low "wake" pulse for DHT11
  Pause 23.9     'overlap the 1mS DHT22 "wake" pulse
  Device humid GP0, T, H  'start DHT22 command
  If T <> 1000 Then ' 1000 = checksum error or no response
    H = H*10 >> 8   ' convert data format from DHT22 to DHT11
    T = (T*10 >> 8) + (T*10 And 255) / 10 ' and add tenths of degrees
'    byte 1 = humidity, 2 = not used, 3 = temp., 4 = tenths of degrees, 5 = checksum
    a(n%) = T      ' load array to get 10 sample mean
    Inc n% : n% = n% Mod 10 ' set array index, after 9 jump back to 0
    If a(9) < 100 Then T = Int(Math(sum a())) / 10 'if array is full get mean to 1 DP
  EndIf
  Pin(GP1) = 1 ' ensure correct start for next run (optional)
End Sub

'=======================================================================
' DS3231 RTC temp

Function RTCtemp() As FLOAT
' DS3231 RTC temp. registers get updated at 64 S intervals.
 Local D%, Q%
 RTC GetReg 17, D% ' Degrees are in register 17
 RTC GetReg 18, Q% 'and 0.25C steps in register 18, using top 2 bits.
 RTCtemp = D% + Q% / 256
End Function

'=======================================================================
'VL53LOX laser distance

Dim Integer  dat(4), dst(1), dist
Const VLaddr=&H29 'VL53LOX I2C address
I2C write VLaddr,0,2,0,1          'trigger first VL53LOX measurement
Pause 75

Sub VL53LOX
  I2C write VLaddr,1,1,30
  I2C read VLaddr,0,2,dst()
  dist = (dst(0)<<8) + dst(1) - 20

  I2C write VLaddr,0,2,0,1  'trigger measurement for next read
End Sub

'=======================================================================
'Main Loop
Print "AHT10 humid      AHT10 temp      RTC temp        DHT11 temp      DHT11 humid "'    distance"

Do
  AHT10
  Pause 500
  DHT11
  Pause 500
'   VL53LOX
  Print  humid;" %",, temp;" C",, RTCtemp()" C   ", T" C    ", H" %"',, dist" mm"
  Pause 1000 ' read every 2S
Loop

Sub mm.prompt
 Print Cwd$"> ";
End Sub

End

The output to console. For the DHT11 a rolling mean starts after 10 samples to reduce jitter.
Saved 3005 bytes
A:/> RUN
AHT10 humid      AHT10 temp      RTC temp        DHT11 temp      DHT11 humid
39 %            19.9 C          19.5 C          20.3 C          48 %
39 %            19.9 C          19.5 C          19.8 C          42 %
39 %            19.9 C          19.5 C          19.7 C          48 %
39 %            19.9 C          19.5 C          19.9 C          48 %
39 %            19.9 C          19.5 C          20 C            48 %
39 %            19.9 C          19.5 C          19.7 C          48 %
39 %            19.9 C          19.5 C          20.1 C          48 %
39 %            19.9 C          19.5 C          19.9 C          48 %
39 %            19.9 C          19.5 C          20 C            48 %
39 %            19.9 C          19.5 C          19.9 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
39 %            19.9 C          19.5 C          19.8 C          48 %
A:/>

Edit
Ignore the VL53LOX bit, it was already attached to the RTC so its code was left in.

Edit 2
The discrepancy between the AHT10 and DHT11 humidity readings has gradually decreased in the time they have been sealed in the jar. I put a small silica gel capsule in with them so I could compare them at lower humidity.
The difference was 18% when they went in a few weeks ago and this afternoon is down to 6%.
.
Edited 2023-12-05 16:55 by phil99
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4234
Posted: 07:49am 05 Dec 2023
Copy link to clipboard 
Print this post

  TassyJim said  It will be interesting to see how well it recovers from prolonged high humidity.
I now have a couple on the slow boat from China.

Jim


For continued high humidity use the "wet sock" method on a DS18B20 sensor.
That works quite good.

Volhout
PicomiteVGA PETSCII ROBOTS
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 11:37pm 05 Jan 2024
Copy link to clipboard 
Print this post

  Quote  It will be interesting to see how well it recovers from prolonged high humidity.

There are a few new accurate, affordable T & H modules now available. One that got my attention is the TI HDC1080 I2C.
  Quote  8.3.3 Heater
The heater is an integrated resistive element that can be used to test the sensor or to drive condensation off the sensor.
The heater can be activated using HEAT, bit 13 in the Configuration Register. The heater helps in reducing the accumulated offset after long exposure at high humidity conditions.
Once enabled the heater is turned on only in the measurement mode. To accelerate the temperature increase it is suggested to increase the measurement data rate.

At this stage I don't have a use for another so won't get one.

After lengthy comparison with the AHT10 it seems my effort to revive the DHT11 have only been partially successful, the humidity is off by 10% to 20%, varying with temperature.
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 4234
Posted: 05:34pm 07 Jan 2024
Copy link to clipboard 
Print this post

I have used the TI HDC2080, and it is quite accurate, but also not handling high humidity long time very well.
Hence the wet sock.
You can “calibrate” the 2080

Volhout
PicomiteVGA PETSCII ROBOTS
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024