Menu

728x90

468x60

Navbar

Showing posts with label Environment Sensor. Show all posts
Showing posts with label Environment Sensor. Show all posts

Friday, September 26, 2025

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing

The DHT11 temperature and humidity sensor features a temperature and humidity sensor
complex with a calibrated digital signal output. By using the exclusive digital-signal-acquisition
technique and temperature and humidity sensing technology, it ensures high reliability and
excellent long-term stability. This sensor includes a resistive-type humidity measurement
component and an NTC temperature measurement component, and connects to a high performance 8-bit micro-controller, offering excellent quality, fast response, anti-interference
ability and cost-effectiveness.

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing

Its standard TTL bi-directional I/O suitable for dsPIC30F2010 Interfacing and Programming. It needs only three wires, +5VDC, DATA and GND. Its DATA pin requires a weak pull up resistor with a resistance around 4.7k. 

I use my dsPIC30F2010 DIY Prototype Board to test this program. 

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
DHT11 Humidity & Temperature
Sensor

Parameters

Relative Humidity
Resolution:             16Bit
Repeatability:         ±1%RH
Accuracy:               25℃ ±5%RH
Interchangeability:  Fully interchangeable
Response time:       1/e (63%)25℃ 6s
                                1m/s Air 6s
Hysteresis:             <±0.3%RH
Long-term stability: <±0.5%RH/yr


Temperature
Resolution:             16Bit
Repeatability:         ±1℃
Accuracy:               25℃ ±2℃
Response time:       1/e (63%) 10S


Electrical Characteristics
Power supply:         DC 3.3~ 5.5V
Supply current:       Measure 0.3mA Standby 60μA
Sampling period:     Secondary Greater than 2 seconds 

For more detail about interfacing this sensor with a micro-controller please see this post. 

dsPIC30F2010 CCS PICC Programming

The newer version of CCS PICC (v5.119) has a C driver for the DHT-11 environmental sensor that could be called using a few lines of code. The "dht11.c" C driver locates in the drivers directory in program files. I use pin RB0 of dsPIC30F2010 to interface with this sensor.

The following example shows a simple sensor reading and data displaying over the RS-232.

  • board.h 
  1. #include <30F2010.h>
  2. #device ICSP=1
  3. #fuses HS,NODEBUG,NOWDT,PR,CKSFSM
  4. #use delay(crystal=20000000)

  5. #define LED0 PIN_D0
  6. #define LED1 PIN_D1
  7. #define SW0 PIN_C13
  8. #define SW1 PIN_C14

  9. #define DELAY 500

  10. #use rs232(UART1, baud=9600, stream=UART_PORT1)

 

  •  main.c
  1. #include "board.h"

  2. #define PIN_DHT11_DATA PIN_B0
  3. #include <dht11.c>

  4. void main(){
  5. unsigned int8 relativeHumidity;
  6. unsigned int8 tempC;
  7. printf("\n\rdsPIC30F2010 Prototype Board.");
  8. printf("\n\rWednesday 25 September 2025");
  9. printf("\n\rDHT-11 Humidity Sensor Example\n\r");
  10. printf("\r\n\r\dht-11.c - DHT11 example starting\r\n\r\n");
  11. dht11_init();
  12. delay_ms(1000);
  13. while(1){
  14. dht11_read(&relativeHumidity, &tempC);
  15. printf("HUMIDITY=%03u%%, TEMPERATURE=%02uC\r\n", relativeHumidity, tempC);
  16. delay_ms(2000);
  17. }
  18. }

 

  • Memory Use 


  •  Serial Input/Output Monitor

 


 
 Click here to download this example.

I add a 16x4 LCD (TC1604A-04) to display temperature and humidity data. Using a character LCD is very conventional for most small micro-controller programming and interfacing. I copied the "lcd.c" 16x2 LCD C driver to my project folder that I can modify it without changing the original file. I disable the LCD R/W pin (wire to GND) and I upgrade this LCD driver to a 16x4 LCD.

Its "board.h" header file remains the same to above example. 

  • main.c

 

  1. #include "board.h"

  2. #define PIN_DHT11_DATA PIN_B0
  3. #include <dht11.c>

  4. #define LCD_RS_PIN PIN_E4
  5. #define LCD_RW_PIN PIN_E5
  6. #define LCD_ENABLE_PIN PIN_E5
  7. #define LCD_DATA4 PIN_E0
  8. #define LCD_DATA5 PIN_E1
  9. #define LCD_DATA6 PIN_E2
  10. #define LCD_DATA7 PIN_E3

  11. #include "lcd.c"

  12. #define lcd_clear() lcd_putc('\f')
  13. #define lcd_home() lcd_putc('\a')
  14. /*For 16x4 LCD Only*/
  15. #define line_1() lcd_send_byte(0,0x80);
  16. #define line_2() lcd_send_byte(0,0xC0);
  17. #define line_3() lcd_send_byte(0,0x90);
  18. #define line_4() lcd_send_byte(0,0xD0);


  19. void main(){
  20. unsigned int8 relativeHumidity;
  21. unsigned int8 tempC;
  22. printf("\n\rdsPIC30F2010 Prototype Board.");
  23. printf("\n\rFriday 26 September 2025");
  24. printf("\n\rDHT-11 Humidity Sensor Example\n\r");
  25. printf("\r\n\r\dht-11.c - DHT11 example starting\r\n\r\n");
  26. dht11_init();
  27. lcd_init();
  28. printf(LCD_PUTC,"dsPIC30F2010 LCD");
  29. line_2();
  30. printf(LCD_PUTC,"DHT-11 Sensor");
  31. line_3();
  32. printf(LCD_PUTC,"Example Using");
  33. line_4();
  34. printf(LCD_PUTC,"CCS PICC v5.119");
  35. delay_ms(5000);
  36. lcd_clear();
  37. while(1){
  38. dht11_read(&relativeHumidity, &tempC);
  39. printf("HUMIDITY=%03u%%, TEMPERATURE=%02uC\r\n", relativeHumidity, tempC);
  40. lcd_home();
  41. printf(LCD_PUTC," DHT-11 Sensor");
  42. line_2();
  43. printf(LCD_PUTC," Reading:");
  44. line_3();
  45. printf(LCD_PUTC,"Humidity: %03u%%",relativeHumidity);
  46. line_4();
  47. printf(LCD_PUTC,"Temperature:%02u%cC",tempC,0xDF);
  48. delay_ms(1000);
  49. }
  50. }

 The TC1604A-04 works fine even it's too old.

dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing
Start-Up Screen


dsPIC30F2010 and DHT-11 Environmental Sensor Interfacing
DHT-11 Sensor Reading and Displaying

Click here to download this example.
 


Wednesday, December 27, 2023

PIC16F887 DHT-11 LCD Example Using XC8

DHT11 converts the surrounding temperature and humidity to serial digital data for any micro-processor. It uses only one serial data wire. It able to convert between 0 and 50 degree Celsius in temperature, and 0 to 90RH for humidity. It's suitable for air conditioning system, agricultural application, etc.

PIC16F887 DHT-11 LCD Example Using XC8
DHT-11 Sensor

PIC16F887 DHT-11 LCD Example Using XC8
Prototyping Board

It contains 5 bytes (40 bits) of digital data. They are 1 byte of humidity, 1 byte of temperature, and 1 byte of check sum. For more information about decoding its data byte please check this post.

PIC16F887 DHT-11 LCD Example Using XC8
DTH-11 Sensor

 
PIC16F887 DHT-11 LCD Example Using XC8
TC1604A-01 16x4 Character LCD Module

In this example, I use an 8-bit PIC micro-controller PIC16F887, a DHT-11 sensor module, and a 16x2 character LCD to read and display the environmental data. I use the Microchip MPLABX IDE v6.15 and its XC8 C compiler v2.36 (free version).

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on December 26, 2023, 9:03 PM
  6.  * PIC16F887 DHT-11 Sensor and LCD Example
  7.  * MPLABX IDE v6.15 XC8 v2.36
  8.  */
  9.  
  10. #include <xc.h>
  11. #include "config.h"
  12. #include "LCD4Bits.h"
  13. #include <stdio.h>
  14.  
  15. #define _XTAL_FREQ 8000000UL
  16.  
  17. #define DATA_PIN RD0
  18. #define DATA_DIR TRISD0
  19.  
  20. uint8_t data[5];
  21. void readDHT11(void){
  22. for(uint8_t i=0;i<5;i++) data[i]=0;
  23. DATA_DIR=0;
  24. DATA_PIN=1;
  25. __delay_ms(10);
  26. DATA_PIN=0;
  27. __delay_ms(18);
  28. DATA_PIN=1;
  29. __delay_us(30);
  30. DATA_PIN=0;
  31. DATA_DIR=1;
  32. __delay_us(10);
  33. while(DATA_PIN==0);
  34. __delay_us(10);
  35. while(DATA_PIN==1);
  36. __delay_us(10);
  37.  
  38. //Start of Transmission
  39. for(uint8_t i=0;i<5;i++) {
  40. for(uint8_t j=0;j<8;j++){
  41. __delay_us(5);
  42. while(DATA_PIN==0);
  43. __delay_us(50);
  44. if(DATA_PIN==1) { data[i]|=(1<<7-j); while(DATA_PIN==1);}
  45. }
  46. __delay_us(10);
  47. }
  48. /*CRC Calculation - the last byte data[4] is check sum*/
  49. uint8_t crc = data[0]+data[1]+data[2]+data[3];
  50. if(crc!=data[4]) {for(uint8_t i=0;i<4;i++) data[i]=0; return;}
  51. __delay_us(10);
  52. }
  53.  
  54.  
  55. void main(void) {
  56. OSCCONbits.IRCF=7;
  57. char msg[8];
  58. PORTA=0;
  59. TRISA=0;
  60. lcdInit();
  61. lcdString("Temperature:");
  62. __delay_ms(1000);
  63. while(1){
  64. readDHT11();
  65. sprintf(msg,"%2dRH",data[0]);
  66. lcdXY(1,2); lcdString(msg);
  67. sprintf(msg,"%2d%cC",data[2],223);
  68. lcdXY(8,2); lcdString(msg);
  69. __delay_ms(500);
  70. }
  71. return;
  72. }
  73.  

I use its 8MHz internal oscillator clock source leaving the XTAL1 and XTAL2 pin opened.

PIC16F887 DHT-11 LCD Example Using XC8

Dashboard

PIC16F887 DHT-11 LCD Example Using XC8

Running Program



PIC16F887 DHT-11 LCD Example Using XC8

Running Program

 

PIC16F887 DHT-11 LCD Example Using XC8

Simulating Program


Proteus VSM can be used for circuit design and simulation. Click here to download its source file.



Wednesday, October 11, 2023

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8

Overview

The DHT11 temperature and humidity sensor features a temperature and humidity sensor
complex with a calibrated digital signal output. By using the exclusive digital-signal-acquisition
technique and temperature and humidity sensing technology, it ensures high reliability and
excellent long-term stability. This sensor includes a resistive-type humidity measurement
component and an NTC temperature measurement component, and connects to a high performance 8-bit micro-controller, offering excellent quality, fast response, anti-interference
ability and cost-effectiveness.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
DHT11 Humidity & Temperature
Sensor

Parameters

Relative Humidity
Resolution:             16Bit
Repeatability:         ±1%RH
Accuracy:               25℃ ±5%RH
Interchangeability:  Fully interchangeable
Response time:       1/e (63%)25℃ 6s
                                1m/s Air 6s
Hysteresis:             <±0.3%RH
Long-term stability: <±0.5%RH/yr


Temperature
Resolution:             16Bit
Repeatability:         ±1℃
Accuracy:               25℃ ±2℃
Response time:       1/e (63%) 10S


Electrical Characteristics
Power supply:         DC 3.3~ 5.5V
Supply current:       Measure 0.3mA Standby 60μA
Sampling period:     Secondary Greater than 2 seconds 

Micro-controller Interfacing

This sensor uses only one bidirectional pin to accept a start signal from the micro-controller. It will send back its serial data with 40 bits length to the micro-controller. 

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Typical Application
A 5 kill Ohms pull up resistor is usable for a connection length below 20 meters. For the connection over 20 meters, choose a suitable value of pull up resistor. Its power supply is between 3 to 5.5V DC. For a stable operation at long range, use a an appropriate filtering capacitor.

Bi-directional Serial Communication

The micro-controller output pin is in normal high logic standby mode. It must issue a start command by putting its data pin to low level around 18 milliseconds, and then the data pin must be pull up to high logic for 30 microseconds. DHT11 will send a low to high logic respond signal to micro-controller.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Overall Communication Process

Micro-controller's data pin must be assigned to input direction to accept serial data from DHT11 sensor.

MCU sends out start signal and DHT11 responses
The response signal from DHT11 is a low (around 80 microseconds) to high logic level (around 80 microseconds).

After this response signal, the sensor will send its serial data with 40 bits length. A low logic level of 50 microseconds indicates a start of each data bits. After that is a high logic level with two distinct duration that identify between 0's and 1's logic value of each data bits.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Data"0" Indication
Logic high with a duration around 25 to 28 microseconds indicates a 0's data bit.
 
PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Data "1" Indication

Logic high with a duration around microseconds indicates a 1's data bit.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
logic 0's and 1's values

To sample this signal, the micro-controller should use a timer or a simple delay function to detect the duration of high logic signal.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Data Timing Diagram

After the transmission of its 40 bits data, a low end time with a duration of 50 microseconds indicate the end of transmission. It will turn to logic high due to a pull up resistor.

Data Format 

Its total data is 40 bits wide that include these data bytes,

  1. byte 0 - decimal humidity value
  2. byte 1 - empty humidity value
  3. byte 2 - decimal temperature value
  4. byte 4 - empty temperature value
  5. byte 5 - parity.

This sensor has only decimal humidity and temperature value. Its empty value data bytes are ignore.

It parity byte is useful for error checking. We can calculate as follow,

parity(byte 5) = byte 0 + byte 1 + byte 2 + byte 3

If this calculation is not verified then the micro-controller must read this sensor data again. For example, the sensor is read 53RH (humidity) and 24 degree Celsius (temperature) with a parity of 77 (in binary 0100 1101). We can calculate it as below,

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Parity Check
That is,

0011 0101 + 0000 0000 + 0001 1000 + 0000 0000 = 0100 1101

This 40-bit data is correct. So we can use it.

Micro-controller Interfacing And Programming

A simple 8-bit micro-controller could command and decode the serial data from this sensor easily. A 8051 micro-controller could interface with this sensor with a simple display output. The firmware require a little space of micro-controller program memory space.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Program Simulation

A simple 8-bit PIC micro-controller is suitable for this task, due to a little amount of I/O, small footprint, and a reasonable amount of RAM and ROM space. I use a PIC16F84A micro-controller. This controller has only digital I/O, without analog I/O and communication module. We don't need these peripheral here. I use Proteus VSM to simulate this sample program because this micro-controller was burn out a long time ago. I will not buy it more. Currently there are a lot of newly designed controllers from Microchip Technology with rich peripherals and low cost.

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
Sample Serial Data

We can see its serial data transmission over the virtual DSO screen of the simulator. To check this serial signal without a micro-controller we can use a switch instead with an oscilloscope. We just press the switch to make low signal and the release it. The output serial data of this sensor will be sent out.

  1. /*
  2.  * PIC16F84A DHT11 Temperature and Humidity Sensor Interfacing
  3.  * With HD44780 Character LCD
  4.  * MPLABX IDE v1.51
  5.  * XC8 v2.36
  6.  */
  7.  
  8. #include <xc.h>
  9. #include "LCD4Bits.h"
  10.  
  11. #define _XTAL_FREQ 4000000UL
  12. #define DATA_PIN RA4
  13. #define DATA_DIR TRISA4
  14.  
  15. uint8_t data[5];
  16. void readDHT11(void){
  17. for(uint8_t i=0;i<5;i++) data[i]=0;
  18. DATA_DIR=0;
  19. DATA_PIN=1;
  20. __delay_ms(10);
  21. DATA_PIN=0;
  22. __delay_ms(18);
  23. DATA_PIN=1;
  24. __delay_us(30);
  25. DATA_PIN=0;
  26. DATA_DIR=1;
  27. __delay_us(10);
  28. while(DATA_PIN==0);
  29. __delay_us(10);
  30. while(DATA_PIN==1);
  31. __delay_us(10);
  32.  
  33. //Start of Transmission
  34. for(uint8_t i=0;i<5;i++) {
  35. for(uint8_t j=0;j<8;j++){
  36. __delay_us(5);
  37. while(DATA_PIN==0);
  38. __delay_us(50);
  39. if(DATA_PIN==1) { data[i]|=(1<<7-j);}
  40. }
  41. __delay_us(10);
  42. }
  43. /*CRC Calculation - the last byte data[4] is check sum*/
  44. uint8_t crc = data[0]+data[1]+data[2]+data[3];
  45. if(crc!=data[4]) {for(uint8_t i=0;i<4;i++) data[i]=0; return;}
  46. __delay_us(10);
  47. }
  48. int main(void){
  49. PORTA=0;
  50. TRISA=0;
  51.  
  52. PORTB=0;
  53. TRISB=0;
  54.  
  55. lcdInit();
  56.  
  57. lcdXY(1,1); lcdString("Humidity : ");
  58. lcdXY(1,2); lcdString("Temperature: ");
  59.  
  60. while(1){
  61. readDHT11();
  62.  
  63. lcdXY(13,1);
  64. /*Humidity is below 90RH*/
  65. if(data[0]>=10) lcdData(48+(data[0]%100)/10);
  66. lcdData(48+(data[0]%10));
  67. lcdData('R');
  68. lcdData('H');
  69.  
  70. /*Temperature is below 50 degree Celsius*/
  71. lcdXY(13,2);
  72. if(data[2]>=10) lcdData(48+data[2]/10);
  73. lcdData(48+(data[2]%10));
  74. lcdData(223);
  75. lcdData('C');
  76. __delay_ms(500);
  77. }
  78. return 0;
  79. }

Built program uses only 50% of program memory. We can add a few switches and an output relay or transistor to set the a temperature condition with an output device (an AC or DC fan).

PIC16F84A DHT11 Temperature And Humidity Sensor And Character LCD Interfacing Using XC8
MPLABX IDE Build Output
The MPLABX IDE release is v1.51 (2012). I use the XC8 v2.36. It's a free version without code optimization and technical support. Click here to download its source file.




336x280

Search This Blog

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (34) Analog Comparator (1) Arduino (16) ARM (13) AT89C52 (7) ATMega32 (58) ATMega644P (27) AVR (87) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (4) Display (133) Distance Sensor (3) ds1307 (10) DS18B20 (5) ds3231 (1) ds3232 (2) dsPIC (5) dsPIC30F1010 (3) dsPIC30F2010 (5) EEPROM (5) Environment Sensor (5) ESP32 (1) esp8266 (1) Graphical LCD (2) I2C (38) ILI9341 (1) Input/Output (76) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (2) LCD (75) LM35 (3) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) Module PCB (1) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (10) PCD8544 (3) PCF8574 (10) PIC (108) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (13) SH1106 (3) Shift Register (13) Shift Registers (10) Software TWI (2) SPI (39) ST7735 (1) STM32 (11) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (5) STM32CubeIDE (13) STM32F103C8T6 (9) STM32F401CCU6 (1) SysTick (4) temperature sensor (13) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (8) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)