Measuring Temperature and Humidity with Sensors
New England has cold winters. The temperatures stay below freezing most of the late fall through early spring, and the humidity is very dry. During the summers, the weather can easily exceed 90F (~32C) with dew points above 60F (~16C). With these wide ranges between the seasons, it can be a struggle to maintain optimal temperature and humidity levels most of the year.
When the temperature and humidity are at optimal levels, it’s not only more comfortable but it’s better for one’s health. The ideal range for humidity is between 35% and 50%. However, humidity will vary depending on the outdoor temperature.
With all these variables and conditions, it can be hard to know what’s right depending on the current weather. Thankfully, the magic of technology can be used to determine the optimal humidity levels.
Since the beginning of this endeavor, my plan includes monitoring key systems throughout the tiny house to improve the quality of living for the occupants. The first step is to build several sensors to gather the precious data and then add software intelligences to detect and automate.
At the moment there are a few sensors in the tiny house. The webcam detects motion and keeps an eye on the inside, the neurio monitors how much electricity is in use, and the magic mirror shows the house’s activities. There are plans for more sensors that will do more things. However, in order to help maintain an optimal living space, one must first collect and measure data.
Four Temperature and Humidity Sensors
It might not be necessary to have four sensors. However, four is a good start to measure the temperature and humidity from different parts of the tiny house. These sensors are in the bedroom loft, great room, bathroom, and under the kitchen sink.
The Great Room
Currently, there is one sensor in the great room. The webcam is watching from above. Instead of building a separate temperature and humidity sensor, there is available space on the webcam’s Raspberry Pi Zero W to connect a sensor.
While adding the sensor, I replaced the original casing of the Raspberry Pi. One reason is to attach the sensor where the heat of the unit wouldn’t influence the readings. Secondly, the original casing did not attach to the wall very well. Therefore, I replaced the case with a wedge of wood that will keep the camera pointed at the correct angle.
The bathroom sensor not only will monitor the temperature, but more importantly the humidity. The temperature and humidity sensor is connected to the Raspberry Pi inside the magic mirror.
The magic mirror has plenty of space to add sensors. Therefore, instead of adding a separate Raspberry Pi, utilizing the existing one makes it much easier to consolidate the bathroom sensors. There are plans to add other sensors to this unit, but that is a topic for another time.
The Bedroom Loft
The loft is a sensor all by itself at the moment. Depending on how my testing goes, it may not be necessary to have a permanent sensor here. This depends on how much the great room sensor and the loft sensor compare in their readings over time. Therefore, this sensor is portable. All one has to do is simply plug it in to an outlet and it begins to collect data and upload it to the cloud.
The loft sensor uses a Raspberry Pi Zero W since there is likely only a single sensor component attached. If the loft ends up having other sensors, this may be upgraded to a Raspberry Pi 3 to have the necessary resources to collect the metrics.
Under the Sink
The main components for the plumbing are located under the sink. The temperature and humidity sensor is the first of a few sensors that will be connected to this unit. Therefore, I am using a Raspberry Pi 3 for the extra horsepower to make sure there are enough resources available when all the sensors are active.
This sensor has a case to protect it from splashing water. I mounted the case and sensor onto a piece of wood to keep it organized. A single screw secures it near the water pump about 12″ (~30 cm) from the floor.
It seems like a lot of work for little gain. Why not just use a humidifier with a built in humidity sensor that turns on and off as necessary? Why have temperature sensors when the HVAC can turn on and off as needed?
Those appliances work great for one particular purpose. The heat hump and humidifier react to a single environmental condition. However, smart homes need data from several sources for a period of time in order to analyze data in different ways to react appropriately.
For example, the HVAC will turn on and off to maintain a room temperature of 70F (~21C). What happens when the conditions outside are too cold for a heat pump? It begins to lose its ability to produce heat. Therefore, it will continuously run until it heats the room to 70F (~21C) which it will never achieve until the outside temperature increases. However, if a temperature sensor detects the temperature drops to 60F (~16C), it could trigger an alert that turns on a secondary heat source. Then, a separate alert can turn off the secondary heat source once the temperature returns to normal.
This is the basic premise of how I will use automation to make this tiny house a smart home. By collecting data from different kinds of sensors, algorithms can take in the information and perform actions when one or more conditions are met.
How to Build a Temperature and Humidity Sensor
Building a temperature and humidity sensor isn’t difficult at all. It really is a few components and free software that’s available on the web. All it takes is some basic computer skills and a little time to have one of these sensors running and actively collecting data.
Note: These steps are written for beginners. For those that are more advanced, some of the steps and explanations may seem basic. However, in my experience I’ve found nuggets of wisdom in basic instructions.
There isn’t that much hardware needed to make a sensor. Overall, the cost ranges from $30 to $90 depending on the components. Regardless, the following gear is required to put together a sensor:
- USB Keyboard
- HDMI Monitor
- Internet Access
A single temperature and humidity sensor requires the following:
- A Raspberry Pi 3 Starter Kit or a Raspberry Pi Zero W Starter Kit
- An AM2302 Temperature and Humidity Sensor
- (Optional for Raspberry Pi Zero W) A GPIO Hammerhead Solderless kit for those that don’t like to Solder
Note: I am using a Raspberry Pi 3 when I intend to add more than two sensors. Although a Raspberry Pi Zero W will work 99% of the time, the software I am writing may require extra horsepower for multiple sensors.
Connecting the Sensor
The sensor connects to the Raspberry Pi’s GPIO. Thankfully, the pins used with the Raspberry Pi 3 and Raspberry Pi Zero W are the same. However, the colors of the wires that come with the sensor are not always the same. This isn’t an issue, it just means to pay attention.
When looking directly at the Raspberry Pi unit (shown above), the left most pin on the bottom row is pin #1. The bottom row has the odd number pins, and the upper row has the even number pins.
The sensor has three wires. In this example blue connects to the power positive input (+), green connects to the data pin (out), and yellow connects to the negative power input (-). Next, connect the blue wire to pin #2 (5v power), connect the yellow wire to pin #6 (ground), and connect the green wire to pin #7 (GPIO 4) on the Raspberry Pi.
Note: Software changes over time. New features and bug fixes are welcome changes. Unfortunately, errors may occur with newer versions. It may be necessary to investigate errors. However at the time of this writing, everything installed properly on my four temperature and humidity sensors using these steps.
Installing the Operating System
Some Raspberry Pi units come preloaded with Raspbian on the microSD card. If so, then there is no need to flash it. However, re-flashing the microSD card removes any non-essential software packages and maximizes the amount of free space.
Whenever I create IoT devices or sensors, I always use the lite version. The lite version loads the bare minimum software components. This means more storage space, more memory, and more horsepower for my software. Although, I’ll use the full version when the Raspberry Pi will be used as a desktop, or the project specifically requires it.
Important: Flashing a microSD card erases it completely. Therefore, make sure there isn’t anything important worth saving before proceeding.
Flashing the microSD card is not difficult for those that haven’t done it before. Personally, I use scripts for Ubuntu and MacOS X to flash microSD cards. For Microsoft Windows Users, these instructions work well.
sudo ./pi_flash_darwin.sh ./2018-11-13-raspbian-stretch-lite.img
MicroSD Flash Utility
PASS: Image File is ./2018-11-13-raspbian-stretch-lite.img
This script assumes the microSD card is mounted to /dev/disk2
If this is the correct mount point, select Y to continue. Otherwise,
select N and update the script to use the appropriate mount point.
Continue (y/n)? y
Starting the imaging process ..
Unmount of all volumes on disk2 was successful
1780+0 records in
1780+0 records out
1866465280 bytes transferred in 86.509493 secs (21575266 bytes/sec)
Disk /dev/disk2 ejected
Done! MicroSD imaging complete
Now, the microSD card is ready to use. Next, install it in the Raspberry Pi, connect the keyboard, monitor, and power. Then, turn it on.
Configuring the Operating System
If the full desktop version of Raspbian is installed, many of these steps are configured through a series of questions when logging in for the first time. Otherwise, the lite version uses a tool called raspi-config to help set the initial configuration settings. This utility simplifies configuring the operating system without having to edit system files.
Regardless of which version of the Raspbian operating system is installed, the default username is pi and the password is raspberry. Quite original from the Raspbian team!
After logging in, the next step is to configure network parameters, localization, and password. To begin, run the following command to start the utility with the correct system permissions:
The raspi-config utility menu is quite easy to navigate and very intuitive. Although there are many options, I typically use raspi-config to set the following:
- First, use option 1 to change the password
- Next, select option 2 to define a hostname and configure Wi-Fi settings
- Then, select option 4 to set locale, update the timezone, and set Wi-Fi country
- Lastly, select option 5 to enable SSH remotely
Note: Although enabling SSH remotely is recommended, it isn’t required if there will always be a keyboard and monitor connected to the sensor. However, most sensors are tucked away. Therefore, the only way to access it is from another device remotely.
Next, reboot the raspberry pi and log in again with the user name pi and the new password.
Update Software Components
Out of date software means potential bugs and security vulnerabilities. Therefore, it’s best to make sure everything is up to date. The apt utility easily manages this task by running each of these commands.
sudo apt update
sudo apt upgrade
sudo apt autoremove
The first command checks for outdated packages. If there are updates available, then the second command upgrades them. Optionally, running the third command removes packages no longer in use, which will recover precious storage space. Lastly, reboot to verify that everything starts correctly.
Loading the Temperature and Humidity Sensor Libraries
The sensor requires libraries to read the temperature and humidity data. There are several libraries and examples available to collect metrics using different programming languages. In these steps, the sensor uses a python script to read the sensor’s data.
Python is quite popular. Unfortunately, there are two major versions supported even though Python 3 is the future of the language. Python 2.7’s end-of-life was set in 2015. Then, it was postponed to 2020 because a large amount of existing code could not easily be forward-ported to Python 3. This means there is potentially a lot of work when porting python 2 scripts to python 3. Regardless, python 2 and 3 are both actively in use by a large and growing community.
Since both versions are available, I’ve provided shell scripts to install the necessary libraries for both python 2 and python 3. Regardless of which one you choose, don’t forget to run them with sudo. Sudo insures the proper system level permissions to configure everything properly.
Note: If an error occurs, the script may not have execute permissions. To set this permission, type chmod 755 install_dependencies_python*.sh and press enter. Then, run the above command again.
Measuring the Temperature and Humidity
Now that everything is in order, it’s time to start collecting data. Gathering the data is straightforward. Although, there are countless ways to do something useful with it. First, let’s read the data by downloading this python script to the sensor. Then, execute the following:
./dht_sensor.py 2302 4 F
Note: If an error occurs, the script may not have execute permissions. To set this permission, type chmod 755 dht_sensor.py and press enter. Then, run the above command again.
The first parameter specifies the type of sensor. The supported options are 11, 22, and 2302 which represent the DHT11, DHT22, and AM2302 sensors respectively. The next parameter specify which GPIO to use when reading the data. Based on the above instructions, the data wire connects to GPIO 4. Lastly, the F or C determines if the output will be in Fahrenheit or Celsius.
At this point, there are numerous projects that can be done with these measurements. That is up to you to decide. Personally, I have other software and scripts that take these measurements and upload them to the cloud for reasons that will unfold in future blogs.
Regardless, there are many projects created by other people that involve temperature and humidity sensors. Furthermore, there are many websites that have walk-throughs and examples of different projects. All it takes is a little time and effort when searching the web. Good luck!
Sensors measure data. Furthermore, data can be a tool to improve insight and awareness. A temperature and humidity sensor can track the current environment. Then, algorithms can use the data to trigger events that increase or decrease the temperature and humidity.
Regardless, sensors are used to track important metrics. These metrics can either be acted on alone or with the combination of other sensor data. For this tiny home, temperature and humidity will be an important piece to the puzzle. Although it’s a single set of data points, it will be used with other metrics to help make anyone’s stay in this tiny house more comfortable and pleasant.