/*
By Henderson Hood.
This code is designed to initiate communication with a DHT11 temperature
and humidity sensor. Its main objective is to send the required start signal
to the sensor over its 1-wire bus. After sending the start signal, the code
configures the Raspberry Pi Pico to listen for the sensor's expected 40-bit
data response, which includes temperature and humidity data.
The program then enters a loop, allowing for the observation of the data
line's activity, particularly the sensor's response, using a virtual
logic analyzer.
To view the 40-bit data captured here by the virtual Logic analyzer, run the program and
then exit. Save the file. Open the file in Pulseview or similar viewer to see the
PWM based data from the DHT11. Part 2 of this Lab (LAB-DHT11-part2) will decode and
print the temperature and humidity returned by the DHT11 on the terminal.
*/
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define DHT11_PIN 5
int main() {
stdio_init_all();
gpio_init(DHT11_PIN);
// Send start signal
gpio_set_dir(DHT11_PIN, GPIO_OUT);
sleep_ms(1);
gpio_put(DHT11_PIN, 0); // Pull low for at least 18ms
sleep_ms(18);
// Pull high for 20-40us
gpio_set_dir(DHT11_PIN, GPIO_IN); //configure PIN as input will release the bus
sleep_us(30); //wait for 40-bit data to be ready
//
while (1) {
// Keep the program running to observe the signal on an oscilloscope
tight_loop_contents();
}
return 0;
}
/*
Think of this as the Pico starting a chat with the DHT11 using the 1-wire communication protocol.
They use a single data line for this.
Here's how the code works on this 1-wire bus:
1. Initialization: The code sets up standard input/output. It also prepares the designated
GPIO pin (pin 5).
2. Sending the Start Signal on the 1-Wire Bus: This uses the specific behavior of
the 1-wire bus.
◦ Initially, the 1-wire bus is high. This is due to pull-up resistors. The DHT11 has a
built-in pull-up resistor.
◦ The Pico configures the GPIO pin as an output.
◦ To start, the Pico actively pulls the 1-wire bus low. It holds it low for a
specific time (1ms initially, then 18ms). This forces the bus from its normal high state.
◦ This prolonged low signal tells the DHT11 the host (Pico) wants to communicate.
◦ After holding it low (18ms), the code releases the 1-wire bus by setting the pin to input.
◦ Due to the DHT11's internal pull-up, the bus will return to its idle high state.
◦ A short pause (30 microseconds) allows the DHT11 to get ready to respond on
the high 1-wire bus.
3. Waiting and Observing the Response on the 1-Wire Bus: The code enters an infinite
loop to keep running. This allows you to watch the 1-wire data bus with your
logic analyzer. Look for the DHT11 to take control after the Pico releases it
(pin set to input). The DHT11 will acknowledge by pulling the 1-wire bus low briefly,
then high, before sending the 40-bit data.
To summarize the key points:
• Communication uses a 1-wire bus.
• The 1-wire bus is normally high due to pull-up resistors, including in the DHT11.
• The Pico starts by actively pulling the 1-wire bus low.
• After the start signal, the Pico releases the bus.
• The DHT11 then controls the 1-wire bus to send its response.
By observing the logic analyzer, you can see the pulse train (40 pulses: short pulse is '0'
long pulse is '1') on this single wire as the Pico sends the start signal and the DHT11
tries to respond using its 1-wire protocol. Seeing the initial low pulse from the Pico,
followed by the DHT11 pulling the line low and then high, suggests the sensor is likely working.
In part two, we will decode print this PWM encoded data into Temperature and Humidty values.
*/
pico:GP0
pico:GP1
pico:GND.1
pico:GP2
pico:GP3
pico:GP4
pico:GP5
pico:GND.2
pico:GP6
pico:GP7
pico:GP8
pico:GP9
pico:GND.3
pico:GP10
pico:GP11
pico:GP12
pico:GP13
pico:GND.4
pico:GP14
pico:GP15
pico:GP16
pico:GP17
pico:GND.5
pico:GP18
pico:GP19
pico:GP20
pico:GP21
pico:GND.6
pico:GP22
pico:RUN
pico:GP26
pico:GP27
pico:GND.7
pico:GP28
pico:ADC_VREF
pico:3V3
pico:3V3_EN
pico:GND.8
pico:VSYS
pico:VBUS
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
logic1:D0
logic1:D1
logic1:D2
logic1:D3
logic1:D4
logic1:D5
logic1:D6
logic1:D7
logic1:GND