Raspberry Pi - Temperature Sensors

Posted on Thu 04 April 2019 in Home Automation,

I started writing a post:databases-ugh about my recent ventures with databases, went to link to one of them and realized I hadn't actually written anything down. So here goes.

I use temperature sensors with the RaspberryPi, coupled with a 433MHz transmitter to switch devices on and off dependent on room humidity or temperature.

I've been through a few different sensors so thought I'd post my findings and workings.

![aMess] (images/sensorsRPi.png "RPi Sensors")

From left to right, DS18b20, DHT22, BME280. Bottom, a mess of cables.

DHT11 / DHT22

Both are nice sensors but completely useless in humid environments. I found when the room was >80% humidity for too long (the desired in my case) the sensor would get stuck high and not come back down. Took dry rice and a radiator to temporarily fix. If they get too humid, they just don’t work. I now only use them for temperature and low humidity environments.

The DHT22 is slightly more accurate and faster at reading than the DHT11, it's slightly more expensive too.

They have supporting libraries by Adafruit that are very easy to work with and install.

I have been using the below to install the libraries.

sudo git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get install build-essential python-dev
sudo python setup.py install
cd ~/Adafruit_Python_DHT/examples

Look for the AdafruitDHT.py script.

sudo ./AdafruitDHT 22 17

![DHT22] (images/tempdht11.png "DHT22")

Replace 22 with 11 if using a DHT11, replace 17 with the GPIO number you are using.

BME280

The BME280 is my favorite so far. It just works. It's reliable, accurate and fast. ATOW the BME280 will cost you £3 more than a DHT22. Unless you need a DHT11/22, go for it.

Although it works slightly differently than the DHT22 its just as easy to install and use. Thanks Adafruit.

sudo git clone https://github.com/adafruit/Adafruit_Python_BME280.git 
cd Adafruit_Python_BME
sudo apt-get install build-essential python-dev
sudo python setup.py install
cd ~/Adafruit_Python_BME280/

Look for the Adafruit_BME280_Example.py

sudo python Adafruit_BME280_Example.py

![BME280] (images/temp280.png "BME280")

DS18b20

1-Wire. Completely different sensor than the first two. I like it because it comes in a waterproof version attached to long cables which is perfect for my needs. You can also daisy chain sensors off, one wire. Each sensor has its own unique ID address that can be called individually off the same GPIO pin, which is handy.

I've found the key to getting these sensors to work properly lies in the wiring. You need to have a 4.7k ohm resistor, otherwise you'll get dodgy readings. Terminations need to be strong and tight. No hairlines.

You don’t need to install anything on the Pi to get this working but you do need to enable 1 wire.

sudo nano /boot/config.txt

Drop the below line at the bottom of that file.

dtoverlay=w1gpio

Reboot the Pi!

sudo reboot

Log back in and run the below command.

sudo modprobe w1-therm

Quickly check that your 1-Wire device has been detected. It should have an I2C address. Run the below.

sudo i2cdetect -y 1

![I2C] (images/i2crpi.png "I2C detect)

You can see mine listed as 76 - make a mental note of yours

Now CD in to the below directory.

CD  /sys/bus/w1/devices

List all attached 1-Wire devices using LS, CD in to the device folder and probe the device for a reading.

CD 28(your ID here) && cat w1_slave

You can see the current temperature is 23.5c

![DS18b20] (images/ds18b20.png "DS18b20)

Overall, dependent on application I'd start off with the BME280's. They're the most reliable and very easy to work with. The DS18b20's provide multiple sensors on one cable run, which is very handy. Sadly, despite now owning a small box of them, the DHT11/22's aren’t good for humid environments. They work perfectly as sensors and are easy to use - just not for me.