SMILEY’S WORKSHOP ■ BY JOE PARDUE
C PROGRAMMING - HARDWARE - PROJECTS
Arduino Handheld Prototyper
Part 3 Fresh Air Controller
Last month, we continued our series on an Arduino handheld prototyper that — as the
name implies — lets us develop prototypes that are portable in our very own hand. I'll
admit it. I've been having too much fun with this prototyper. This thing lets me do
Arduino development using a PC (as usual) but also lets me get input from pushbutton
keys, plus produce visual output on an LCD — all in a compact portable framework.
Gone are the days of having the bits and pieces scattered around on a table, hooked up
to a PC. Now, I can just grab the whole thing and take off.
Fresh Air Controller for a Castle
I was in the midst of developing the handheld prototyper when
a friend who lives nearby asked me to help him use an Arduino to
develop a fresh air controller for his castle. (Well, it’s certainly close
enough for these parts.) It's got a courtyard (Figure 1), a great
room, suits of armor, a broadsword next to a fireplace you could
roast a dragon in, plus gargoyles (Figure 2). Joel Fairstein owns this
particular castle and does some outstanding tinkering, so he
decided to do a project to help heat and cool his place. This is a
perfect application for our handheld prototyper — even if you don't
own a castle.
■ FIGURE 1: Castles have courtyards. ■ FIGURE 2: What's a castle without gargoyles?
September 2013 69
Go to www.nutsvolts.com/index.php?/magazine/article/september2013_SmileysWorkshop for any additional
files and/or downloads associated with this article. You can also discuss this topic at http://forum.nutsvolts.com.
■ FIGURE 4: Inside view of a vent box and the controller.
■ FIGURE 3: Fresh air controller prototype. ■ FIGURE 5: Outside view of a vent box.
Figure 3 shows the prototyper set up as a fresh air can feel cooler in Death Valley at 105° than you might
controller; Figures 4 and 5 show the vent boxes Joel feel in a Louisiana swamp at 85°. The reason is that we
designed that have an aerodynamic vane that gently sweat to cool off. For sweat to work, it has to evaporate.
opens when the attic fan turns on, then closes to keep out If the air is dry, it can evaporate quickly; if the air is very
the rain when the attic fan shuts off. Of course, they have humid, it might not evaporate at all.
a bug screen to keep out the critters at all times. To use a modest simplification: The amount of water
These vent boxes are easily removable for those times vapor that air can hold depends on the air temperature.
of the year when it is just too hot or cold outside to Warmer air can hold more water vapor than cooler air.
warrant using this system. This is why it tends to rain when a warm front with a lot
The fresh air controller relies on us having accurate of moisture runs into a cold front. The warm air cools
measures of indoor and outdoor temperatures and down and the water vapor condenses into liquid that
humidity, so we'll know when to turn the attic fan on and drops out of the sky.
off. Let's look at measuring these parameters and the We use the term RH (Relative Humidity) to mean the
sensor we will be using. amount of water vapor (humidity) that may be present at
a given temperature before the air becomes saturated and
water begins to turn to liquid — the dew point.
Sensing Temperature and Humidity An RH of 100% means that the air is saturated,
while 0% means there is no water vapor present. People
Relative Humidity tend to be most comfortable at an RH between 30%
Humidity tends to be important to our comfort. You and 70%.
70 September 2013
■ FIGURE 8: Fritzing PCB view.
■ FIGURE 6: Fritzing breadboard view.
Measuring RH
In order to measure RH, we need to not only sense
the water vapor pressure but we need to sense the
temperature, as well. There are many ways to do this, but
the most common inexpensive way this is done is with
sensors such as the DHT22 (Figure 6) that outputs a
digital signal that we can read for the RH and
temperature. In practice, we can read this sensor about
once every two seconds and expect to get an RH reading
that is ±1% accurate. The digital signal output is somewhat
arcane, but fortunately we have access to pre-built and
tested libraries to use with the Arduino.
Temperature
Temperature is a measure of how hot or cold an
object is; we measure temperature with a thermometer
(duh). The DHT22 uses a thermistor (thermal resistor)
which is a type of resistor that has a resistance that varies
significantly with temperature. As with the RH, the
temperature is reported as part of the digital data output ■ FIGURE 7: Fritzing schematic view.
of the DHT22. The temperature reading is accurate to
±0.36 Fahrenheit. Adafruit or Virtuabotix for about $13]. This sensor is a bit
more expensive than its DHT11 little brother, but it is also
more precise, accurate, and has a better range for
temperature/humidity.
Using the DHT22 Hardware It is absolutely critical that you pay attention here
There are lots of ways to sense temperature and because you can very easily kill your $13 DHT22 if you
humidity. We will use the DHT22 [you can get one from wire it wrong.
September 2013 71
■ FIGURE 9: Open the DHT22 example.
Design for DHT22 With Fritzing
Let's look at how to use Fritzing to design a
circuit with the DHT22 on the Arduino proto
shield. You can refer to my blog at
http://blog.smileymicros.com/ to see how to do
this. You can get the Fritzing component for the
DHT22 from http://learn.adafruit.com/using-the-
adafruit-library-with-fritzing/download-the-
fritzing-library-from-github. Sorry about the long
URL, but Adafruit is a good place to get the
DHT22. They also have their own Arduino library
for this part. However, we will use the
aforementioned Virtuabotix library (it worked
better for me) since you can also get a DHT22
there.
We see the Fritzing component in the
breadboard view in Figure 6; the schematic view
in Figure 7; and the printed circuit board (PCB)
view in Figure 8. Note very carefully how this is
wired. Make sure that you don't miswire this
device because (as mentioned) you can kill it if
you do. The Adafruit tutorial recommends a 10K
pull-up on the data line, but I found it was not
needed in this application.
Test It With the Arduino
As mentioned, we will use the DHT22
Arduino library written by Joseph Dattilo of
Virtuabotix (another good reason to buy your
DHT22 from them). The link to get this code is
www.virtuabotix.com/core/wp-content/uploads
/2012/09/DHT22_1S0A.zip.
Since this is GPL, I've included it in the
FreshAirController.zip download that you can get
from the article link.
To use this library, unzip the DHT22_1S0A.zip
file and then copy the DHT22 directory to your
Arduino libraries directory. In my case, this was
C:\Arduino-1.0.4\libraries. If you have the Arduino
integrated development environment (IDE) open,
you will need to close it and reopen it so that it will
find the library.
The Virtuabotix library has some examples that
we can use to test this design. Open the Arduino
IDE, then in the File menu item click on the
Examples\DHT22\dht22_functions as shown in
Figure 9.
■ FIGURE 10: DHT22 example.
72 September 2013
■ FIGURE 11: Two DHT22s breadboard view.
You can see in the code the various steps
required to use the sensor. Add the following to
your include list:
#include <dht22.h>
Next, you'll need to create an instance of the
class library as follows:
dht22 DHT22;
We have physically attached the DHT22
sensors to pin 11, so you will need to change the
following in the example setup function. Change
this:
DHT22.attach(2);
to this:
DHT22.attach(11);
Reading the Sensors
We read the sensor as follows:
int chk = DHT22.read();
Reading the sensor returns a value indicating
the statues of the read which you may check with
the following:
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error");
break;
case -2: Serial.println("Time out error");
break;
default: Serial.println("Unknown error");
break;
}
You will want to design your own code to
respond to these conditions since you only want to
use those values from a read that reports "OK."
■ FIGURE 12: Two DHT22s schematic view.
In the example code, we see several ways to
acquire the data that was read, and report it to the
serial port:
Serial.print("Temperature (°F): ");
Serial.print("Humidity (%): "); Serial.println(DHT22.fahrenheit(), DEC);
Serial.println((float)DHT22.humidity, DEC);
Serial.print("Temperature (°K): ");
Serial.print("Temperature (°C): "); Serial.println(DHT22.kelvin(), DEC);
Serial.println((float)DHT22.temperature, DEC);
September 2013 73
Serial.print("Dew Point (°C): ");
Serial.println(DHT22.dewPoint(), DEC);
Serial.print("Dew PointFast (°C): ");
Serial.println(DHT22.dewPointFast(), DEC);
We see the output on the Arduino serial
monitor in Figure 10. Note that this runs the
serial port at 9600 baud.
Two DHT22 Sensors
Adding a second sensor is shown in Figures
11 and 12 in Fritzing breadboard and schematic
views. We connect the second sensor to the
Arduino pin 10 and make a couple of minor
changes in the code. We are now ready to go.
The following source code shows how
simple it is to add a sensor to a project when
you use a library. In the first example, we
created an instance of the dht22 class using
dht22 DHT22. For the example that uses two
sensors, we simply add two instances of the
class with:
dht22 DHT220;
dht22 DHT221;
Next, we copy and paste some code as
■ FIGURE 13: DHT22 example with two sensors.
shown in the listing. We’ll have two sensors
working in a matter of minutes:
#include <dht22.h>
dht22 DHT220;
■ FIGURE 14: Attic fan. dht22 DHT221;
void setup()
{
DHT220.attach(10);
DHT221.attach(11);
Serial.begin(9600);
Serial.println("DHT22 TEST PROGRAM - two
sensors ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT22LIB_VERSION);
}
void loop()
{
Serial.println("\n");
int chk0 = DHT220.read();
Serial.print("Read sensor 0: ");
switch (chk0)
{
case 0: Serial.println("Sensor 0 OK");
break;
74 September 2013
case -1: Serial.println("Sensor 0 Checksum
error"); break;
case -2: Serial.println("Sensor 0 Time out
error"); break;
default: Serial.println("Sensor 0 Unknown
error"); break;
}
Serial.print("Sensor 0 Humidity (%): ");
Serial.println((float)DHT220.humidity, DEC);
Serial.print("Sensor 0 Temperature (°C): ");
Serial.println((float)DHT220.temperature, DEC);
Serial.print("Sensor 0 Temperature (°F): ");
Serial.println(DHT220.fahrenheit(), DEC);
Serial.print("Sensor 0 Temperature (°K): ");
Serial.println(DHT220.kelvin(), DEC);
Serial.print("Sensor 0 Dew Point (°C): ");
Serial.println(DHT220.dewPoint(), DEC);
Serial.print("Sensor 0 Dew PointFast (°C): ");
Serial.println(DHT220.dewPointFast(), DEC);
Serial.print("Read sensor 0: ");
int chk1 = DHT221.read();
switch (chk1)
{
case 0: Serial.println("Sensor 1 OK"); break;
case -1: Serial.println("Sensor 1 Checksum
error"); break;
case -2: Serial.println("Sensor 1 Time out
error"); break;
default: Serial.println("Sensor 1 Unknown
error"); break;
}
Serial.print("Sensor 1 Humidity (%): ");
Serial.println((float)DHT221.humidity, DEC);
Serial.print("Sensor 1 Temperature (°C): ");
Serial.println((float)DHT221.temperature, DEC);
Serial.print("Sensor 1 Temperature (°F): ");
Serial.println(DHT221.fahrenheit(), DEC);
Serial.print("Sensor 0 Temperature (°K): ");
Serial.println(DHT221.kelvin(), DEC); ■ FIGURE 15: Two DHT22s on handheld prototyper.
Serial.print("Sensor 1 Dew Point (°C): ");
Serial.println(DHT221.dewPoint(), DEC);
Serial.print("Sensor 1 Dew PointFast (°C): "); temperature and humidity both inside and outside of the
Serial.println(DHT221.dewPointFast(), DEC); castle. We will look at an algorithm in a moment that lets
us set trip points so our attic fan (Figure 14) will come on
delay(2000); and draw fresh air through the vent box (refer back to
}
Figures 1 and 2) to provide us with inexpensive cooling or
This provides the data to the serial monitor shown in heating as needed.
Figure 13. We hook up the I2C mini terminal part of the
handheld prototyper as shown in Figures 15 and 16. This
Fresh Air Controller Design combines the Arduino proto shield and the I2C mini
terminal into the handheld prototyper used in this system.
In order to control the fresh air, we need to know the The I2C mini terminal will let the user see the various
September 2013 75
This is the algorithm I came up with for
deciding whether to turn the fan on or off,
based on the above variables:
// Run the algorithm
if(humidityOutdoor >= setHumidity)
{
fanState = 0;
}
else if( (tempOutdoor > setTempLow) &&
(tempOutdoor < tempIndoor) &&
(setTempLow != -1))
{
fanState = 1;
}
else if( (tempOutdoor < setTempHigh) &&
(tempOutdoor > tempIndoor) &&
(setTempHigh != -1))
{
fanState = 1;
}
else
{
fanState = 0;
}
setFan(fanState);
■ FIGURE 16: Fresh air controller schematic view.
Please note that in the above code the
else if statement should be all on one line in
the Arduino code. I split it up here for
readability.
temperature and humidity parameters on the LCD, and First, we check to see if the humidity outdoors is
input temperature and humidity setpoints using the keys greater than the set humidity (meaning it is raining). If so,
to select from menu items on the LCD. we turn the fan off.
Next, we check to see if the temperature outdoors is
greater than our low temperature setting AND that the
The Fresh Air Controller Algorithm temperature outdoors is less than the indoor temperature,
AND that the user has set the low temperature. If all that
The tasks for the fresh air controller seem fairly simple is true, we turn the fan on.
when you first think about them. If it is too warm indoors Then, we check to see if the temperature outdoors is
and cooler outdoors, you turn on the attic fan. If it is too less than our high temperature setting AND that the
cold indoors and warmer outdoors, you turn on the attic temperature outdoors is greater than the indoor
fan. If the attic fan is on and it starts to rain, you turn off temperature, AND that the user has set the high
the attic fan. temperature. If all that is true, we turn the fan on.
What you need is for the user to specify what is 'too Finally, if none of that is true, we turn the fan off.
warm' and 'too cold,' then let the computer logic do the Did I say 'fairly simple'? Well it is, but it took me a
rest. First, you will want to get the user to specify the while to get my head around the logic. Maybe that says
following values: something about my head?
setHumidity Next month, we will see how to complete the design
setTempLow
for this handheld prototyper project using the I2C mini
setTempHigh
terminal to display the fresh air controller data on the LCD
Then, you have the Arduino read the DHT22 for the and get user feedback from the keys. NV
following values:
humidityOutdoor
tempOutdoor
tempLow
76 September 2013