Initial Release
This commit is contained in:
11
gpio/dht_sensor/CMakeLists.txt
Normal file
11
gpio/dht_sensor/CMakeLists.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
add_executable(dht
|
||||
dht.c
|
||||
)
|
||||
|
||||
target_link_libraries(dht pico_stdlib)
|
||||
|
||||
pico_add_extra_outputs(dht)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(dht)
|
||||
|
||||
58
gpio/dht_sensor/README.adoc
Normal file
58
gpio/dht_sensor/README.adoc
Normal file
@@ -0,0 +1,58 @@
|
||||
= DHT-11, DHT-22, and AM2302 Sensors
|
||||
:xrefstyle: short
|
||||
|
||||
The DHT sensors are fairly well known hobbyist sensors for measuring relative humidity and temperature using a capacitive humidity sensor, and a thermistor. While they are slow, one reading every ~2 seconds, they are reliable and good for basic data logging. Communication is based on a custom protocol which uses a single wire for data.
|
||||
|
||||
[NOTE]
|
||||
======
|
||||
The DHT-11 and DHT-22 sensors are the most common. They use the same protocol but have different characteristics, the DHT-22 has better accuracy, and has a larger sensor range than the DHT-11. The sensor is available from a number of retailers.
|
||||
======
|
||||
|
||||
== Wiring information
|
||||
|
||||
See <<dht-wiring-diagram>> for wiring instructions.
|
||||
|
||||
[[dht-wiring-diagram]]
|
||||
[pdfwidth=75%]
|
||||
.Wiring the DHT-22 temperature sensor to Raspberry Pi Pico, and connecting Pico's UART0 to the Raspberry Pi 4.
|
||||
image::pi-and-pico-uart-and-dht-sensor.png[]
|
||||
|
||||
NOTE: One of the pins (pin 3) on the DHT sensor will not be connected, it is not used.
|
||||
|
||||
You will want to place a 10 kΩ resistor between VCC and the data pin, to act as a medium-strength pull up on the data line.
|
||||
|
||||
Connecting UART0 of Pico to Raspberry Pi as in <<dht-wiring-diagram>> and you should see something similar to <<dht-serial-output-diagram>> in `minicom` when connected to `/dev/serial0` on the Raspberry Pi.
|
||||
|
||||
[[dht-serial-output-diagram]]
|
||||
[pdfwidth=75%]
|
||||
.Serial output over Pico's UART0 in a terminal window.
|
||||
image::serial-output.png[]
|
||||
|
||||
Connect to `/dev/serial0` by typing,
|
||||
|
||||
----
|
||||
$ minicom -b 115200 -o -D /dev/serial0
|
||||
----
|
||||
|
||||
at the command line.
|
||||
|
||||
== List of Files
|
||||
|
||||
A list of files with descriptions of their function;
|
||||
|
||||
CMakeLists.txt:: Make file to incorporate the example in to the examples build tree.
|
||||
dht.c:: The example code.
|
||||
|
||||
== Bill of Materials
|
||||
|
||||
.A list of materials required for the example
|
||||
[[dht-22-bom-table]]
|
||||
[cols=3]
|
||||
|===
|
||||
| *Item* | *Quantity* | Details
|
||||
| Breadboard | 1 | generic part
|
||||
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
|
||||
| 10 kΩ resistor | 1 | generic part
|
||||
| M/M Jumper wires | 4 | generic part
|
||||
| DHT-22 sensor | 1 | generic part
|
||||
|===
|
||||
83
gpio/dht_sensor/dht.c
Normal file
83
gpio/dht_sensor/dht.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
**/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/gpio.h"
|
||||
|
||||
const uint LED_PIN = PICO_DEFAULT_LED_PIN;
|
||||
const uint DHT_PIN = 15;
|
||||
const uint MAX_TIMINGS = 85;
|
||||
|
||||
typedef struct {
|
||||
float humidity;
|
||||
float temp_celsius;
|
||||
} dht_reading;
|
||||
|
||||
void read_from_dht(dht_reading *result);
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
gpio_init(LED_PIN);
|
||||
gpio_init(DHT_PIN);
|
||||
gpio_set_dir(LED_PIN, GPIO_OUT);
|
||||
while (1) {
|
||||
dht_reading reading;
|
||||
read_from_dht(&reading);
|
||||
float fahrenheit = (reading.temp_celsius * 9 / 5) + 32;
|
||||
printf("Humidity = %.1f%%, Temperature = %.1fC (%.1fF)\n",
|
||||
reading.humidity, reading.temp_celsius, fahrenheit);
|
||||
|
||||
sleep_ms(2000);
|
||||
}
|
||||
}
|
||||
|
||||
void read_from_dht(dht_reading *result) {
|
||||
int data[5] = {0, 0, 0, 0, 0};
|
||||
uint last = 1;
|
||||
uint j = 0;
|
||||
|
||||
gpio_set_dir(DHT_PIN, GPIO_OUT);
|
||||
gpio_put(DHT_PIN, 0);
|
||||
sleep_ms(20);
|
||||
gpio_set_dir(DHT_PIN, GPIO_IN);
|
||||
|
||||
gpio_put(LED_PIN, 1);
|
||||
for (uint i = 0; i < MAX_TIMINGS; i++) {
|
||||
uint count = 0;
|
||||
while (gpio_get(DHT_PIN) == last) {
|
||||
count++;
|
||||
sleep_us(1);
|
||||
if (count == 255) break;
|
||||
}
|
||||
last = gpio_get(DHT_PIN);
|
||||
if (count == 255) break;
|
||||
|
||||
if ((i >= 4) && (i % 2 == 0)) {
|
||||
data[j / 8] <<= 1;
|
||||
if (count > 16) data[j / 8] |= 1;
|
||||
j++;
|
||||
}
|
||||
}
|
||||
gpio_put(LED_PIN, 0);
|
||||
|
||||
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) {
|
||||
result->humidity = (float) ((data[0] << 8) + data[1]) / 10;
|
||||
if (result->humidity > 100) {
|
||||
result->humidity = data[0];
|
||||
}
|
||||
result->temp_celsius = (float) (((data[2] & 0x7F) << 8) + data[3]) / 10;
|
||||
if (result->temp_celsius > 125) {
|
||||
result->temp_celsius = data[2];
|
||||
}
|
||||
if (data[2] & 0x80) {
|
||||
result->temp_celsius = -result->temp_celsius;
|
||||
}
|
||||
} else {
|
||||
printf("Bad data\n");
|
||||
}
|
||||
}
|
||||
BIN
gpio/dht_sensor/pi-and-pico-uart-and-dht-sensor.fzz
Normal file
BIN
gpio/dht_sensor/pi-and-pico-uart-and-dht-sensor.fzz
Normal file
Binary file not shown.
BIN
gpio/dht_sensor/pi-and-pico-uart-and-dht-sensor.png
Normal file
BIN
gpio/dht_sensor/pi-and-pico-uart-and-dht-sensor.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 303 KiB |
BIN
gpio/dht_sensor/serial-output.png
Normal file
BIN
gpio/dht_sensor/serial-output.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 409 KiB |
Reference in New Issue
Block a user