Building A Real Time Temperature and Humidity Sensor Package

Written by Roshan Noronha, Marcelo Paco, and Mahen Krishnakumar

Over the next 6 months, I’m researching the feasibility of growing mealworms on a waste food diet. Early into this project, I kept finding condensation in the mealworm growing containers. If I didn’t take care of it right away, the oats that the mealworms were feeding on would absorb the condensation and get moldy. As such, a lot of time was wasted separating the worms from the oatmeal whenever condensation was present.

Mealworms in our growing containers

However, if I had a way to measure the temperature and humidity in the growing containers, I could prevent condensation from forming in the first place. As such, the goal of our project was to accurately measure and store the temperature and humidity in each of our mealworm growing containers.

Online options didn’t thrill me as much as I thought: ~$50–$100 per sensor, and we’d need at least six. This meant I’d be burning through a large chunk of my research budget just to fix this one issue. Previously I had designed and built a PCR machine controlled via an Arduino so I was confident that approach would work in this situation. I partnered with two of my friends — Marcelo and Mahen — and we got to work.

We started by setting the following goals:

  1. The sensors used should have accurate data collection and be reliable enough for repeated use over a 6 month period, at least.
  2. The sensors should be placed away from the Control Unit and mounted inside each growing container.
  3. The data must be logged on a spreadsheet either locally or in the cloud without someone having to manually upload and group the data from each growing container together.
Raspberry Pi

With our goals determined, the next step was finding our sensors and other electronics. We went with a Raspberry Pi as the “brains” of the setup for reliable control and automation. There were several benefits in choosing the Pi. Firstly, if we added more growing containers, we’d be able to add more sensors thanks to the Pi’s 26 General Purpose I/O (GPIO) pins. More room for sensors also meant that we could add more diverse sensors to measure things like CO² or NO² emissions. Lastly, the 3.3V and 5V pins of the Pi helped us in testing sensor functionality.

DHT22 Temperature and Humidity Sensor

For data collection, we chose the DHT22 (or AM2302) Digital Relative Humidity and Temperature sensor. This sensor was capable of interfacing with the GPIO pins on the Pi. The DHT22 also featured a higher sensitivity range for both temperature and relative humidity. These sensors are mass produced by Aosong Electronics in Guangzhou, China, and are precisely calibrated in the factory.

Since we use Google Docs for team collaboration, we went with Google Sheets as our data storage platform. The Raspberry Pi was running a Raspbian distro that was configured with Python and ran notable libraries such as gspread, Adafruit_DHT, OAuth Authentication, among others. This allowed the Pi to push the sensor data to the Google Sheet at set intervals.

Having bought all our sensors and electronics, we tested everything out on a breadboard first. This ensured that the sensors were working correctly and that our code was storing the data from each sensor on our Google Sheet.

The wiring process was fairly straightforward since we had the right tools. In this case, Marcelo and Mahen used a CP-301G Pro’sKit Precision wire stripper and a variety of color coated wires that were 0.8mm in diameter. Since the breadboard was being used temporarily, they made sure that the wire was solid and not stranded. Stranded wire is used to permanently wire modules together because it is directly soldered into the breadboard to make the connection.

All the pins on a Raspberry Pi. 17 of these are GPIO pins which are represented by the green circles

The Pi has 26 input pins, out of these, 17 of are GPIO. As such, as long as one of those 17 pins were used, the DTH22 sensor would be able to send and receive data.

To verify that each sensor was connected to the correct pin, gpio allreadall was run in the command line. The command provides diagram signalling that shows which pins are active and inactive. The wiring schematic was provided by Adafruit and was repeated six times — once for each mealworm container. It’s extremely important to triple check that the wiring has been done correctly before turning on the power. Someone in our group, whose name rhymes with Smarcelo, accidentally fried a DTH22 sensor by switching the position of the power and ground wires. Triple check your work!

On the Left: Breadboard connected to the Raspberry Pi On the Right: Close up of the breadboard and sensors

We also tested out RJ11 cables for data transmission over a long distance. Since RJ11 cables are shielded, they would prevent interference from the other sensors and maintain data integrity.

Testing the the RJ11 cable over a long distance to check for data integrity

The original code for our setup was written by Mahesh Venkitachalam over at electronut.in. In order for that code to work for our purposes, we modified it to work with the Google Sheets API. The first step was to import our dependencies.

Importing dependencies

Following that, the API keys were set up using Google’s API console. From there, we simply had to store our keys securely and implement them into our code.

Implementing our API keys in our code. This allowed for our spreadsheet to be updated.

The last step was to get the the temperature and humidity values from each sensor via the getSensorData() function. This data is added to our spreadsheet as a new row.

Storing the temperature and humidity values. These will be sent to our Google Sheet.

Feel free to check out the code in its entirety on our github.

With the code and sensor wiring good to go, we began the process of translating the breadboard circuit onto an actual circuit board. Recall that our second goal was to separate the sensors from the control unit. As such, this added a bit of complexity to our design.

Circuit board design separating the different parts of the circuit

We didn’t have too much experience with translating this into an actual working design, so I reached out to my friend Kush Chhatbar to give us a hand. For avid readers of this blog, you may recall that Kush previously helped me design my PCR machine. It was great to work with him again to turn this idea into reality.

Using our sketch as a template, Kush made some tweaks that achieved our goals in a more streamlined manner. To start, Kush proposed a custom circuit board — Raspberry Pi shield— that would mount on top of the Raspberry Pi. The shield featured six RJ11 ports and would connect to the GPIO pins on the Pi. The benefit of this approach was that all six sensors could be powered simultaneously and receive the correct voltage. This meant that we would not have to use a USB hub, six USB cables and six voltage regulators to power each sensor — features that were included in our original design.

Left: Raspberry Pi shield design Right: Design for the Breakout board for each sensor

Since the sensors are located away from the Raspberry Pi and shield, we needed a method to connect the two. That’s where the breakout board came into play. It was simply a smaller circuit board that had an RJ11 port on it and a mount for the DHT22 temperature/humidity sensor. As such, the RJ11 cables were joined via the RJ11 ports from the shield to the breakout boards. This design allowed us to change any of the sensors in the event of a malfunction.

Having sorted out the logistics, we went to work turning the sketches into reality. And by we I mean Kush :) He sourced the parts, designed the custom PCB’s, figured out the wire routing, and soldered the components for the Raspberry Pi shield and breakout boards.

Soldering the connections onto the breakout board

He did a phenomenal job and you can see the results of his hard work below…

On the Left: Finished Raspberry Pi shield with RJ11 ports and breakout boards for the DH22 sensors — On the Right: Breakout board with RJ11 port and DH22 sensor attached

Putting this whole project together was really fun. Below, you can see me crimping the RJ11 jacks onto the cable.

Crimping Ethernet cables

After connecting the cables to the sensors and to the Pi, we were ready to test out the system!

Sensors connected to the Raspberry Pi shield with the RJ11 cables. Time to test it out!

The last thing left to do was to verify that the sensors were sending data to the Google spreadsheet. Additionally, we also had to double check that no erroneous values were being sent as that could indicate potential issues with our sensors.

Our sensor package seemed to be working properly at first since data was being sent to our Google Sheet. However, we soon noticed that most of the sensors had stopped sending data.

Getting data from one sensor only. As you can imagine, we were thrilled to see this. #sarcasm

We tried several things to troubleshoot this issue, including using a different Raspberry Pi, increasing the sensor refresh rate, refactoring our code, and checking the soldering connections.

Troubleshooting the sensor package. On the Left: Kush checking the power going to each sensor — On the Right: Mahen and Lucas checking our code for potential issues

Kush determined the solution by tracing the path of electricity from the sensors back to the Pi. Using that approach he realized that there was no reference point by the which the voltages could be measured — no common ground. As such, the circuits on the Raspberry Pi Shield needed to be joined to the Raspberry Pi since the sensor data was transmitted as a voltage. As each circuit had its own independent power source — 5V for the Pi and 3.3V for the shield, a common ground is critical so that both circuits are able to communicate data.

On the Left: Joining the ground of the Pi to the ground of the Shield — On the Right: Coating the solder work with silicone. This will prevent a short from occurring.

To test this, a jumper wire was inserted between the ground of the Pi and that of the shield. Lo and behold, we starting seeing consistent temperature and humidity data! After the soldering the jumper wire to the shield and coating the circuit with silicone, we had a working sensor package! It’s just that easy, folks.

The completed setup
On the Left: Sensors in the mealworm containers — On the Right: The Raspberry Pi and shield. The monitor is showing the data being sent to our Google Sheet.

I always like working on projects that try to solve a real world problem or have a real world application so this was definitely right up my alley. It really wouldn’t have been possible without Marcelo, Mahen, and Kush. They all went above and beyond to debug any issues that popped up and put in the hours to make sure everything ran smoothly. I’ll definitely mention them in my Nobel Prize acceptance speech :)

Down the road, I plan to use these sensors to measure the effect of temperature on mealworm growth rates. For now, I’m just really excited to start using this data to have mold-free mealworm containers.

If you found this project interesting, feel free to check out the other cool things that are featured on my website.

And to see why I’m growing mealworms in the first place check out our lab’s research!

And definitely check out what Marcelo, Mahen and Kush are working on because they are all involved in game-changing work!

Before I wrap things up I’d like to give special thanks to the Langara College biology department for their guidance and Langara College and the Clean Tech Internship Program for providing the facilities and funding.

As always, thanks for reading.

PS: Some of you may have noticed “Roshan’s Ego” cropping up in all the pictures. Apparently, I’m a bit of a narcissist and the team really appreciated that :)
PPS: Thanks to Jonathan Lee for his tireless effort and dedication in proofreading. Even in the darkest of times, he always pulls through…