 |
 |
View previous topic :: View next topic |
Author |
Message |
GAMCH96
Joined: 13 Aug 2025 Posts: 5
|
PROBLEM INTERFACE DHT22 PIC16F876 |
Posted: Thu Aug 21, 2025 11:02 am |
|
|
Good morning!!
In this case, I want used DHT22 Sensor and Driver DHT22.C . the data obtained in this program is incorrect.
Could you help, place?
//This is DHT22.C Libary Used:
#define dht22 PIN_A2
#bit dht_io = 0xf92.2
byte dht22_dat[5];
byte leer_datos_dht();
Void dht_init (void){
dht_io=0;
delay_ms(1);
output_high(dht22);
}
byte leer_datos_dht(){
byte i = 0;
byte result=0;
for (i=0; i< 8; i++) {
//We enter this during the first start bit (low for 50uS) of the byte
//Next: wait until pin goes high
while(input(dht22)==0);
delay_us(30);
if (input(dht22)==1){//Was: if(PINC & _BV(dht_PIN))
result |=(1<<(7-i));
}
while (input(dht22)==1);
//Was: while((PINC & _BV(dht_PIN)));
}//end of "for.."
return result;
}
Void leer_dht22 (float &dhthum,float &dthtemp){ //byte GlobalErr=0;
byte dht22_in, i, dht22_checksum;
int16 temperatura, humedad;
float temp,hum;
dht_io=0; // configurar el pin como salida
output_high(dht22);
delay_us(20);
output_low(dht22);
delay_ms(18);
output_high(dht22);
delay_us(22);
dht_io=1;// configurar el pin como entrada
delay_us(5);
dht22_in=input(dht22);
if(dht22_in) {
//GlobalErr=1;
printf("\r\ndht condicion 1 de inicio no encontrada");
return;
}
delay_us(80);
dht22_in=input(dht22);
if(!dht22_in){
//GlobalErr=2;
printf("\r\ndht condicion 2 de inicio no encontrada");
return;
}
delay_us(80);
for (i=0; i<5; i++){
dht22_dat[ i ] = leer_datos_dht(); // capturando datos
}
dht_io=0;
delay_us(10);
output_high(dht22);
dht22_checksum= dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3];
if(dht22_dat[4]!=dht22_checksum){
//GlobalErr=3;
printf("\r\nDHT checksum error");
}
humedad = make16(dht22_dat[0],dht22_dat[1]);
temperatura = make16(dht22_dat[2],dht22_dat[3]);
hum = humedad;
temp = temperatura;
dhthum = (hum)/100;
dthtemp = (temp)/100;
}
//Test Code for a Microntroller PIC16F876 :
//LCD module connections PUERTO B
#define LCD_RS_PIN PIN_B0
#define LCD_RW_PIN PIN_B1
#define LCD_ENABLE_PIN PIN_B2
#define LCD_DATA4 PIN_B3
#define LCD_DATA5 PIN_B4
#define LCD_DATA6 PIN_B5
#define LCD_DATA7 PIN_B6
//End LCD module connections
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use rs232 (BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7) // Tx a 9600 BAUDS VIA RS-232 STANDAR
#include "dht22.c"
#include <lcd4x20b.c>
#use fast_io(A)
// Connection pin between PIC16F877A and DHT11 (RHT01) sensor ENTRADA RA0
#BIT Data_Pin = 0x05.0 // Pin mapped to PORTA.0
#BIT Data_Pin_Direction = 0x85.0 // Pin direction mapped to TRISA.0
//Pal bootloader
#build (reset=0x1000,interrupt=0x1008)
#org 0x0000,0x0FFF{}
void main(){
//int16 lux;
float dhthum, dthtemp;
lcd_init();
lcd_putc("\fSensor T-H\nDHT22 RHT03");
printf ("\f\r\nSensor de temperatura y humedad:\r\nDHT22-RHT03");
delay_ms(500);
//printf(lcd_putc,"\fHola-Saludos\nEnviame un dato");
dht_init();
delay_ms(300);
lcd_putc("\f");
while(true){
//set_adc_channel(0);
//delay_ms(1);
// lux=read_adc();
// printf("\r\nAdc =: %lu ",lux);
//delay_ms(10);
//leer_dht();
leer_dht22(dhthum, dthtemp);
// sht21_both(temp,humid);
delay_ms(10);
lcd_putc('\f'); // LCD clear
lcd_gotoxy(1, 1); // Go to column 1 row 1
printf(lcd_putc, "RH = %f %%",dhthum); // Display message1
lcd_gotoxy(1, 2); // Go to column 1 row 2
printf(lcd_putc, "Tem = %f %cC", dthtemp,223); // Display message2
printf("\r\nDHT22 RH = %f %%",dhthum);
delay_ms(1);
printf("\r\nDHT22 Temp = %f %cC", dthtemp,223);
delay_ms(1);
delay_ms(2000);
}
} |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9569 Location: Greensville,Ontario
|
|
Posted: Fri Aug 22, 2025 8:19 am |
|
|
1st step..
dump out the RAW data from the sensor and YOU decode the data into real temperature and humidity. That way you'll know if it's the sensor or the 'math' that the PIC program is doing. |
|
 |
GAMCH96
Joined: 13 Aug 2025 Posts: 5
|
PROBLEM INTERFACE DHT22 PIC16F876 |
Posted: Fri Aug 22, 2025 9:35 am |
|
|
Thank you..with pleasure.. I will verify it and check it..thank you....  |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19929
|
|
Posted: Sat Aug 23, 2025 3:09 am |
|
|
There are fundamental faults with what is being done.
You don't want to pull the DHT22 signal high. This needs to be done
by an external pull-up resistor. The signal should be wired so it floats high.
The PIC then pulls this wire low, waits, and then lets the line float so the
sensor can take control of the wire. Pulling it high, the two chips will
fight one another.
The DHT is an open-collector output drive device.
The sequence should be:
output low on the signal
wait >1mSec
output float the signal (you can do this by driving high, and then
reading immediately).
You then read the signal from the DHT.
Do you have a pull up resistor?. It won't work unless you have.
Also, you do not tell us your processor clock rate?. The method you are
using to generate the bit mask for the data is very slow. This could
easily be taking too long, and then the samplign will not be at the right
place in subsequent bits.
Then the actual sampling is wrong. Whether a bit is 1 or 0 depends on the
_length_ of the high signal after the 50iSec low pulse. If it is about 30uSec
the bit is a 0 if it is around 70uSec, the bit is a 1. You therefore need to
be reading the level about 60uSec after the rising edge. If the signal is
high at this point it is a 1 otherwise a 0. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|