Measurement of external parameters like humidity and temperature is critical for meteorological applications, medical devices, agriculture and industrial drying systems.
Presented here is a Humidity module HYT271 that provides low drift and good stability at high humidity range. This gives excellent humidity accuracy of 1.8%RH and reproducibility of 0.2%RH.
In this tutorial, you will know how to interface humidity sensor HYT 271 with 8051 using I2C.
HYT 271 can be connected to any microcontroller like PIC, Renesas, MSP430, STM microelectronics, 8051, ARM 7 etc.
But, before going to know the humidity sensor interfacing, it is important to know about the hardware components of the capacitive humidity sensor.
Circuit Components
- HYT 271 Humidity sensor -1
- Microcontroller (8051, ARM, PIC, MSP430 etc.)
- 2.2 KΩ resistors -2
- Connecting wires
The cost of the humidity sensor is around 23$.
This application measures the temperature and humidity in the environment by sending commands to the microcontroller using I2C protocol. Hence, a low cost 8 bit microcontroller is enough for building the temperature and humidity sensor project.
HYT 271 Humidity Sensor Connection Diagram
The below figure shows the interfacing of HYT 271 humidity sensor to the 8051 microcontroller.
The capacitive humidity sensor has 4 pins to communicate with the microcontroller.
- Pin 1 – SDA
- Pin 2 – GND
- Pin 3 – VCC
- Pin 4 – SCL
Working of HYT 271
After Power on, the RH (Relative Humidity) sensor is in sleep mode. In this mode, the sensor will not measure the humidity and temperature. To read the humidity and temperature values, you have to issue the proper commands using I2C interface.
HYT 271 module supports two commands to read the humidity and temperature.
- Measuring Request (MR)
- Data Fetch Request (DF)
The 8051 microcontroller writes the MR command using the following I2C write sequence.
- Start Bit
- Humidity sensor write Address
- Acknowledgment Bit from the sensor
- Stop bit
Now, after sending the MR request the sensor will wake up and calculates the humidity, temperature. This measured value is stored in a register.
To read the sensor data, a DF request is sent to the microcontroller using following I2C read sequence.
- Start Bit
- Humidity Sensor Read Address
- Acknowledgment Bit from the sensor
- Read the Data
- Acknowledgment Bit from the microcontroller
- Stop bit
C Program of HYT 271 Humidity sensor
The embedded C code is compatible with all humidity sensors from HYT series. IST (Innovative sensor technology) provides HYT 271, HYT 221 and HYT 939.
To get started with coding, you need to know the address of the sensor. In this case, the default slave address for the humidity module is 0x28.
The Write address for the HYT is 0x50 and the read address is 0x51.
I have written two functions for the MR request and DF request. To enable i2c communication interface, I have used the bit-banging method by defining two I/O pins of the microcontroller as SDA and SCL.
I have chosen 8051 microcontroller pins (Port 1.4 and Port 1.5) for interfacing HYT 271.
/*Embedded C Code for HYT 271 Humidity sensor using 8051 * Author: Kumar B * ©2018 Codrey Electronics */ #include<reg51.h> #include<math.h> #define SDA_OUT P1=0x00 /* Configuring Port 1 as Output*/ #define SDA_IN P1=0XFF /* Configuring Port 1 as Input*/ sbit SCL=P1^4; /*SCL connected to Port 1.4 of 8051*/ sbit SDA=P1^5; /*SDA connected to port 1.5 of 8051*/ #define SCL_ENABLE SCL=1 /*Configure the SCL as HIGH*/ #define SCL_DISABLE SCL=0 /*Configure the SCL as LOW*/ #define SDA_ENABLE SDA=1 /*configure the SDA as HIGH*/ #define SDA_DISABLE SDA=0 /*configure the SDA as LOW*/ #define HYT_Address 0x28 //Slave address of HYT 271 #define RDADDR 0x51 //Read address of hyt #define WRADDR 0x50 //Write address of hyt void msdelay(int milli_sec); void Start(void); void Stop(void); void Ack_rd(void); void PC_Write(unsigned char *str); void PC_Write_SendByte(unsigned char d); void DF_request(void); void No_Ack(); void MR_Request(void); void Out_Byte(unsigned char byte); void Baudrate_9600(void ); int read_data_hum; char p[7],hum[10]; unsigned char read_data_temp; /* A Function to send a string to the PC terminal */ void PC_Write(const unsigned char *str) { while(1) { if(*str =='\0') break; PC_Write_SendByte(*str++); msdelay(10); } } /*A Function to send a single byte to PC*/ void PC_Write_SendByte(unsigned char d) { SBUF=d; //Put single byte in SBUF (serial Buffer memory)// while(TI==0); //Wait until the byte is transmitted from serial buffer to PC// TI=0; //clear TI flag to send next byte to the SBUF// } /* This function generates baud rate of 9600 bits per second */ /*Displays serial character on to the PC hyper terminal*/ void Baudrate_9600 (void ) { /*-------------------------------------- Set serial port for 9600 baud at 11.0592 MHz. ------------------- -------------------*/ SCON = 0x50; /*Sets the Serial Control Register*/ TMOD |= 0x20; /*Configuring the Timer register in Timer mode*/ TH1 = 0xFD; /*Higher byte of Timer 1*/ TR1 = 1; /*This will start the timer 1 in run mode*/ P3=0X03; /*Setting the TXD pin*/ } //Function to generate delay in milli seconds // void msdelay(int milli_sec) { unsigned char j ; for(; milli_sec>0; milli_sec--) { for(j=255; j>0; j--); for(j=232; j>0; j--); } } //Start condition for I2c communication with HYT271// void Start(void) { SCL_DISABLE; SDA_ENABLE; SCL_ENABLE; SDA_DISABLE; } //Stop condition for I2c communication with HYT271// void Stop(void) { SCL_DISABLE; SDA_DISABLE; SCL_ENABLE; SDA_ENABLE; } //Funtion to receive Acknowledgment condition from slave HYT271// void Ack_rd(void) { SCL_DISABLE; SDA_ENABLE; SCL_ENABLE; while(SDA); } //Function to send NO Acknowledgement to HYT271// void No_Ack( ) { SCL_DISABLE; SDA_ENABLE; SCL_ENABLE; } // Function to send a byte of data to HYT271// void Out_Byte(unsigned char byte) { unsigned char j,k; k = byte; for (j= 0; j < 8; j++) { SCL_DISABLE; SDA=(k&(0x80>>j))?1:0; /*Writing bit_by_bit*/ SCL_ENABLE; } } /* Measurement request to HYT 271 to start calculating humidity and temperature */ void MR_Request(void) { Start( ); Out_Byte(WRADDR); Ack_rd( ); Stop( ); msdelay(60); } /*Function to read a byte of data from HYT slave*/ unsigned char Read_Byte_E2(void) { unsigned char m,data_byte=0; for(m=0; m<8; m++) { SCL_DISABLE; SCL_ENABLE; if(SDA) data_byte|=0x80>>m; } return data_byte; } /*This function receives the humidity and temperature data from the slave */ void DF_request(void) { char i=0,j=0; Start( ); Out_Byte(RDADDR); Ack_rd( ); read_data_hum=Read_Byte_E2( ); //Fetching 2 bytes of humidity read_data_hum=(read_data_hum<<8)& 0x3fff; Ack_rd( ); read_data_hum=((read_data_hum | Read_Byte_E2( )) & 0xffff); read_data_hum = 100.0 / pow(2,14) * read_data_hum; //conversion for humidity calculation PC_Write("Humidity="); while(read_data_hum) //converting humidity value to ascii format// { p[i]=(read_data_hum%10)+0x30; read_data_hum = read_data_hum/10; i++; } p[i]='\0'; for(j=0; p[j]!='\0'; j++) //Reversing the humidity data for displaying { hum[j]=p[(i-1)-j]; } PC_Write(hum); PC_Write("\r\n"); No_Ack( ); Stop(); Ack_rd( ); read_data_temp=Read_Byte_E2( ); read_data_temp = 165.0 / pow(2,14) * read_data_temp-40; //conversion calculation for temperature// while(read_data_temp) { p[i]=(read_data_temp%10)+0x30; read_data_temp = read_data_temp/10; //converting temperature byte to ascii format// i++; } p[i]='\0'; for(j=0; p[j]!='\0'; j++) { hum[j]=p[(i-1)-j]; } PC_Write(hum); PC_Write("\r\n"); } /*The main code starts from here*/ void main() { Baudrate_9600(); //Setting for 9600 baud rate// MR_Request(); //Measurment request to HYT271// DF_request(); //Data fetch from hyt 271// }The above code snippet is tested on 8051 and you can interface the humidity sensor to another microcontroller you want.
The only thing to do is change the pin configuration of SDA and SCL.
Conclusion
The HYT 271 Humidity sensor works on 3.3V power rails. By providing the excess voltage may damage the sensor. So, be careful. To make the humidity sensor to work accurately it is recommended to use the pull-up resistors 2.2K Ω.
Do you have any idea of implementing different logic? If yes, share with us.
Hi,I use you code in 89C52.The Humidity is OK,but the temp is always 255. Can you give me a help,Thank you