Introduction: Easy Temperature and Humidity on Raspberry Pi
Air temperature and relative humidity are very common parameters that need to be measured. Raspberry Pi doesn't come with analog capabilities, and reading sensors isn't exactly easy.
On this instructable we're using the EzTemp sensor.
With EzTemp you get a professional relative humidity sensor by Honeywell and an accurate thermistor that you can read using your serial port on your Raspberry Pi.
Let's see how to do it!
Step 1: Connection
EzTemp uses the serial port on your Raspberry Pi to communicate.
Identify the serial pins on your Raspberry Pi. Then connect:
- RPi GND to EzTemp GND.
- RPi Tx to EzTemp Rx.
- RPi Rx to EzTemp Tx.
- RPi 3.3V to EzTemp 3.3V.
Notice that a Tx pin must be connected to a Rx pin, and vice-versa.
Now you are ready to use the EzTemp!
Step 2: Disable the Serial Console
By default, the Raspberry Pi uses the serial port for the shell. As noticed by MarcR on the comments (thanks for the tip), we need to disable the serial console to use the EzTemp.
Method 1: raspi-config script
The easiest way to disable the serial console is by running the raspi-config script. Open a shell and type:
It will show a menu, just go to 8 Advanced Options > A7 Serialand select No.
Now reboot the Pi and go to the next step.
Method 2: Automatic script
We can disable and enable the serial console using the rpi-serial-console script. To install it, open the shell and type:
sudo wget https://raw.githubusercontent.com/lurch/rpi-serial-console/master/rpi-serial-console -O /usr/bin/rpi-serial-console && sudo chmod +x /usr/bin/rpi-serial-console
To display whether the serial console is currently enabled or not, simply run:
To disable the serial console, simply run:
sudo rpi-serial-console disable
To enable the serial console, simply run:
sudo rpi-serial-console enable
After enabling or disabling the serial console you'll need to reboot the Pi for it to take effect, and go to the next step.
Method 3: Manual
Then open this file with your favorite editor:
sudo nano /boot/cmdline.txt
Its content should be something like this:
dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Remove any reference to ttyAMA0, getting something like this:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
Save changes, and open inittab:
sudo nano /etc/inittab
At the end of the file, comment out this line:
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Reboot your Pi and go to the next step.
Step 3: Download the Example Script
On your Raspberry Pi, you must download this script. You can do it opening the shell and typing:
wget <a href="https://raw.githubusercontent.com/CAIMANICS/EzTempRH-for-Raspberry-Pi/master/software/EzTempRH.py" rel="nofollow">https://raw.githubusercontent.com/CAIMANICS/EzTempRH-for-Raspberry-Pi/master/software/EzTempRH.py</a>
Now run the script typing:
And that's it! you have your EzTemp running.
EzTemp&RH build 2 Temperature: 24.0C Humidity: 48.0% External: 1018 / 2048 counts Version: 2
Let's see how to use it in your programs.
Step 4: Tinker
Here we'll analyze that each part of the script does, so you can understand it, and modify it for your projects.
First of all, we need to open the serial port. The script uses PySerial module to do this.
import serial ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
Now, let's query the sensor for the build value. It is a static value that identifies the version of the EzTemp.
First, we need to send the command 'v' to the EzTemp:
The build value is a 2-byte value, so we should expect a 4-byte (1-byte echo + 2-byte build + 1-byte checksum) response from the EzTemp. Now read the response:
s = ser.read(4)
Data is received in separate bytes, although the build identifier is a 2-byte value. To "reconstruct" it, we must shift the most significant byte 8 bits to the left, and then add the least significant byte. This way we recover the 2-byte field correctly:
version = (ord(s) << 8) + ord(s)
You can use the sentence above everytime you receive a 2-byte value from EzTemp.
The last line of this block simply prints the result on screen:
print 'EzTemp&RH build ' + str(version)
The rest of the script does the same querying sensor values from the EzTemp. Let's see the temperature value as an example:
ser.write("d") s = ser.read(4) temperature = (ord(s) << 8) + ord(s)
print 'Temperature: ' + str(1.0*temperature/10) + 'C'
In this case, we use the command 'd' to query the temperature in celsius degrees with 0.1ºC precision. As you can see, the capture of the response is quite similar. The difference is on how to handle the returned value.
When we query temperature or any other parameter with 0.1 precision, the data is received as a 2-byte integer in tenths of the value. This is, if temperature is 21.5ºC, the EzTemp receives the value 215, and the user script must divide by 10 to get the actual temperature.
All values have an integer precision command if you don't require decimals. Using these commands easens reading and processing of the returned data. Check the datasheet for the complete list of commands.
Step 5: Useful Resources
You can find more information on the EzTemp on GitHub:
Especially, the latest version of its datasheet:
Want an EzTemp?
You can buy one on Tindie:
or our local makerspace, MakeLab Fábrica Digital (in Spanish):