#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define DHT_PIN 26
#define LED_PIN 14
#define MAX_TIMINGS 85
int main() {
stdio_init_all();
gpio_init(DHT_PIN);
gpio_set_dir(DHT_PIN, GPIO_OUT);
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while (true) {
int data[5] = {0, 0, 0, 0, 0};
gpio_put(DHT_PIN, 0);
sleep_ms(20);
gpio_put(DHT_PIN, 1);
sleep_us(40);
gpio_set_dir(DHT_PIN, GPIO_IN);
for (int i = 0; i < MAX_TIMINGS; i++) {
int count = 0;
while (gpio_get(DHT_PIN) == 0) {
count++;
sleep_us(1);
if (count > 255) break;
}
count = 0;
while (gpio_get(DHT_PIN) == 1) {
count++;
sleep_us(1);
if (count > 255) break;
}
if (i >= 4 && i % 2 == 0) {
data[i / 16] <<= 1;
if (count > 16) data[i / 16] |= 1;
}
}
printf("Humidity: %d.%d%%\n", data[0], data[1]);
printf("Temperature: %d.%d°C\n", data[2], data[3]);
//BLINK LED
gpio_put(LED_PIN, 1);
sleep_ms(500);
gpio_put(LED_PIN, 0);
sleep_ms(1000);
}
return 0;
}