Operation of DHT11 type sensor with nrf5340DK board

Hello,

I send you a message because I try to do work a sensor of type DHT11.

I am a beginner with Nordic SDK, I use VS Code to develop and NRF5340DK board. 

I connect the sensor to a gpio pin that I write in overlay file file.

The card "see" the sensor but I din't fetch samples, I don't know why.

Here is my prjconf file : 

CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_GPIO=y
CONFIG_ADC=y
CONFIG_SENSOR=y
CONFIG_NTC_THERMISTOR=y
CONFIG_LOG=y

And my main.c file :

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/sensor.h>

#define SLEEP_TIME_MS 100

//if(!device_is_ready(button.port)){
//  return -1;
//}
/* #define OW_NODE DT_NODELABEL(ow_button)

static const struct gpio_dt_spec ow_gpio = GPIO_DT_SPEC_GET(OW_NODE, gpios); */

int main(void)
{
    //int ret = gpio_pin_configure_dt(&button, GPIO_INPUT);
//if (ret < 0) {
 //return -1;
//}
//bool val = gpio_pin_get_dt(&button);
//printk("%B\n", val);

    //nrf_gpio_cfg_input(GPIO_PIN, NRF_GPIO_PIN_PULLUP);
    /* const struct device *ow_bus = device_get_binding("OW_GPIO");
    if (!ow_bus) {
   printk("GPIO device bus not found!\n");
    //return 1;
}
    gpio_dev = device_get_binding(ONE_WIRE_PORT);
    if (!gpio_dev) {
        printk("GPIO device not found!\n");
        return 1;
    }

    int ret = gpio_pin_configure(gpio_dev, 0, GPIO_OUTPUT | GPIO_OPEN_DRAIN | GPIO_PULL_UP);
if (ret < 0) {
    printk("Failed to configure pin\n");
} */

/* if (!device_is_ready(ow_gpio.port)) {
        printk("GPIO device not ready!\n");
    }

    int ret = gpio_pin_configure_dt(&ow_gpio, GPIO_OUTPUT | GPIO_OPEN_DRAIN | GPIO_PULL_UP);
    if (ret < 0) {
        printk("Failed to configure pin\n");
    }

    printk("GPIO configured successfully.\n");

    int val = gpio_pin_get(ow_gpio.port, ow_gpio.pin);
if (val < 0) {
    printk("Erreur lecture GPIO\n");
} else {
    printk("Valeur GPIO : %d\n", val);
} */
k_msleep(SLEEP_TIME_MS);
const struct device *dht = device_get_binding("DHT_SENSOR");
k_msleep(SLEEP_TIME_MS);
if (!dht) {
    printk("DHT not found\n");
}k_msleep(SLEEP_TIME_MS);
if (!device_is_ready(dht)) {
    printk("DHT device not ready\n");
}

struct sensor_value temphumidity;
k_msleep(SLEEP_TIME_MS);
int ret = sensor_sample_fetch_chan(dhtSENSOR_CHAN_ALL);
if (ret < 0) {
    printk("Failed to fetch sample: %d\n"ret);
}
k_msleep(SLEEP_TIME_MS);
ret = sensor_channel_get(dhtSENSOR_CHAN_AMBIENT_TEMP , &temp);
if (ret < 0) {
    printk("Failed to get temperature: %d\n"ret);
}

ret = sensor_channel_get(dhtSENSOR_CHAN_AMBIENT_TEMP , &humidity);
if (ret < 0) {
    printk("Failed to get humidity: %d\n"ret);
}

printk("Temp: %d.%06d °C\n"temp.val1temp.val2);
printk("Humidity: %d.%06d %%\n"humidity.val1humidity.val2);

    //gpio_pin_configure(gpio_dev, ONE_WIRE_PIN, GPIO_INPUT | GPIO_PULL_UP);
    while (1) {
        printk("Hello World!\n");
        k_msleep(SLEEP_TIME_MS);
    }
}

And my overlay file : 

/*
 * Device Tree overlay for DHT sensor sample on Arduino Giga R1 WiFi (M7 Core)
 * Connect the DHT sensor's data pin to Arduino pin D7 (PB4).GPIO_ACTIVE_LOW | GPIO_PULL_UP
 * For DHT11 Sensor.
 */

/ {
    aliases {
        /* Assign dht0 alias to our defined DHT sensor node */
        dht0 = &dht_sensor;
    };

    /* Define the DHT sensor node */
    dht_sensor: dht_sensor {
        compatible = "aosong,dht";
        label = "DHT_SENSOR";

        /* Specify the GPIO pin connection and enable the pull-up */
        dio-gpios = <&gpio0 0 (GPIO_ACTIVE_LOW)>;

        /* Enable the device */
        status = "okay";
    };
};
&arduino_adc {
    io-channel-map = <0 &adc 0>, <1 &adc 1>, <2 &adc 2>, <3 &adc 3>, <4 &adc 4>, <5 &adc 5>;
    compatible = "arduino,uno-adc";
};
With this code, the msg Failed to fetch sample : -5 display on the console.

Thank you for your help !

Best regards, 

Jade Mouillot

Related