SHT75 Sensor for the measurement of Humidity and Temperature using PIC16F876A
In this Microcontroller Lab experiment , we will learn to measure the relative humidity and temperature by using a integrated sensor HST75 and PIC microcontroller 16F876A.
Introduction of Microcontroller Project:
There are many applications where we need to
measure the ambient Temperature and relative humidity, as these are very
important for human as well as all living organisms. Like if anyone want to
make an incubator, then he always which to measure the temperature and humidity
if its incubator to give proper environment for eggs to have good results. If
the temperature of environment is not favorable, it high or low, the results
from incubator will not be good. To control these two parameters, first step is
to measure and then second step comes to control. To maintain Temperature, the
heater and Fan combination with proper control will serve the job. Similarly,
to maintain the humidity the Humidifiers and dehumidifiers will keep
indoor humidity at a comfortable and desired level.
Scope of Project:
In this project first step is address in which
we will measure the humidity and temperature using PIC microcontroller and an
integrated digital sensor for the detection of humidity and temperature. The
PIC Microcontroller we will use in this project will be PIC 16F876A. The
integrated sensor for the detection of these physical parameters from
environment will be SHT75, this is a chip sensor which includes transducers for
both humidity and temperature with it.
Schematic circuit diagram of Microcontroller Project:
below is the circuit diagram for the measurement of humidity and temperature using SHT75 and PIC Microcontroller.
SHT75 Humidity and Temperature PIC16F876A |
The Digital Sensor SHT75:
The sensor we are using in this project is a Sensirion’s SHT series of digital sensors number SHT75. The
SHT75 have built-in transducers for measuring the
temperature and relative humidity. The output of the SHT75 sensor will provide
calibrated digital outputs in the form of digital signal. The digital output
signal having the measured results of two parameter The Temperature and
relative humidity will be interfaced to PIC Microcontroller PIC16F876A. The
details of the sensor, the specification of sensor, its interface, and its
communication protocol are being discussed in next section of this post. Then the
schematic circuit diagram, implementation of the communication protocol in
coding will be presented at the end of this post.
Specification of SHT75 Sensor:
As discussed earlier the SHT75 Sensor is made by Sensirion. There are various SHT series of digital sensors for
measuring both relative humidity and temperature. But here in this project we
will use a specific number SHT75. The sensing element of temperature and
humidity are integrated along with some necessary electronics components. Like
the sensor for humidity is capacitive. On the variation of moisture levels in
the surrounding of transducer will changes the dielectric constant of two
plates of a parallel-plate capacitor as a result the capacitance of capacitor
will vary. The capacitance will be detected and measured by associated
electronics of transducer and will be transformed to relative humidity results.
The associated electronics will be consisting of section like signal
conditioning, analog-to-digital conversion, and digital interface circuitries which
are all integrated onto this sensor chip.
Communication Protocol "I2C" Interface between Microcontroller and sensor:
The output signal of the SHT75 will flow the serial
communication protocol called I2C. The I2C protocol requires only two lines for
data communication between two ends. The digital outputs will be read through a
two-wire (SDA for data and SCK for clock) serial interface by the
microcontroller.
Pull Up Resistors:
The external pull-up resistor will be required to pull the
signal high on the SDA line and SCK line. Usually the board of sensor comes
with pull-up resistors connected to both SDA and SCK lines. But sometimes the
module does not have the built-in pull up resistor, in that case we have to
apply these externally. So a careful look on circuitry of module will help us
to decide about the provision of pull up resistors.
Operating Range of SHT75:
The operating range of the sensors is 0 to 100% for relative humidity, and -40.0 to 123.8 °C for temperature. The results of SHT75 have an accuracy about 2.0% to #3.0% in measuring relative humidity depending upon the operating power supply voltages which are in the range of 2.4 to 5.5 V, the datasheet recommends to use 3.3V for highest accuracy. The measurement resolution for temperature is 14-bit and for humidity is 12-bit.
The Clock PIN of SENSOR SHT75:
The cock pin named SCK is the clock line that
is used to synchronize the communication between the microcontroller and the
sensor SHT75. The SCK line is an input only pin on the sensor’s side. The clock
will be generated by microcontroller. Thus, the microcontroller will be
responsible for the generation of the clock signal.
The DATA PIN of SENSOR SHT75:
The DATA or SDA pin of the sensor SHT75 is a bidirectional DATA transfer pin for sending data out of the sensor and receiving DATA from microcontroller. To start measurements the sensor SHT75 will receive a conversion command from the microcontroller. The Sensor will perform required operation for measurements the temperature and humidity and then results will be send out using the same data communication line.
Communication between Sensor and Microcontroller:
The
I2C Bus will be used for the necessary communication between sensor and
microcontroller. The communication will always be started by microcontroller
like to start measurements followed by acknowledgement from sensor. The
measured results will be provided by sensor to microcontroller al such thinks
will be happening in a specific handshake mechanism which is described as
under.
1. Start of Communication Signal from Microcontroller:
The
start of measurement communication is described as under using the format of
C-language, however it can be converted into any compiler specific syntax and format
of any other suitable programming language.
void
Start_Communication() {
// This a generic function to describe the
start of transmission or communication sequence between //microcontroller and
Sensor SHT75
SDA_Direction_Control_BIT = 1; // The data PIN will act as input pin
now
// As the Bidirectional IO PINS of PIC
Microcontroller can be control to read data only
// or to write data only mode
//
If it is set to “1” , it will be used to read data from out side like from
sensor,
SCL = 1; // The process will be
initiated with a high level on Clock Pin
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SDA_Direction_Control_BIT = 0; // SDA as output
SDA = 0; // Second step is to
bring the serial data pin “DATA” to low level logic
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SCL = 0; // Third step is down
the Clock to low level logic
Delay_us(1); // Some delay to give appropriate time to
sensor read input data like 1 us delay
SCL = 1; // Fourth Step is bring
the Clock pin to high level logic signal
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SDA_Direction_Control_BIT = 1; // Fifth step is to make data line
as to read data.
// SDA
as input,
Delay_us(1) // Some delay to give appropriate time to sensor read input data like 1 us delay
SCL = 0; // last step is to bring
the clock “SCL” to low level logic signal
}
2. Acknowledgement from Microcontroller:
During
the communication between sensor and microcontroller, there will be need to
send an ack signal from microcontroller to the sensor on receiving one byte
each time. This task wil be done using following generic C-language function.
void
ACKNOWLEDGEMENT_SIGNAL_FROM_MICROCONTROLLER() {
SDA_Direction_Control_BIT = 0; // make the Bi-directional DATA PIN “SDA”
as output
SDA = 0; // Write “0” to “DATA” PIN to
make it at logic low
SCL = 1; // Bring Clock to high Level
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SCL = 0; // down the Clock low
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SDA_Direction_Control_BIT = 1; // DATA high
}
3. Start of Measurement Command from Microcontroller to Sensor:
Now we will discuss, how the Microcontroller
will command the sensor SHT75 to start a measurement, how the sensor send an
acknowledgement, and how the Microcontroller will wait till the completion of
measurement process, and how the data will be communicated from sensor to
microcontroller using the data and serial clock. The process will envove the
following tasks:
(a)
Microcontroller will issue a “Start the Measurement” Command to Sensor
SHT75
(b) The
Microcontroller will wait till the measurement is completed
(c) The
Microcontroller will receive the two-byte measurement readings of Temperature
and Humidity.
(d) Mathematical manipulation of received results
with the help of datasheet to get the desired presentable output of Temperature
and humidity.
This is the function written in C-Language to
start the conversion or measurement process of physical parameter by the transducer
in the SHT75.This function will be called with one argument like 0X03 or 0X05.
The sensor will accept a conversion start command from the microcontroller for
the measurement of either temperature or relative humidity. Thus
microcontroller have to send separate command each time, weather sensor should measure
temperature or sensor should measure humidity.
Measurement Initiation Commands:
The measurement initiation commands send by
microcontroller to the SHT75 sensor for relative humidity 00000101 (05H) in hex
format 0X05 and to start measurement of temperature the measurement initiation command
is 00000011 (03H) in hex format 0X03. It may be noted that the
first three most significant bits of command byte are the address bits for the
device which will be considered zero for SHT75 sensors and the remaining 05
bits of the command byte are the command bits used for various functions.
Futhermore one should pay attention that before start of measurement the
microcontroller rhave to send start of communication in a specific pattern
which is already have neen described above.
Function written C-Language for acquiring data
of measured value by initiating start command:
long int Start_Measurements(short command) { //
accept a byte as what to start
Command_to_send
= command; // command Either
0x03 for Temperature
// or
for humidity 0x05
Serial_communication_Reset(); // Reset interface/ communication among the
sensor and MCU
Start_Communication(); // Call the subroutine to start the
communication between //sensor and Microcontroller
Received_Data_02_Byte
= 0; // initialization of a local variable
SDA_Direction_Control_BIT = 0; // Set Bidirectional data pin “SDA”
as output
SCL =
0; //Set the serial
clock “SCL” as low
for(i =
1; i <= 8; i++) { //The for loop
has been used to repeat a specific task 8 times
if (Command_to_send.7
== 1) // Check if the 7th
bit is high
SDA_Direction_Control_BIT
= 1; // Set Bidirectional data pin “SDA” as
input
else
{ // else if bit 7 =
0
SDA_Direction_Control_BIT
= 0; // Set Bidirectional data pin “SDA” as
output
SDA
= 0; // pull DATA line
low
}
Delay_us(1); // Some delay to give appropriate time to
sensor read input data like 1 us delay
SCL =
1; // Set the serial
clock “SCL” as high
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SCL =
0; // Set the serial
clock “SCL” as low
Command_to_send
<<= 1; // move
contents of j one place left
// This will be
continue until all 8-bits of command are sent
}
SDA_Direction_Control_BIT
= 1; // Set Bidirectional data pin “SDA”
as input
SCL =
1; // Set the serial
clock “SCL” as high
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
SCL =
0; // Set the serial
clock “SCL” as low
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
while
(SDA == 1) // WAIT while DATA is high, do nothing and continues
check it
Delay_us(1); // Some delay to give
appropriate time to sensor read input data like 1 us delay
//// when SDA goes down, the program will step
next
// Read 2 bytes of measurement data after it is
ready
for (i
= 1; i <=16; i++) { // repeat 16
times
Received_Data_02_Byte
<<= 1; // move
contents of k one place left
SCL =
1; // Set the serial
clock “SCL” as high
if
(SDA == 1) // if DATA is
high
Received_Data_02_Byte
= Received_Data_02_Byte | 0x0001;
// set
the corresponding bit of K to 1
SCL =
0;
if (i
== 8 ) !! (i == 16 ) //
Send an acknowledge after each byte
ACKNOWLEDGEMENT_SIGNAL_FROM_MICROCONTROLLER();
}
return Received_Data_02_Byte; // Return the measurement
data consisting //of two bytes
}
4. Reset communication function between the
microcontroller and the sensor:
Sometimes the response from the sensor is long awaiting
or communication link is lost, in that cases a rest of communication function
must be available to restore the communication channel. Here we will learn to
write such a RESET communication channel function for microcontroller and the
SHT75 sensor.
void Serial_communication_Reset () {
SCL =
0; // set the SCL low
SDA_Direction_Control_BIT
= 1; // Define SDA as input to pull the
DATA line high
for (i =
1; i <= 10; i++) // repeat 18 times
SCL =
~SCL; // invert the
serial clock pin “SCL”
}
How to measure the dew point with the help of a
sensor which is capable to measure humidity and temperature only?
With help of SHT75 sensors, we can only measure the Temperature and humidity. If we have to measure the dew point, then this sensor does not measure or provide the result of dew point directly. But the dew point can be calculated by using the results of the humidity and temperature measurements in a specific equation. The datasheet of this sensor SHT75 give us the required equation and coefficients to be used in the calculation of the dew point.
Complete Software for the sensor SHT75 and Microcontroller PIC 16f876A:
#include <16F876A.h> #fuses HS,NOWDT,PUT,NOPROTECT #use delay(clock=20000000) #define DHT_IO PIN_C0 #define RS PIN_B4 #define RW PIN_B6 #define EN PIN_B5 void lcd_initialization(); void lcd_send_cmd(unsigned char c); void lcd_send_data(unsigned char z); void disp_cmd(unsigned char cmd_value); void disp_data(unsigned char data_value); #include<SHT11.c> void main() { float Temperature, Humidity; setup_comparator(NC_NC_NC_NC); SETUP_ADC(ADC_OFF); SETUP_CCP1(CCP_OFF); lcd_ initialization(); Sensor_initialization(); disp_cmd(0x80); printf(disp_data, "Humidity"); delay_ms(500); while(1) { read_sensor (Temperature, Humidity); disp_cmd(0x80); printf(disp_data, "Temp:%3.1f %cC", Temperature, 223); disp_cmd(0xC0); printf(disp_data, "RH :%3.1f %% ", Humidity); delay_ms(500);
//delay 500 ms between reading to prevent self heating of sensor } } void lcd_ initialization() { disp_cmd(0x02);
// To initialize LCD in 4-bit mode. disp_cmd(0x28);
// To initialize LCD in 1 lines, 5x7 dots and 4bit mode. disp_cmd(0x0C); disp_cmd(0x01); disp_cmd(0x06); disp_cmd(0x80); } void lcd_send_cmd(unsigned char c) { output_c(c); output_low(RS); output_low(RW); output_high(EN); delay_ms(30); output_low(EN); } void lcd_send_data(unsigned char z) { output_c(z); output_high(RS); output_low(RW); output_high(EN); delay_ms(30); output_low(EN); } void disp_cmd(unsigned char cmd_value) { unsigned char cmd_value1; cmd_value1=(cmd_value & 0xF0); lcd_send_cmd(cmd_value1); // Send to LCD cmd_value1 = ((cmd_value<<4) & 0xF0); // Shift 4-bit and mask lcd_send_cmd(cmd_value1); // Send to LCD } void disp_data(unsigned char data_value) { unsigned char data_value1; data_value1=(data_value & 0xF0); lcd_send_data(data_value1); data_value1 = ((data_value<<4) & 0xF0); lcd_send_data(data_value1); } ///////////////////////////////////////////////////////////////////////////// // // // Driver file for SHT75 Temperature & Humidity Sensor // // // // ***** To initialize SHT75 sensor upon power up ***** // // // // Function : sht_initialization() // // Return : none // // // // // // ***** To measure and calculate SHT75 temp & real RH ***** // // // // Function : read_sensor (temp, Humidity) // // Return : temperature & true humidity in float values // // // //////////////////////////////////////////////////////////////////////////// #define sensor_data_pin PIN_C0 #define sensor_clk_pin PIN_C1 //***** Function to alert SHT75 ***** void Communication_start (void) { output_float(sensor_data_pin); //data high output_bit(sensor_clk_pin, 0); //clk low delay_us(1); output_bit(sensor_clk_pin, 1); //clk high delay_us(1); output_bit(sensor_data_pin, 0); //data low delay_us(1); output_bit(sensor_clk_pin, 0); //clk low delay_us(2); output_bit(sensor_clk_pin, 1); //clk high delay_us(1); output_float(sensor_data_pin); //data high delay_us(1); output_bit(sensor_clk_pin, 0); //clk low } //***** Function to write data to SHT75 ***** int1 Write_data_sensor (int8 Two_Bytes) { int8 i, mask = 0x80; int1 ack; //Shift out command delay_us(4); for(i=0; i<8; i++) { output_bit(sensor_clk_pin, 0); //clk low if((Two_Bytes & mask) > 0) output_float(sensor_data_pin);
//data high if MSB high else output_bit(sensor_data_pin, 0);
//data low if MSB low delay_us(1); output_bit(sensor_clk_pin, 1); //clk high delay_us(1); mask = mask >> 1; //shift to next bit } //Shift in ack output_bit(sensor_clk_pin, 0); //clk low delay_us(1); ack = input(sensor_data_pin); //get ack bit output_bit(sensor_clk_pin, 1); //clk high delay_us(1); output_bit(sensor_clk_pin, 0); //clk low return(ack); } //***** Function to read data from SHT75 ***** int16 Read_data_sensor (void) { int8 i; int16 Two_Bytes = 0; const int16 mask0 = 0x0000; const int16 mask1 = 0x0001; //shift in MSB data for(i=0; i<8; i++) { Two_Bytes = Two_Bytes << 1; output_bit(sensor_clk_pin, 1); //clk high delay_us(1); if (input(sensor_data_pin)) Two_Bytes |= mask1;
//shift in data bit else Two_Bytes |= mask0; output_bit(sensor_clk_pin, 0); //clk low delay_us(1); } //send ack 0 bit output_bit(sensor_data_pin, 0); //data low delay_us(1); output_bit(sensor_clk_pin, 1); //clk high delay_us(2); output_bit(sensor_clk_pin, 0); //clk low delay_us(1); output_float(sensor_data_pin); //data high //shift in LSB data for(i=0; i<8; i++) { Two_Bytes = Two_Bytes << 1; output_bit(sensor_clk_pin, 1); //clk high delay_us(1); if (input(sensor_data_pin)) Two_Bytes |= mask1;
//shift in data bit else Two_Bytes |= mask0; output_bit(sensor_clk_pin, 0); //clk low delay_us(1); } //send ack 1 bit output_float(sensor_data_pin); //data high delay_us(1); output_bit(sensor_clk_pin, 1); //clk high delay_us(2); output_bit(sensor_clk_pin, 0); //clk low return(Two_Bytes); } //***** Function to wait for SHT75 reading ***** void Wait_till_Conversion_done (void) { int16 sht_delay; output_float(sensor_data_pin); //data high output_bit(sensor_clk_pin, 0); //clk low delay_us(1); for(sht_delay=0; sht_delay<30000; sht_delay++)
// wait for max 300ms { if (!input(sensor_data_pin)) break;
//if sensor_data_pin low, SHT75 ready delay_us(10); } } //***** Function to reset SHT75 communication ***** void Communication_RESET (void) { int8 i; output_float(sensor_data_pin); //data high output_bit(sensor_clk_pin, 0); //clk low delay_us(2); for(i=0; i<9; i++) { output_bit(sensor_clk_pin, 1);//toggle clk 9 times delay_us(2); output_bit(sensor_clk_pin, 0); delay_us(2); } Communication_start(); } //***** Function to soft reset SHT75 ***** void Sensor_soft_reset (void) { Communication_RESET();
//SHT75 communication reset Write_data_sensor(0x1e);
//send SHT75 reset command delay_ms(15); //pause 15 ms } //***** Function to measure SHT75 temperature ***** int16 Measure_Temperature (void) { int1 ack; int16 Two_Bytes; Communication_start(); //alert SHT75 ack = Write_data_sensor(0x03);
//send measure temp command and read ack status if(ack == 1) return; Wait_till_Conversion_done();
//wait for SHT75 measurement to complete Two_Bytes = Read_data_sensor();
//read SHT75 temp data return(Two_Bytes); } //***** Function to measure SHT75 RH ***** int16 Measure_Humidity (void) { int1 ack; int16 Two_Bytes; Communication_start(); //alert SHT75 ack = Write_data_sensor(0x05);
//send measure RH command and read ack status if(ack == 1) return; Wait_till_Conversion_done();
//wait for SHT75 measurement to complete Two_Bytes = Read_data_sensor();
//read SHT75 temp data return(Two_Bytes); } //***** Function to calculate SHT75 temp & RH ***** void calculate_data (int16 temp, int16 humid,
float & Temperature, float & rhlin, float & Humidity) { float Humidity1, rh; //calculate temperature reading Temperature = ((float) temp * 0.01) - 40.0; //calculate Real RH reading rh = (float) humid; rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0; //calculate True RH reading Humidity = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin; } //***** Function to measure & calculate SHT75 temp & RH ***** void read_sensor (float & Temperature, float & Humidity) { int16 Sensor_Temperature, Sensor_humidity; float realhumid; Sensor_Temperature = 0; Sensor_humidity = 0; Sensor_humidity = Measure_Temperature(); //measure temp Sensor_humidity = Measure_Humidity(); //measure RH calculate_data (Sensor_humidity,
Sensor_humidity, Temperature, realhumid, Humidity);
//calculate temp & RH } //***** Function to initialise SHT75 on power-up ***** void Sensor_initialization (void) { Communication_RESET(); //reset SHT75 delay_ms(20); //delay for power-up }
No comments:
Post a Comment
Please ask if you have any question regarding the programming of MCU, or have any problem in development of your electronics project. microcontroller51.blogspot.com