DHT11 Pico Library - C++ - Raspberry Pi Pico  1.0
The DHT11 Pico Library provides a C++ implementation for interfacing with the DHT11 temperature and humidity sensor on the Raspberry Pi Pico microcontroller. It offers functions to read temperature and relative humidity from the sensor and supports error handling. This library simplifies the integration of the DHT11 sensor into Pico-based projects, allowing developers to easily retrieve environmental data for various applications.
dht11-pico.cpp
Go to the documentation of this file.
1 
11 #include "dht11-pico.h"
12 
13 Dht11::Dht11(uint pin){
14  gpioPin=pin;
15  gpio_init(pin);
16  sleep_ms(1000);
17 }
18 
20  gpio_deinit(gpioPin);
21 }
22 
23 long long Dht11::read(){
24  int count=0;
25  long long raw=0;
26  gpio_set_dir(gpioPin, GPIO_OUT);
27  gpio_put(gpioPin,0);
28  sleep_ms(20);
29  gpio_set_dir(gpioPin, GPIO_IN);
30 
31  while(gpio_get(gpioPin)==1){
32  count++;
33  sleep_us(5);
34  if(count==POLLING_LIMIT){
35  return TRANSMISSION_ERROR;
36  }
37  }
38 
39  count=0;
40  while(gpio_get(gpioPin)==0){
41  count++;
42  sleep_us(5);
43  if(count==POLLING_LIMIT){
44  return TRANSMISSION_ERROR;
45  }
46  }
47 
48  count=0;
49  while(gpio_get(gpioPin)==1){
50  count++;
51  sleep_us(5);
52  if(count==POLLING_LIMIT){
53  return TRANSMISSION_ERROR;
54  }
55  }
56 
57  //transmission start
58  for(int i=0;i<40;i++){
59  count=0;
60  //~50us
61  while(gpio_get(gpioPin)==0){
62  sleep_us(5);
63  }
64  //bit 0 or 1
65  while(gpio_get(gpioPin)==1){
66  sleep_us(5);
67  count++;
68  }
69  if(count>=THRESHOLD){
70  raw|=1;
71  }
72  raw=raw<<1;
73  }
74  //check if raw data is valid
75  if(
76  ((raw & RH_INT_MASK)>>32) +
77  ((raw & RH_DEC_MASK)>>24) +
78  ((raw & TEMP_INT_MASK)>>16) +
79  ((raw & TEMP_DEC_MASK)>>8) -
80  ((raw & CHECKSUM_MASK)) > 1
81  ){
82  return TRANSMISSION_ERROR;
83  }
84  return raw;
85 }
86 
87 double Dht11::readT(){
88  long long raw=read();
89  if(raw==TRANSMISSION_ERROR){
90  return TRANSMISSION_ERROR;
91  }
92  int temp_int = (raw & TEMP_INT_MASK)>>16;
93  uint temp_dec = (raw & TEMP_DEC_MASK)>>8;
94  if(temp_int<0)
95  temp_dec=-temp_dec;
96  double temp = temp_int + 0.1*temp_dec;
97  return temp;
98 }
99 
100 double Dht11::readRH(){
101  long long raw=read();
102  if(raw==TRANSMISSION_ERROR){
103  return TRANSMISSION_ERROR;
104  }
105  int rh_int = (raw & RH_INT_MASK)>>32;
106  uint rh_dec = (raw & RH_DEC_MASK)>>24;
107  double rh = rh_int + 0.1*rh_dec;
108  return rh;
109 }
110 
111 void Dht11::readRHT(double *temp, double *rh){
112  long long raw=read();
113  if(raw==TRANSMISSION_ERROR){
114  *temp= *rh = TRANSMISSION_ERROR;
115  return;
116  }
117  int temp_int = (raw & TEMP_INT_MASK)>>16;
118  uint temp_dec = (raw & TEMP_DEC_MASK)>>8;
119  int rh_int = (raw & RH_INT_MASK)>>32;
120  uint rh_dec = (raw & RH_DEC_MASK)>>24;
121 
122  *temp = temp_int + 0.1*temp_dec;
123  *rh = rh_int + 0.1*rh_dec;
124 }
long long read(void)
Private method to read raw data from the DHT11 sensor.
Definition: dht11-pico.cpp:23
double readRH(void)
Read and retrieve the humidity value from the DHT11 sensor.
Definition: dht11-pico.cpp:100
uint gpioPin
GPIO pin connected to the DHT11 sensor.
Definition: dht11-pico.h:63
Dht11(uint pin)
Dht11 class constructor. Initializes GPIO and waits for the sensor to stablize.
Definition: dht11-pico.cpp:13
double readT(void)
Read and retrieve the temperature value from the DHT11 sensor.
Definition: dht11-pico.cpp:87
~Dht11()
Dht11 class destructor. De-initialize GPIO.
Definition: dht11-pico.cpp:19
void readRHT(double *temperature, double *rel_humidity)
Read both temperature and humidity values from the DHT11 sensor.
Definition: dht11-pico.cpp:111
DHT11 Sensor Library for Raspberry Pi Pico.
const int THRESHOLD
Threshold for differentiating between bit 0 and bit 1 during DHT11 data transmission.
Definition: dht11-pico.h:18
const int POLLING_LIMIT
Maximum number of polling attempts during DHT11 data transmission.
Definition: dht11-pico.h:23
const long long RH_DEC_MASK
Bit mask to extract the decimal part of relative humidity from the raw data.
Definition: dht11-pico.h:38
const long long CHECKSUM_MASK
Bit mask to extract the checksum from the raw data.
Definition: dht11-pico.h:53
const long long TEMP_INT_MASK
Bit mask to extract the integer part of temperature from the raw data.
Definition: dht11-pico.h:43
const int TRANSMISSION_ERROR
Error value returned when there is a transmission error during DHT11 data reading.
Definition: dht11-pico.h:28
const long long TEMP_DEC_MASK
Bit mask to extract the decimal part of temperature from the raw data.
Definition: dht11-pico.h:48
const long long RH_INT_MASK
Bit mask to extract the integer part of relative humidity from the raw data.
Definition: dht11-pico.h:33