News:

Let's find out together what makes a PIC Tick!

Main Menu

Procedure for DHT11/22

Started by keytapper, Apr 14, 2025, 09:05 PM

Previous topic - Next topic

keytapper

Hello folks,
I'm trying to familiarize with a humidity sensor, but I'd like to see on the simulation how it works.
I saw an example but it's not as flexible as a procedure.

Obviously I'm geared up for the midrange MCU family.

Ignorance comes with a cost

RGV250

Hi,
Got code for AHT20 and SHT31 temp/humidity sensors. I did look at the DHT11/22 but quite old so figured the newer ones should be more accurate. Not sure that is the case.

Bob

flosigud

#2
DHT11/22 are extremely slow. One measurement takes about a minute. I have code somewhere that reads in interrrupt, but it doesn't compile as I have lost include files for serial and osc. I also have code for the much nicer AHT20.

'    -----------------------------------------------------------------------------
'    Name    : AHT20_Humidity.bas
'    Author    : Flosi Guðmundsson
'    Date    : 31.8.2024
'    Version    : 1.0
'    Notes    :
'    -----------------------------------------------------------------------------
'
'    Include "..\33fj128GP802.inc"
'    Include "..\..\33FJ12MC202.inc"
    Include "..\..\33FJ32MC302b.inc"

'    $define HardwareIIC

    Include "AHT20_Humidity.inc"
 
    Dim fTemperature As Float = 0
    Dim fHumididy As Float = 0

'    DelayMS 400

'    -----------------------------------------------------------------------------
'    If AHT20_Connected() = FALSE Then
'        HRSOut "Not AHT20"
'        Stop
'    EndIf

'    -----------------------------------------------------------------------------
    If AHT20_Begin() = FALSE Then            ' This is according to the datasheet
        HRSOut "Not Calibarated",13
        Stop
    EndIf

'    -----------------------------------------------------------------------------
    Do
        AHT20_ReadData()   
        fTemperature = AHT20_fTemperature()
        fHumididy = AHT20_fHumidity()
        HRSOut "Temperature: ", Dec fTemperature, "C°", 10,13
        HRSOut "Humitidy:      ", Dec fHumididy, "%RH", 10,13
        HRSOut "Calibrated:      ", Bin bState & 4 >> 2, " ", 10,13    ' Calibrated flag
        HRSOut "Status reg    ", Bin8 bState, " ", 10,13,10,13        ' Status register
        DelayMS 2000
    Loop

Include
'    ---------------------------------------------------------
'    Name    : AHT20_Humidity.Inc
'    Author    : Flosi Guðmundsson
'    Date    : 31.8.2024
'    Version    : 1.0
'    Notes    :
'    ---------------------------------------------------------
'
'    AHT-20 Constants
'    ---------------------------------------------------------
'
    Symbol AHT20_ADDRESS $70
    Symbol AHT20_STATUS $71
    Symbol AHT20_MEASURE $AC
    Symbol AHT20_RESET $BA
    Symbol AHT20_SOFT_RESET $BA
    Symbol AHT20_INITIALIZE $BE

    Symbol BUSY_MASK $80
    Symbol CALIBRATED_MASK 8

    Symbol TRUE 1
    Symbol FALSE 0
'
'    AHT-20 IIC macros
'    -----------------------------------------------------------------------------
'    $define HardwareIIC
   
    $ifdef HardwareIIC
        Declare HSCL1_Pin = PORTB.8                ' SCL connected to this pin
        Declare HSDA1_Pin = PORTB.9                ' SDA connected to this pin
        Declare Hbus1_Bitrate = 400                ' I2C bus bitrate (in KHz)
        Include "Hbus.inc"                        ' Hbusin, Hbusout replacement routines
       
        $define AHT20_GetData HBusIn AHT20_ADDRESS
        $define AHT20_GetStatus() HBusIn AHT20_ADDRESS
         $define AHT20_Calibrated() HBusIn AHT20_ADDRESS & CALIBRATED_MASK
        $define AHT20_Busy() HBusIn AHT20_ADDRESS & BUSY_MASK
        $define AHT20_WaitWhileBusy() While AHT20_Busy() = TRUE : Wend
       
        $define AHT20_SoftRESET() HBusOut AHT20_ADDRESS,[AHT20_SOFT_RESET,0]
        $define AHT20_Init() HBusOut AHT20_ADDRESS,[AHT20_INITIALIZE, $08, $00]
        $define AHT20_InitMeasure() HBusOut AHT20_ADDRESS,[AHT20_MEASURE,$33, $00]
    $else
        Declare SCL_Pin = PORTB.8                ' SCL connected to this pin
        Declare SDA_Pin = PORTB.9                ' SDA connected to this pin
        Include "Bus.inc"
       
        $define AHT20_GetData BusIn AHT20_ADDRESS
        $define AHT20_GetStatus() BusIn AHT20_ADDRESS
        $define AHT20_Calibrated() BusIn AHT20_ADDRESS & CALIBRATED_MASK
        $define AHT20_Busy() BusIn AHT20_ADDRESS & BUSY_MASK
       
        $define AHT20_SoftRESET() BusOut AHT20_ADDRESS,[AHT20_SOFT_RESET,0]
        $define AHT20_Init() BusOut AHT20_ADDRESS,[AHT20_INITIALIZE, $08, $00]
        $define AHT20_InitMeasure() BusOut AHT20_ADDRESS,[AHT20_MEASURE,$33, $00]
    $endif

'    AHT-20 procedure and functions
'    -----------------------------------------------------------------------------
'
'    AHT20_Begin()                Initializes AHT 20 and returns
'                                TRUE If Calbrated Bit in STATUS register is Set
'
'    AHT20_ReadData()            Reads raw data and returns in dRawHumidity And dRawTemp
'
'    AHT20_fHumidity()            Returns Humitidy %RH   
'
'    AHT20_fTemperature()        Returns Temperature in C°
'
'    AHT20_Connected()
'    -----------------------------------------------------------------------------

     Proc AHT20_Connected(), Bit
        Result = FALSE
        If AHT20_GetData < 255    Then
            Result = TRUE
        EndIf
    EndProc

'    AHT20_Begin()
'    -----------------------------------------------------------------------------
'    Initializes AHT 20 and returns TRUE if Calbrated bit in status register is set

    Proc AHT20_Begin(), Bit
        Result = FALSE                            ' Let's be pessimistic
        DelayMS 40                                ' Wait 40 ms
        AHT20_SoftRESET()                        ' Reset
        AHT20_Init()                            ' Initialize
        AHT20_ReadData()
        If bState.3 = TRUE Then                    ' If Calibarated return TRUE
            Result = TRUE
        EndIf
    EndProc

'    AHT20_ReadData()
'    -----------------------------------------------------------------------------   
'    Reads data into bState, dRawHumidity and dRawTemp

    Proc AHT20_ReadData()
        Global Dim dRawHumidity As Dword = 0
        Global Dim dRawTemp As Dword = 0
        Global Dim bState As Byte = 0            ' Status register
        AHT20_InitMeasure()                        ' Trigger a measurement.
        DelayMS 80                                ' Wait for measurement to complete
        AHT20_GetData,[bState ,dRawHumidity.Byte2,dRawHumidity.Byte1,dRawHumidity.Byte0,dRawTemp.Byte1,dRawTemp.Byte0]
        dRawTemp.Byte2 = dRawHumidity.Byte0 & $0F
        dRawHumidity = dRawHumidity >> 4
    EndProc

'    AHT20_ReturnHumidity()
'    -----------------------------------------------------------------------------   
'    Returns floating point percent Relative Humitidy

    Proc AHT20_fHumidity(),Float
        Result = 0
        Result = dRawHumidity / 1048576.0
        Result = Result * 100.0
    EndProc

'    AHT20_ReturnTemperature()
'    -----------------------------------------------------------------------------   
'    Returns floating point temperature in C°

    Proc AHT20_fTemperature(),Float
        Result = 0
        Result = dRawTemp / 1048576.0
        Result = Result * 200.0
        Result = Result -50.0
    EndProc

This should work on midrange too.

Pepe

demo proteus sht21

keytapper

Thank everyone, I appreciate it.
I'm on the move, so I should spend some days before to get settled.
Ignorance comes with a cost