Dtynin
Dtynin
How to Fetch and Display Data from DHT22 Sensor on AVR32UC with Zephyr OS?
hey guys, still trying to advance on my work, I've been working on fetching and displaying data from the DHT22 sensor using the AVR32UC microcontroller, but I'm encountering a persistent data fetch failure. Here’s the code I’ve implemented:
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <sys/printk.h>

const struct device *sensor_dev;

void main(void)
{
struct sensor_value temp, humidity;

sensor_dev = device_get_binding(DT_LABEL(DT_INST(0, ams_dht22)));
if (!sensor_dev) {
printk("Failed to bind DHT22 sensor\n");
return;
}

if (sensor_sample_fetch(sensor_dev) < 0) {
printk("Failed to fetch data\n");
return;
}

sensor_channel_get(sensor_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(sensor_dev, SENSOR_CHAN_HUMIDITY, &humidity);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
printk("Humidity: %d.%d %%\n", humidity.val1, humidity.val2);
}
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#include <sys/printk.h>

const struct device *sensor_dev;

void main(void)
{
struct sensor_value temp, humidity;

sensor_dev = device_get_binding(DT_LABEL(DT_INST(0, ams_dht22)));
if (!sensor_dev) {
printk("Failed to bind DHT22 sensor\n");
return;
}

if (sensor_sample_fetch(sensor_dev) < 0) {
printk("Failed to fetch data\n");
return;
}

sensor_channel_get(sensor_dev, SENSOR_CHAN_AMBIENT_TEMP, &temp);
sensor_channel_get(sensor_dev, SENSOR_CHAN_HUMIDITY, &humidity);

printk("Temperature: %d.%d C\n", temp.val1, temp.val2);
printk("Humidity: %d.%d %%\n", humidity.val1, humidity.val2);
}
regardless of confirming the sensor's physical connections, verifying its initialization, ensuring the DHT22 sensor driver is correctly configured in prj.conf, and checking the device tree (dts file) for accurate sensor definitions, I still receive the error Failed to fetch data. How can I successfully fetch and display temperature and humidity data from the DHT22 sensor on the AVR32UC microcontroller?
11 replies