More complete SHT-31 library

I’m working on a project that includes a couple of SHT-31 temperature/humidity sensors; they are a great, reasonably inexpensive sensor with ± 2% accuracy, 0.1% repeatability and 0.01 % resolution for humidity and ± 0.3 ℃ accuracy, 0.06 ℃ repeatability, and 0.015 0.3 ℃ resolution. There is also a more accurate version the SHT-35. What drew me to it was the fact that it has proper I2C and two user selectable addresses.

The already ported Adifruit library works well but while considering writing some additional functions to take advantage some of the extra features I came across a much more fully featured library written by these people out of the UK, For what it’s worth it’s also my favorite breakout for the sensor, the Adifruit one is nice, the ones I currently have are from China, and the orientation of the address pin is not ideal (see below)
to switch the address from 0x44 to 0x45 you need to jump the pin on the top right with the one on the bottom left.
In contrast, the ClosedCube one has jumpers
There is also a cutout around the sensor, I cannot find where Sensirion suggests this at the moment, but you can see on their dev board that they have included it.
Finally, ClosedCube has made it easy to add the filter cap if you need it.

I’ve “ported” the library over; I use quotes because all I needed to do was change the includes and add one or two type-casts from char * to uint_t* or something along those lines, I assume because of a small difference in how particle handles the Wire.H library.

There is a lot there, and I am going to go through the process of documenting it when I get the chance then share in the library manager, but I wanted to share it here first in case anyone with more experience writing documentation wanted to help. It’s pretty self-explanatory, but I figure documentation couldn’t hurt. I also will probably namespace the library to be safe.

For now, you can check it out at


Cool :slight_smile:

I have been using the SHT-31 sensors with true I2C for a year + now and they work great!

Recently Adafrut started selling the SHT-31D sensors with the dust cover over the sensor which is a good improvement in my opinion.

I looked over the functions in your library and what I really like is the code to setup the Alert interrupt pin to trigger based on Temp or Humidity thresholds and if they pass the threshold you program in. That allows your microcontroller to sleep and be woken up via the Alert pin which can save a lot of power.

I just started using this sensor to control 3 x 10w high-speed cooling fans inside a high power equipment case with variable fan speeds to keep the fans running slower at lower temps to keep the sound levels down.

I don’t really understand the benefit of the temp polling speeds and repetability settings but I assume it makes a difference in accuracy somehow?

Thanks for sharing! It is valuable to me at a minimum :tada:

@RWB I’m glad it will be of some help. I added a namespace to the library and added an example with two sensors to make sure it works with both. As for the various polling speeds and repeatability I haven’t had a lot of time to read through the datasheet, I think the polling speed refers to how often the chip itself refreshes its values from the onboard sensors.
I do notice when running Pooling Mode vs. ClockStrech Mode the numbers are slightly different. Curious to get my hands on an SHT-35 to see the difference. At some point,
I’m going to write up some documentation for the library which is not my favorite activity, so that might take a while.
Probably sometime over the weekend, I will figure out how to add it to the library manager. I got an error saying to reinstall the particle CLE when I tried which didn’t resolve the issue, so I took a break.

1 Like

@RWB From the datasheet
“The stated repeatability is 3 times the standard deviation (3σ) of multiple consecutive measurements at the stated repeatability and at constant ambient conditions. It
is a measure for the noise on the physical sensor output. Different measurement modes allow for high/medium/low repeatability.”



Makes sense but which repeatability method is most accurate? Which consumes the least amount of power?

Looks like High takes a reading every 15ms and may be more accurate? And Low takes a reading every 4ms and uses more power?

I wonder what the default settings are for these default libraries, I never looked but know the thing just works perfectly regardless :slight_smile:

Do you have your head wrapped around it?

Also, I wonder if any of these libraries use the heater? Or is the heater used automatically?

Such an advanced thermometer :smile:

As far as I can tell high repeatability is more accurate but uses more power. As best I can tell so far (I just finished reading page 9 of the datasheet carefully), you can either do single shot mode where the CMOSens chip will idle and start a measurement when it is asked, so when you initiate a measurement it will take 4-15ms before it can reply with the results depending on the repeatability setting. If clock stretching is on it will hold SCL down during this time to prevent other devices from using the bus in the meantime.
You can also set it to periodic data acquisition mode where the CMOSens chip will automatically update it’s data at a set frequency (I haven’t gotten to that part of the datasheet).
The frequency settings are because all the devices on the same bus need the same clock frequency.
It looks like the adifruit library defines all of the various constants and then uses 0x2400 which is single shot high repeatability without clock stretching it seems.
As for the heater I haven’t gotten to that part of the data sheet but I know for a different sensor I was working with a while ago it only came into play when you expressly set it to on, I don’t remember what it’s purpose is I think it has something to do with condensation.
For what it’s worth here is the sample code from the company that makes the actual sensors I have not had a chance to read through it.

1 Like

Looks like the heater is for plausibility checking only

I take that as just a way to check that the unit is working? Meaning turning on the heater should increase the temp by a few degrees C if it’s working? Is that how you take that?

Yeah that is how I read it. I’m going to try to write some basic documentation this evening and figure out how to upload it to the library manager.

@RWB from Si7020 datasheet, I would imagine you can use it the same way
5.5. Heater
The Si7020 contains an integrated resistive heating element that may be used to raise the temperature of the
sensor. This element can be used to test the sensor, to drive off condensation, or to implement dew-point
measurement when the Si7020 is used in conjunction with a separate temperature sensor such as another Si7020
(the heater will raise the temperature of the internal temperature sensor).

Good to know.

This is the first I have ever read about how the heater is actually used. I assumed it was automatically handled by the sensor but guess not.

What are you using the sensor for yourself?

I’m designing a replacement control board for a 170 count wine cooler that I’m using as a humidor. Using a Nextion display for a touchscreen interface, and controlling the various fans, lights, and compressor in the unit.

I see why you want a super accurate humidity sensor now :wink:

I much prefer the 4D Systems displays, go with one of those if you have not bought the Nextion display yet. So much easier to get the look you want, plus their new software has some pretty cool animation widget features that Nextion does not have.

Agreed the 4d systems display looks nicer, i had already ordered a nextion display when I found them, unfortunately. I’m pretty happy with what I’ve got set up on the nextion display but might try out a 4d systems display at some point.

To be honest, the more accurate sensors are not needed I use the similar humidity control media to the stuff that museums use to regulate humidity for storage so I don’t even really need to monitor it but the accuracy is part of the fun.
I started designing the board but will probably redesign at some point.

1 Like

You did a good job on the display layout in my opinion.

The PCB looks nice also. I’ve been laying out a new PCB for a project for the last 3 days myself. What PCB software are you working with? I’m using KiCad, I’m gonna guess your using Eagle?

Thank you, I think the layout looks pretty good, I really like the concept of handling the display logic in the display and communicating with it via serial, it takes the load off of the microcontroller, it also makes coding a lot simpler, I can handle the actual control logic on the photon and then setup callbacks to react to input from the display.
Yes I did use eagle, autodesk gave me 4 years of their premium software when I was a student and it’s nice that you can now export to a 3d model really easily. I have not really played around with any other PCB layout software but I hear KICad is nice.

Eagle’s 3D rendering is superior to Kicad’s for sure. But Kicad is Free and has a great online forum for help which is valuable.

So is this a project just for yourself or are you building and selling these humidors?

It’s just a personal project, I worked a while ago on a commercial product in the same space but had to put it on hold for the time being as my visa situation changed and would have been too complicated to continue without moving back to Canada, at some point I will probably get back to it. I picked up this large wine cooler for this project for really cheap because it wasn’t working, I have since fixed it but thought it would be fun to replace the control board with something i designed myself and had all of the functionality that I wanted built in. I might adapt it for a smaller unit that is commonly used as a humidor at some point if I get the time.
What are you currently working on

I see, yes it’s fun to keep trying and learning with these electronics projects.

I’m working on portable solar power systems that are Wifi and Cellular connected.

Keep up the good work.

Very nice, I started working on a wiki for documenting the library.