Read The Arduino Inventor's Guide Online
Authors: Unknown
FIGURE 7-16:
Transistor circuit to drive the motor from an Arduino pin
The last piece in this circuit is a protection diode, sometimes called a
fly-back diode
, and it protects the transistor from damage that may be caused by the motor. Inside a motor are a bunch of coils that create an electromagnet that pushes and pulls against permanent magnets in the motor—this is what causes the axle to spin. Coils are a really interesting component used in electronics. The magnetic field that they create is actually a form of stored energy, and when the circuit is turned off, this stored energy can rebound and cause large voltage spikes that will damage the transistor. The fly-back diode creates a path for this voltage spike to dissipate without going through the transistor. It’s sometimes also called a
snubber circuit
.
In order to wire this component in correctly, it is important to note that diodes are
polarized
, and the orientation makes a difference. The body of the diode has a line or band at one end of the body, as shown in
Figure 7-17
. Make sure that the side with the band is connected to the positive (5 V) side of the motor.
FIGURE 7-17:
Fly-back diode used in the transistor circuit, with a quarter for size comparison
Add the diode to the motor, making sure that the side of the diode with the line is connected to the 5 V motor wire, as shown in the circuit diagram in
Figure 7-16
. Connect the other leg of the diode to the other wire of the DC motor—the one connected to the collector pin of the transistor. This means that the two legs of the diode share the same connections as the two wires of the motor. When two devices are wired up like this, we say they’re wired
in parallel
.
You should now have a complete circuit that has each of the three subcircuits in
Figure 7-6
. You’ll add a few more lines of code to control the motor.
The code for controlling the motor is very simple, just like the code you used to turn an LED on and off. Add the code in
Listing 7-6
to your current sketch.
LISTING 7-6:
Complete Tiny Desktop Greenhouse control code
#include
Servo myServo;
--
snip
--
void setup()
{
➊
pinMode
(11,
OUTPUT
);
myServo.attach(9, 1000, 2000);
Serial.begin(9600); //initializes the serial communication
--
snip
--
}
void loop()
{
--
snip
--
Serial.println(); //new line character
if(tempF > setPoint)
{
myServo.write(180);
➋
digitalWrite(11,
HIGH
); //turn the fan on
}
else if(tempF < returnPoint)
{
myServo.write(0);
➌
digitalWrite(11,
LOW
); //turn the fan off
}
delay(1000);
}
/***********************************************************/
float volts(int rawCount)
{
const float AREF = 5.0;
float calculatedVolts;
calculatedVolts = rawCount * AREF / 1023;
return calculatedVolts;
}
There are just a few new lines of code. The first is in the
setup()
. This sets pin 11, to which the motor is attached via the transistor, as an
OUTPUT
➊
. Next is the set of conditional
if()
–
else if()
blocks. Here, you add two commands to turn the motor on
➋
and off
➌
. Remember that the motor will really be the greenhouse fan. With these few extra lines of code, the fan will turn on at the same time as the window opens, and it will turn off when the window closes.
After you’ve added these lines of code, upload this latest version to your Arduino. Open the Serial Monitor, and test it again. Try warming up the temperature sensor by squeezing it between your fingers or using your breath, and watch what happens. As soon as the temperature readings get to about 85 degrees Fahrenheit, the servo motor should move and the hobby motor should kick in. You might notice that as soon as the motor turns on, the temperature readings go all out of whack. There is a lot going on here, but we have a quick fix for it.
When the motor turns on, the voltage of the Arduino drops down to about 4.1–4.5 V because of the extra current load of the motor. You may see that as soon as the motor spins up, the temperature readings start changing sporadically, and the motor may continue to turn on and off several times before the temperature readings settle down. Earlier we said that when you use
analogRead()
,
1023
is equal to 5 V, but that’s only partially true. The full truth is that
1023
is equal to whatever the voltage from the source is, so if the source voltage drops to 4.1 V,
1023
is now equal to 4.1 V. This messes up the Arduino’s ability to take accurate measurements.
To rectify this, add two lines of code at the very beginning of the
loop()
, right after the first curly bracket, to tell the Arduino to turn off the motor before reading the temperature sensor:
digitalWrite
(11,
LOW
);
//turn off the motor before
//reading sensor
delay
(1);
//short 1 ms delay before
//reading sensor
Now the Arduino will turn the motor off for exactly 1 ms before reading the voltage on the temperature sensor. This isolates the voltage drop from the motor and the
analogRead()
without adding too much extra code.
After adding these two lines, upload the new sketch to your Arduino, and try heating up the sensor again. Now it should behave much more predictably. With the circuits built and the code working smoothly, it’s time to build the actual greenhouse structure.
We provide a template for the greenhouse enclosure we created using cardboard, but you could use anything you want. In fact, IKEA sells a great little greenhouse called the SOCKER that you could easily modify to work with this project.
For the Tiny Desktop Greenhouse, the finished dimensions are roughly 4.5 × 4.5 inches for the base and 6 inches at the tallest point. In the resources available at
https://www.nostarch.com/arduinoinventor/
, we have two template options: one is broken out into three sheets of 8.5 × 11-inch cardboard (shown in
Figure 7-18
), and the other is on a single sheet of cardboard measuring 11 × 17 inches.
FIGURE 7-18:
Tiny Desktop Greenhouse cardboard enclosure template (not full size)
Carefully cut out the pieces of the template from the cardboard. There are four unique pieces: the pentagonal side pieces, the front/ back square walls, the roof window, and the fan-motor holder. Take one square side piece and one pentagonal front/back piece and lay them side by side, with the transparency side facing up. Use a narrow strip of masking tape to hold these two sides together, as shown in
Figure 7-19
. Repeat this process until you have all four of the side wall pieces attached, but don’t tape the last two walls together; leave the enclosure lying flat until you’ve added the windows.
FIGURE 7-19:
Use a narrow strip of tape to hold the pieces together.
Now you need to cut six pieces of transparency film to be slightly larger than the opening in each of the wall pieces and the windows. For the template we provide, you will need four squares that measure 4.25 × 4.25 inches and two squares that measure 4.25 × 2.5 inches. You should be able to cut these six pieces out of a single sheet of transparency film; it’s a good idea to trace them on first to get the most out of the film. At this point, you only want to secure the windows for the side walls; you’ll add the roof windows at the very end. Using a bead of glue or small piece of tape, secure these windows to the inside of the greenhouse on the same side as the tape, as shown in
Figure 7-20
.
FIGURE 7-20:
Carefully line up the transparency film window to adhere it to the cardboard.
Once those transparency panes are in place, run one more piece of tape along the last exposed edge, and connect it so that you have a square base and a structure that resembles a small greenhouse, as shown in
Figure 7-21
. The top and bottom of the greenhouse should
still be open, and it may still feel a little unstable, but as soon as you add the roof, the entire structure will hold together.