Welcome!
...
Previous Notifications
2024 2nQ
Composite Sensors - Part 2
6min
dht 11 for part 2, the dh11 will be our focus the dht11 is a cost effective digital temperature and humidity sensor using a capacitive humidity sensor and a thermistor to measure the surrounding air, a digital equivalent is returned on the data pin (no analog input pins needed the sensor is easily identifiable in its powder blue housing with 4 legs protruding these four legs are vcc, gnd, nc (not concerned), and data if you purchase the sensor all by its lonesome, then there is an extra pin to ignore as shown below if you purchase as a ready module, the manufacturer may have removed access to the pin these two scenarios are illustrated below owing to the construction of the sensor, it is not advisable to use it in highly moist or corrosive environments the dht22 which looks like the dht11 but has white or ivory packaging is similar in function to the dht11, it's just more accurate the dht22 will also read a negative or below zero temperature, whereas the dht11 is above zero only please see dht deep dive docid\ p63a6ry6plviuncjzkfg1 for more engineering content connection diagram when connecting or wiring the sensor, there are a few considerations the sensor has a specific voltage operating range, and this makes it usable for low power devices as well as devices operating at a regular voltage there is a single pin that functions to return data from the sensor to the mcu interacting with it power supply the device requires 5vdc but can also work at 3v3; this can be useful for devices that are k or j variants that operate at 3v3 the output of the device is a digital value, not an analog one which ensures a reliable return from the device code example c // lcd module connections sbit lcd rs at rb2 bit; sbit lcd en at rb3 bit; sbit lcd d4 at rb4 bit; sbit lcd d5 at rb5 bit; sbit lcd d6 at rb6 bit; sbit lcd d7 at rb7 bit; sbit lcd rs direction at trisb2 bit; sbit lcd en direction at trisb3 bit; sbit lcd d4 direction at trisb4 bit; sbit lcd d5 direction at trisb5 bit; sbit lcd d6 direction at trisb6 bit; sbit lcd d7 direction at trisb7 bit; sbit data at ra0 bit; sbit datadir at trisa0 bit; char message1\[] = "temp = 00 0 c"; char message2\[] = "rh = 00 0 %"; unsigned short tout = 0, checksum, i; unsigned short t byte1, t byte2, rh byte1, rh byte2; void startsignal(){ datadir = 0; // data port is output data = 0; delay ms(25); // low for at least 18us data = 1; delay us(30); // high for 20 40 us datadir = 1; // data port is input } unsigned short checkresponse(){ tout = 0; tmr2 = 0; t2con tmr2on = 1; // start tmr2 while waiting for sensor response while(!data \&\& !tout); // if there's no response within 256us, the timer2 overflows if (tout) return 0; // and exit else { tmr2 = 0; while(data \&\& !tout); if (tout) return 0; else { t2con tmr2on = 0; return 1; } } } unsigned short readbyte(){ unsigned short num = 0, t; datadir = 1; for (i=0; i\<8; i++){ while(!data); tmr2 = 0; t2con tmr2on = 1; // start tmr2 from 0 when a low to high data pulse while(data); // is detected, and wait until it falls low again t2con tmr2on = 0; // stop the tmr2 when the data pulse falls low if(tmr2 \> 40) num |= 1\<\<(7 i); // if time \> 40us, data is 1 } return num; } void interrupt(){ if(pir1 tmr2if){ tout = 1; t2con tmr2on = 0; // stop timer pir1 tmr2if = 0; // clear tmr0 interrupt flag } } void main() { unsigned short check; trisb = 0b00000000; portb = 0; trisa = 0b00100001; cmcon = 7; intcon gie = 1; //enable global interrupt intcon peie = 1; //enable peripheral interrupt // configure timer2 module pie1 tmr2ie = 1; // enable timer2 interrupt t2con = 0; // prescaler 1 1, and timer2 is off initially pir1 tmr2if =0; // clear tmr int flag bit tmr2 = 0; lcd init(); lcd cmd( lcd clear); lcd cmd( lcd cursor off); do { delay ms(1000); startsignal(); check = checkresponse(); if (!check) { lcd cmd( lcd clear); lcd out(1, 1, "no response"); lcd out(2, 1, "from the sensor"); } else{ rh byte1 = readbyte(); rh byte2 = readbyte(); t byte1 = readbyte(); t byte2 = readbyte(); checksum = readbyte(); // check for error in data reception if (checksum == ((rh byte1 + rh byte2 + t byte1 + t byte2) \& 0xff)) { message1\[7] = t byte1/10 + 48; message1\[8] = t byte1%10 + 48; message1\[10] = t byte2/10 + 48; message2\[7] = rh byte1/10 + 48; message2\[8] = rh byte1%10 + 48; message2\[10] = rh byte2/10 + 48; message1\[11] = 223; // degree symbol lcd cmd( lcd clear); lcd out(1, 1, message1); lcd out(2, 1, message2); } else{ lcd cmd( lcd clear); lcd out(1, 1, "checksum error!"); lcd out(2, 1, "trying again "); } } }while(1); } moving on composite sensors part 3 composite sensors part 3 docid\ dssul3tep kxlzm18k7yu reference https //www circuitgeeks com/arduino dht11 and dht22 sensor tutorial/ https //www circuitgeeks com/arduino dht11 and dht22 sensor tutorial/