News:

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

Main Menu

AHT10 Temperature and Humidity Sensor Proton Code

Started by dnaci, Feb 03, 2024, 03:53 PM

Previous topic - Next topic

dnaci

Aht20 code prepared by RGV250 is taken as an example. I made some necessary address changes for Aht10. It shows the values on a 4*20 LCD screen. Thanks @RGV250

Device = 16F877A
    Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_OFF, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF
    Declare Xtal = 20
    Declare All_Digital TRUE
    TRISA=0
    TRISB=0
    TRISC=0
    TRISD=0
    TRISE=0
    PORTA=0
    PORTB=0
    PORTC=0
    PORTD=0
    PORTE=0
    Declare LCD_Lines 4
    Declare LCD_Interface 4
    Declare LCD_DTPin PORTB.0
    Declare LCD_RSPin PORTB.5
    Declare LCD_ENPin PORTB.6
    'I2C port declarations
    Symbol SCL = PORTC.6                    'Clock pin (Yellow)
    Symbol SDA = PORTC.7                    'Data pin  (Blue)

    Symbol AHT_ADDR_READ = 0x71             'AHT default address & read
    Symbol AHT_ADDR_WRITE = 0x70            'AHT default address & write
    Symbol AHT_CMD_INIT = 0xE1              'Trigger reading command
    Symbol AHT_CMD_TRIGGER = 0xAC           'Trigger reading command
    Symbol AHT_CMD_SOFT_RESET = 0xBA        'Trigger soft reset

    Dim Temperature_Temp As Dword
    Dim Temperature_Temp_B0 As Temperature_Temp.Byte0 'Alias unsigned Part1 to the low byte
    Dim Temperature_Temp_B1 As Temperature_Temp.Byte1 'Alias unsigned Part2 to the 2nd byte
    Dim Temperature_Temp_B2 As Temperature_Temp.Byte2 'Alias unsigned Part3 to the 3rd byte
    Dim Temperature_Temp_B3 As Temperature_Temp.Byte3 'Alias unsigned Part3 to the high (4th) byte
    Dim Temperature As Float

    Dim Humidity_Temp As Dword
    Dim Humidity_Temp_B0 As Humidity_Temp.Byte0       'Alias unsigned Part1 to the low byte
    Dim Humidity_Temp_B1 As Humidity_Temp.Byte1       'Alias unsigned Part2 to the 2nd byte
    Dim Humidity_Temp_B2 As Humidity_Temp.Byte2       'Alias unsigned Part3 to the 3rd byte
    Dim Humidity_Temp_B3 As Humidity_Temp.Byte3       'Alias unsigned Part3 to the high (4th) byte
    Dim Humidity As Float

    'Dim AHT_Status As Byte                           'Same as Data_0
    Dim AHT_Data_0  As Byte                           '0 - Status
    Dim AHT_Data_1  As Byte                           '1 - Humidity
    Dim AHT_Data_2  As Byte                           '2 - Humidity
    Dim AHT_Data_3  As Byte                           '3 - Humidity / Temperature
    Dim AHT_Data_4  As Byte                           '4 - Temperature
    Dim AHT_Data_5  As Byte                           '5 - Temperature
    Dim AHT_Data_6  As Byte                           '6 - CRC (Not used)
    Dim AHT_CALL As Byte 'Counter so AHT10 is not called every cycle

    DelayMS 40 'AHT only requires 20ms but dont read for 40ms.

    'Main Program Starts Here
    Do
        If AHT_CALL >= 2 Then  'Only call the AHT10 sensor every 2 seconds
          GoSub AHT10 'Call the sensor read subroutine
          Print At 1,1," AHT10 SENSOR DEMO "
          Print At 2,1,"   PROTON BASIC    "
          Print At 3,1,"HUM:", Dec2 Humidity
          Print At 4,1,"TEMP:", Dec2 Temperature
          DelayMS 500
        EndIf

      DelayMS 1000 'Create a delay between reads, also show a time status on the display.
      Inc AHT_CALL
    Loop

    AHT10:
    'DelayMS 10
    'I2COut SDA, SCL, AHT_ADDR_WRITE, [AHT_CMD_SOFT_RESET,0x70,0xBA] 'AHT RESET
    DelayMS 10
    I2COut SDA, SCL, AHT_ADDR_WRITE, [AHT_CMD_TRIGGER,0x39,0x00] '39 I2C pin pullup, 38 pulldown
    DelayMS 10
    I2COut SDA, SCL, AHT_ADDR_WRITE, [AHT_CMD_INIT,0x08,0x00] 'AHT INIT
    DelayMS 10
    I2COut SDA, SCL, AHT_ADDR_WRITE, [AHT_CMD_TRIGGER,0x33,0x00]'Trigger the measurement
    DelayMS 80 'Wait for the measurement to complete
    'Read the raw data
    I2CIn SDA, SCL, AHT_ADDR_READ, [AHT_Data_0, AHT_Data_1, AHT_Data_2,AHT_Data_3, AHT_Data_4, AHT_Data_5, AHT_Data_6]

    'Do the humidity calculation
    Clear Humidity_Temp
    Humidity_Temp_B2 = AHT_Data_1
    Humidity_Temp_B1 = AHT_Data_2
    Humidity_Temp_B0 = AHT_Data_3
    Humidity_Temp = Humidity_Temp >> 4

    Humidity = Humidity_Temp / 0x100000
    Humidity = Humidity * 100

    'Do the temperature calculation
    Clear Temperature_Temp
    Temperature_Temp_B2 = AHT_Data_3 & 0x0F
    Temperature_Temp_B1 = AHT_Data_4
    Temperature_Temp_B0 = AHT_Data_5

    Temperature = Temperature_Temp / 0x100000
    Temperature = Temperature * 200
    Temperature = Temperature - 50
    Return