Skip to content

SHT75 Humidity and Temperature Sensor with PIC18F uC

SHT75 from Sensiron is a fully calibrated (calibration constants are programmed individually to each sensor) digital output sensor. It has a I2C like (not compatible with I2C) interface. The sensors are really accurate. I have noticed three sensors close to each other giving an output only deviating by 0.1C of each other.

The attached source code is derived from an application note provided by Sensiron for interfacing SHT7X sensors to 8051 microcontrollers. Please note this code has temperature/humidity conversion coefficients for older (prior to V4) SHT75 sensors. If you use a newer V4 sensor update the conversion coefficients. The code was written for a PIC18F4580 IC and was compiled with HI-TECH PIC18 compiler.

/*
Simple code to interface SHT75 sensor to PIC18 microcontrollers
To be compiled with HI-TECH PIC18 Compilers
Freely distributable in this original form.
(c) 2009 Pasan Hettiarachchi
(c) Microsolutions
Any questions or comments?  admin@microsolutions.info
*/

#include <pic18.h>
#include "always.h"
#include"delay.h"
#include"serial.h"
#include "SHT75.h"
#include<stdio.h>

__CONFIG (1,XT );
__CONFIG (2,WDTDIS);
__CONFIG (4,LVPDIS & STVRDIS);

void main(){
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [°C]
// 4. calculate dew point [°C]
// 5. print temperature, humidity, dew point
float dew_point;
unsigned char error,checksum;
//unsigned int i;
value humi_val,temp_val;

ADCON1=0x06;
CMCON=0x07;

TRISD=0b11111110; //RD0 is CLK and RD1 is DATA of the SENSIBUS
//Pin definitions are also given in SHT75.h

init_comms();
s_connectionreset();

while(1)
{ error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //measure temperature
if(error!=0) s_connectionreset();                 //in case of an error: connection reset
else
{ humi_val.f=(float)humi_val.i;                   //converts integer to float
temp_val.f=(float)temp_val.i;                   //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
dew_point=calc_dewpoint(humi_val.f,temp_val.f);
//putch('A');
//putch('\n'); //calculate dew point
printf("temp:%5.2fC humi:%5.2f dew point:%5.2f\n",temp_val.f,humi_val.f,dew_point);

}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
DelayS(2);     //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}

}

Download Complete Project: SHT75-PIC18F4580

8 thoughts on “SHT75 Humidity and Temperature Sensor with PIC18F uC

  1. vanni22 says:

    I am writing code for the same sensor. Although I am using a HC12 micro controller. I have a few questions for you if you wouldn’t mind offering your assistance. please feel free to contact me through email and I will send you my questions. Thanks in advance.

    Reply
    • lihiniya says:

      @vanni22
      Did you check the full source code attached to the post above? Perhaps answer to your question is there :)

      Reply
  2. Tope says:

    Please I am trying to adap this code to pic18F14K50 but I am having problem compiling the code. I have my SHT75 data pin on pin RC1 and SCK on Pin RC0 I am using 1 12MHz Cristal oscilator connected to pin RA4 and RA5 and my serial connection to PIN RA0 and RA1. I am using Hi-Tech Microchip MPLAB XC8 compiler Please can you help me. Because I have been troubleshouting the error since but it just not working

    Reply
    • lihiniya says:

      Since I don’t have access to any development tool, I cannot help much. But some steps in porting.

      1. Start with skeleton code which just output something as the SHT75 code. Make sure your primary output method (serial terminal/LCD/SSD) works.

      2. Don’t use any delay/serial/clock related routines attached above. Use the routines provided by compiler.

      3. Make sure the pins used in SHT75 codes are set to digital-IO (not assigned to any peripheral). Two LEDs/switches connected to pins can be used to verify this.

      4. Make sure you use the pull-up resistor in SHT75 data-pin.

      5. Now insert the ported SHT75 code to your project with as many as debug codes required (you should identify where the code fail). When compiling turn-off compiler optimization first. May be it’s messing with bit-bang routines in the code. (s_read_byte, s_write_byte)

      6.If you have an oscilloscope you can see the actual timing for communication and would help you very much in debugging.

      Reply
  3. Chacalito says:

    Hi! I’m developing a class for the SHT75 sensor for mbed. Source code is already given, but they are too complicated. I want to develop a simple.
    Could you please show the .h and .cpp of the class?
    Cordially.

    Reply
  4. Jega says:

    can we interface sht 75 sensor with 8051 microcontroller in edsim51

    Reply

Leave a Reply to Jega Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.