SHT20 Temperature Humidity With Reef-PI

attiland

Well-Known Member
View Badges
Joined
Jul 22, 2020
Messages
531
Reaction score
359
Location
United Kingdom
Hi all,

I have been working on a little bit with humidity and Room temperature sensing with Reef-PI So and I want to share what I have so fare.
One of the key point with my selection was that this has to be cheap while strait forward to set up. The SHT20 is a logical choice as the price is about £2.5-3 with casing, works with DC 3.3 ~ 5.5V and communicates on I2C which makes easy to implement.
SHT20.png


So Let's see how to add it to PI and than connect to Reef-PI software.

As of 29/03/2021 Reef OI has no Humidity Tab, only supports 1 Wire temperature sensors so we have to use the PH logic and File input to set it up. Not a biggie but not an elegant solution.

For file input I like to use virtual drives in the memory as it is fast, we don't need the files to be stored permanently and it saves your SD card and my experience the CPU usage goes down doing this.

So let's see the steps:
1. Wire up your device as per the above picture. (I won't give you pin numbers for SDA and SCL because you may have changed your configuration of pin-out. )
2. If not enabled i2C ye do it now. (I use the GUI you can do it in the hardcore way via command line too ;) )
I2CEnable.PNG

3. Install necessary packages such as I2C-Tools ( note: You may have some of these packages already installed. If you are not sure just run all it won't hurt)
Execute the following four commands respectively to install and restart.
[In terminal]

sudo apt-get install i2c-tools
sudo apt-get install python-smbus
sudo adduser pi i2c
sudo reboot


4. Test I2C and confirm the sensor address
[In terminal]
i2cdetect -y 1
To confirm whether the I2C link sensor is successful (0x40 in the picture is the address of my SHT20, everyone's sensor address may not be the same, this should be noted)
1617050115951.png


5. Create the directories you will store your code (You can change it but if you do fallow it trough in all the codes)
[In terminal]


sudo mkdir /Reef-Hardware
sudo mkdir /Reef-Hardware/SHT20
sudo chown pi -R /Reef-Hardware/
sudo chgrp pi -R /Reef-Hardware/
chmod 755 -R /Reef-Hardware/


6. Write Python temperature and humidity collection program ( Mine is a modified version of one I have found on-line )
Note: the code below is designed to be able to used manual testing also to be added to a scheduler (crontab)
Note 2: The script is writing temperature in C if you want to record in F you will have to change the line strSHT = repr(cTemp) to strSHT = repr(fTemp)

[In terminal]
nano /Reef-Hardware/SHT20/SHT20_Reader.py


import os
import smbus
import time
#Create directories and define output files
rootDir = "/dev/shm"
WorkDrir = "/ReeFHardwareComs/SHT20"
path = rootDir + WorkDrir
access_rights = 0o777
F_Hum_Name = "SHT20_Hum"
F_Temp_name = "SHT20_Temp"

if not os.path.isdir(path):
try:
os.makedirs(path, access_rights)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)

#Get I2C bus
bus = smbus.SMBus(1)
#SHT20 address,0x40(64)
addr =0x40
#Send Temperature measurement command
# 0xF3(243) NO HOLD master
bus.write_byte(addr,0xF3)
time.sleep(0.5)
#Read data back, 2byte
#Temp MSB, Temp LSB
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
#convert the data
temp =data0 *256 +data1
cTemp = -46.85 +((temp*175.72)/65536.0)
ftemp =cTemp*1.8+32
#Send humidity measurement command
bus.write_byte(0x40,0xF5)
time.sleep(0.5)
data0 = bus.read_byte(0x40)
data1 = bus.read_byte(0x40)
humidity=data0 *256 +data1
humidity=-6+((humidity*125.0)/65536.0)
print("Humidity is : ",'%.2f'% humidity,"%")
print("Temperature in C is: "'%.2f'%cTemp,"C")

F_Temp = open(path + "/" + F_Temp_name, "w")
strSHT = repr(cTemp)
F_Temp.write(strSHT)
F_Temp.close()
os.chmod(path + "/" + F_Temp_name, access_rights)

F_Hum = open(path + "/" +F_Hum_Name, "w")
print (F_Hum)
strSHT = repr(humidity)
F_Hum.write(strSHT)
F_Hum.close()
os.chmod(path + "/" + F_Hum_Name, access_rights)



7. run your code to test
[In terminal]
sudo python /Reef-Hardware/SHT20/SHT20_Reader.py

Output should be something like this :
('Humidity is : ', '51.04', '%')
('Temperature in C is: 23.57', 'C')

8. Add the job to crontab (scheduler) so it runs every 30 sec
Note crontab can't run jobs only every minute but the sleep allows you to delay the start
[In terminal]

sudo crontab -e

Add the below 2 lines at the end

* * * * * python /Reef-Hardware/SHT20/SHT20_Reader.py
* * * * * ( sleep 30 ; python /Reef-Hardware/SHT20/SHT20_Reader.py )

9. Add the sensor to Reef PI
- add the file-analog divers first (The script above creates 2 files you will use)

/dev/shm/ReeFHardwareComs/SHT20/SHT20_Hum
/dev/shm/ReeFHardwareComs/SHT20/SHT20_Temp
ReefPIDrivers.PNG


Now add the sensors as PH sensors like below:
ReefPIPH.PNG


10. Adjust your dashboard to show your charts

And you are done.



Product parameters:

Resolution: 12Bit
Repeatability: ±0.1%RH
Accuracy: 25 ° C ± 3% RH
Interchangeability: fully interchangeable
Response time: 1/e (63%) 25 ° C 8s
1m/s air 8s
Hysteresis: <±0.1%RH
Long-term stability: <±0.25% RH/yr

Temperature:

Resolution: 14Bit
Repeatability: ±0.1 °C
Accuracy: 25 ° C ± 0.3 ° C
Response time: 1/e (63%) 10S

Electrical characteristics:

Power supply:

Supply current: measuring 0.3mA standby 60μA
Sampling period: times greater than 2 seconds

Pin Description:

1, VDD power supply 3.3 ~ 5.5V DC
2, SDA bidirectional data line
3, GND ground, negative power supply
4, SCL clock line
 
Avast

robsworld78

Active Member
View Badges
Joined
Feb 14, 2020
Messages
406
Reaction score
635
Location
Edmonton, Canada
Looks good. Not long ago a driver was added for the SHT31 humidity sensor that also works on I2C. Take a look at the DHT22, it doesn't use I2C so much better, I2C and cable don't get along so best to avoid when possible plus you can use the DHT22 on any pin.
 
OP
attiland

attiland

Well-Known Member
View Badges
Joined
Jul 22, 2020
Messages
531
Reaction score
359
Location
United Kingdom
Looks good. Not long ago a driver was added for the SHT31 humidity sensor that also works on I2C. Take a look at the DHT22, it doesn't use I2C so much better, I2C and cable don't get along so best to avoid when possible plus you can use the DHT22 on any pin.
I will do have a look at DHT 22.
Yes i2c has it’s limitations. My plan is to stick it just outside of the box so have a better idea of the environment of where reef-pi operates
 

robsworld78

Active Member
View Badges
Joined
Feb 14, 2020
Messages
406
Reaction score
635
Location
Edmonton, Canada
Yeah I2C is great but sucks at the same time. I2C isn't supposed to run through cable yet they make all these sensors, just setting people up for trouble. I always recommend cable stay under 1ft although it's usually ok with a little more.
 

GaryE

Well-Known Member
View Badges
Joined
Mar 12, 2020
Messages
784
Reaction score
1,104
Location
Coatesville, Indiana
I just used your script and parts of one from the Adafruit website for the SHT31d and cobbled together for that sensor.

There is a direct SHT32d driver, however, there is no temp conversion and that irks me. I am not a Celsius guy. I know fahrenheit and I'm too danged old to convert now.. ;)

Works great!

my script:

#!/usr/bin/python

import os
import time
import board
import busio
import adafruit_sht31d

# Create directories and define output files

rootDir = '/dev/shm'
WorkDrir = '/ReeFHardwareComs/SHT31'
path = rootDir + WorkDrir
access_rights = 0o777
F_Hum_Name = 'SHT31_Hum'
F_Temp_name = 'SHT31_Temp'

if not os.path.isdir(path):
try:
os.makedirs(path, access_rights)
except OSError:
print ('Creation of the directory %s failed' % path)
else:
print ('Successfully created the directory %s ' % path)

# Create library object using our Bus I2C port

i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_sht31d.SHT31D(i2c)

temp = sensor.temperature * 1.8 + 32
sensor.heater = True
time.sleep(1)
sensor.heater = False
time.sleep(10)

print('\nTemperature: %0.1f F' % temp)
print('Humidity: %0.1f %%' % sensor.relative_humidity)

F_Temp = open(path + '/' + F_Temp_name, 'w')
strSHT = repr(temp)
F_Temp.write(strSHT)
F_Temp.close()
os.chmod(path + '/' + F_Temp_name, access_rights)

F_Hum = open(path + '/' + F_Hum_Name, 'w')
print (F_Hum)
strSHT = repr(sensor.relative_humidity)
F_Hum.write(strSHT)
F_Hum.close()
os.chmod(path + '/' + F_Hum_Name, access_rights)
 
SWEET 17 Live Sale!

BIG TANKS VERSUS SMALLER TANKS...WHICH DO YOU PREFER?

  • BIGGER

    Votes: 627 75.8%
  • smaller

    Votes: 156 18.9%
  • Other (please explain)

    Votes: 44 5.3%

Online statistics

Members online
639
Guests online
3,228
Total visitors
3,867

New Posts

tankmatez-bubble-traps-ad
Top