© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
DevHeads IoT Integration ServerDIIS
DevHeads IoT Integration Server•2y ago•
10 replies
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
prj.conf
, and checking the device tree (
dts
dts
file) for accurate sensor definitions, I still receive the error
Failed to fetch data.
Failed to fetch data.
How can I successfully fetch and display temperature and humidity data from the DHT22 sensor on the AVR32UC microcontroller?
Solution
finally gushh😂 , I choked it, I enabled detailed logging, which revealed an I/O error. I then double-checked the GPIO configuration, verified d pin setup, and added a pull-up resistor. After increasing delays and adding debug prints in d driver code, I finally moved the sensor to a different GPIO pin. I think I'm making progress in this, the sensor is now successfully fetching and displaying data. see this
[00:00:00.000,000] <inf> dht: DHT driver initialized
[00:00:00.000,001] <dbg> dht: Starting data fetch
[00:00:00.000,002] <dbg> dht: Starting measurement
[00:00:00.000,003] <dbg> dht: Reading data
Temperature: 25.4 C
Humidity: 60.7 %
[00:00:00.000,000] <inf> dht: DHT driver initialized
[00:00:00.000,001] <dbg> dht: Starting data fetch
[00:00:00.000,002] <dbg> dht: Starting measurement
[00:00:00.000,003] <dbg> dht: Reading data
Temperature: 25.4 C
Humidity: 60.7 %
Jump to solution
DevHeads IoT Integration Server banner
DevHeads IoT Integration ServerJoin
The DevHeads IoT Integration Server accelerates technology engineering by helping pro devs learn, share and collaborate.
2,984Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How to Resolve DHT22 Sensor Driver Binding Issue with Zephyr OS on AVR32UC?
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🪲-firmware-and-baremetal
2y ago
How to Resolve GPIO Pin Configuration Failures for DHT22 Sensor on AVR32UC with Zephyr OS?
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🪲-firmware-and-baremetal
2y ago
GPIO Pin Configuration Error for DHT22 on AVR32UC with Zephyr OS
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🪲-firmware-and-baremetal
2y ago
How Can I Consistently Handle Sensor Data Read Failures on AVR32UC with Zephyr OS?
DevHeads IoT Integration ServerDIISDevHeads IoT Integration Server / 🪲-firmware-and-baremetal
2y ago