0

I'm trying to implement a program from scratch to read from a DHT22 sensor connected to an ESP-01 as a learning exercise. I am basing my implementation on the from adafruit DHT library. Following is my code:

#include <stdio.h>
#include <xtensa/xtruntime.h>

#include "freertos/FreeRTOS.h"

#include "driver/gpio.h"
#include "esp8266/gpio_struct.h"

#define DHT_PIN GPIO_NUM_2
#define DHT_TTL_MAX 1000

// reads gpio level in ~175ns
static inline int dht_get_level()
{
    return (GPIO.in >> DHT_PIN) & 0x1;
}

static inline int dht_sig_ttl(int sig)
{
    int t, tmax;

    tmax = DHT_TTL_MAX * g_esp_ticks_per_us;
    for (t = 0; dht_get_level() == sig && t < tmax; t++)
        ;

    return t / g_esp_ticks_per_us;
}

void app_main()
{
    int ttl;
    gpio_config_t conf;

    printf("us: %d\n", g_esp_ticks_per_us);

    conf.pin_bit_mask = 1UL << DHT_PIN;
    conf.mode = GPIO_MODE_INPUT;
    conf.pull_up_en = GPIO_PULLUP_ENABLE;
    gpio_config(&conf);

    ets_delay_us(1000);

    gpio_set_direction(DHT_PIN, GPIO_MODE_OUTPUT);
    gpio_set_level(DHT_PIN, 0);
    ets_delay_us(1100);
    gpio_set_direction(DHT_PIN, GPIO_MODE_INPUT);

    // wait for the sensor to wake up
    vPortETSIntrLock();
    ttl = dht_sig_ttl(1);
    vPortETSIntrUnlock();
    printf("TTL: %dus (expected: 20-40us)\n", ttl);

    // wait for the first 80us low pulse from the sensor.
    vPortETSIntrLock();
    ttl = dht_sig_ttl(0);
    vPortETSIntrUnlock();
    printf("TTL: %dus (expected: 80us)\n", ttl);
    fflush(stdout);
}

Below is what I see in the logs:

I (46) boot: ESP-IDF  2nd stage bootloader
I (46) boot: compile time 14:47:28
I (46) qio_mode: Enabling default flash chip QIO
I (52) boot: SPI Speed      : 40MHz
I (58) boot: SPI Mode       : QIO
I (64) boot: SPI Flash Size : 2MB
I (70) boot: Partition Table:
I (76) boot: ## Label            Usage          Type ST Offset   Length
I (87) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (99) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (110) boot:  2 factory          factory app      00 00 00010000 000f0000
I (122) boot: End of partition table
I (128) esp_image: segment 0: paddr=0x00010010 vaddr=0x40210010 size=0x14574 ( 83316) map
0x40210010: _stext at ??:?

I (169) esp_image: segment 1: paddr=0x0002458c vaddr=0x40224584 size=0x05680 ( 22144) map
I (178) esp_image: segment 2: paddr=0x00029c14 vaddr=0x3ffe8000 size=0x003dc (   988) load
I (181) esp_image: segment 3: paddr=0x00029ff8 vaddr=0x40100000 size=0x00080 (   128) load
I (195) esp_image: segment 4: paddr=0x0002a080 vaddr=0x40100080 size=0x04548 ( 17736) load
I (213) boot: Loaded app from partition at offset 0x10000
us: 160
I (233) gpio: GPIO[2]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 
TTL: 1000us (expected: 20-40us)
TTL: 0us (expected: 80us)

Looks like the sensor is not pulling down at all. However, when I try the DHT test program in Arduino IDE that uses the Adafruit library, I get the sensor reading, which means that the sensor is not broken. What am I doing wrong?

kovac
  • 343
  • 1
  • 8

0 Answers0