The Arduino Inventor's Guide (36 page)

BOOK: The Arduino Inventor's Guide
11.43Mb size Format: txt, pdf, ePub
PROGRAM THE AUTOVENT

Add the servo code shown in
Listing 7-5
.

LISTING 7-5:
Adding in the servo control


#include
<
Servo
.h>

Servo
myServo;
  
//Example sketch – calculates the temperature from the TMP36
  
//sensor and prints it to the Serial Monitor
  
int rawSensorValue;
  
float rawVolts;
  
float tempC;
  
float tempF;

int
setPoint = 85;
  
int
returnPoint = 83;
  
void setup()
  
{
    myServo.
attach
(

9, 1000, 2000);
//initializes myServo object
    
Serial.begin(9600);  //initializes the serial communication
    
--
snip
--
  
}
  
void loop()
  
{
    
--
snip
--
    
Serial.println();
    
//new line character

   
if
(tempF > setPoint)
    {
      myServo.
write
(180);
    }
    
else if
(tempF < returnPoint)
    {
      myServo.
write
(0);
    }
    
delay(1000);
  
}
  
/***********************************************************/
  
float volts(int rawCount)
  
--
snip
--

Remember from
Project 6
that to use the servo you need to include the Servo library

and create a servo object named
myServo

. Next, create two variables

to define the
set points
for the control system. These set points are the temperatures
at which the window will automatically open and close. Notice that
setPoint
, which opens the window, is 2 degrees higher than
returnPoint
, which closes the window, giving you 2 degrees where the window will not be opening or closing. This control technique, referred to as
hysteresis
, is useful for systems where the temperature might fluctuate slightly and keeps the window from constantly opening and closing at the smallest temperature variation.

Finally, you need to initialize the servo by telling the Arduino that the servo is connected to pin 9

. You might notice that this
myServo.attach()
command has two extra numbers, rather than the one number you used in
Project 6
. To understand why, take a look at “
How Servos Really Work
” on page
201
.

Finally, for the control logic, you’ll use a nested
if()

else if()
control statement. Add the eight lines of code at

just above the
delay()
to move the servo to a 180-degree position if the temperature is above 85 degrees Fahrenheit (the
setPoint
) and return the servo to the 0-degree position if the temperature drops below 83 degrees Fahrenheit (the
returnPoint
). This will open and close the autovent window.

Upload the updated code from
Listing 7-5
to your Arduino, and open the Serial Monitor. Try it out by pinching the sensor between your fingers or cupping your hands around the sensor and blowing deeply to warm it above 85 degrees. Watch the Serial Monitor to see the temperature rise. As soon as it hits 85 degrees, you should hear the servo motor as it moves into position. Now, let the temperature sensor sit and cool down. As soon as it dips below the
returnPoint
, you should hear the servo motor again as it returns to the 0-degree position. Pretty neat, eh?

Now you’ll build the final component: the fan.

BUILD THE FAN MOTOR

The fan, which will move air around in the greenhouse, is going to spin via a small DC hobby motor—a standard cylindrical device with two wires and a center axle that spins when you apply voltage to the leads. The servo motor you’ve been using so far has gearing inside that allows it to move very precisely in a tightly defined range of motion. Recall that it has only about 180 degrees of motion. You’ll use a DC motor here, rather than the servo motor, because you’ll need the fan blade to spin continuously the entire time you apply power to it. This makes it perfect for a fan function. The DC motor you’ll use is designed to operate between 3 V and 6 V but draws around 200–300 mA when it’s spinning. The Arduino
OUTPUT
pins are capable of driving only about 40 mA of current, so to provide enough amps for the DC motor, you’ll build an extra circuit called a
transistor amplifier circuit
—more technically known as a
common-emitter amplifier
.

HOW SERVOS REALLY WORK

Servos are neat devices that move a motor to a specific position based on a signal from the microcontroller. A standard servo motor has a fixed range of motion of about 180 degrees.

All servo motors have three wires: a white (or sometimes yellow or orange) wire for receiving the signal, a red power wire, and a black ground wire. The signal is a unique pulse sent every 20 ms, or at a rate of 50 Hz. To encode a position, a microcontroller varies the width of the pulse to indicate the angle or position that the motor will turn to (see
Project 5
for more details on pulse width modulation). For most standard servo motors, a 1,000-microsecond (1,000 μs) pulse indicates the 0-degree position, and a 2,000 μs pulse indicates the full 180-degree position, so a 1,500 μs pulse indicates the midpoint of the servo, or the 90-degree position.

The Arduino’s
Servo.h
library maps the pulse widths to the motor positions, but the library has a slightly different definition for the servo positions and pulse widths, using a 544 μs pulse for the 0-degree position and a 2,400 μs pulse for the 180-degree position. This allows the library to work with
extended-range
servos, but it is outside the limits of most standard servos. So, when you use a command like
myServo.write(0);
the motor receives a 544 μs pulse and will try moving beyond its own physical limits. If that happens, the motor will shake, buzz, and heat up because it’s just not able to move to a position that coincides with a 544 μs pulse.

To counteract this, you can set limits for the servo in the code. As in
Project 6
, use the command
myServo.attach(9);
to initialize the servo on pin 9 of the Arduino. You can also add parameters to set lower and upper pulse width limits for the motor, with
myServo.attach(9, 1000, 2000);
. With this initialization, the command
myServo.write(0);
will move the motor to its 0-degree angle position and it won’t shake, buzz, or heat up.

If you want to know more about how servos work, take a look at the tutorial on SparkFun at
https://www.sparkfun.com/tutorials/283/
.

A transistor has three legs: the collector (C), base (B), and emitter (E). Any current that goes into the base is amplified on the collector pin. The base is like a control for a door that allows current to move from the collector to the emitter. The more you push on the base, the wider the door opens, and more current can pass through from the collector to the emitter. The amazing thing about transistors is that you need apply only a small amount of current at the base to let a large amount of current flow from the collector to the emitter. If you provide too much current to the base, you can burn the transistor out, so in the same way you’ve used resistors to limit the current flowing through LEDs, you’ll use a 330 Ω resistor in this circuit to limit the current flowing to the base.

There are many different applications for transistors, such as amplifiers and other devices, but here you’re just going to use it like a controllable switch. Even though the Arduino pin can only supply a small amount of current, when you send a
HIGH
signal to the base of the transistor, the transistor turns “on.” This allows current to flow between the collector and the emitter. It essentially connects these pins together, like closing a switch.

The emitter side of the transistor is connected to the ground rail. Notice that one side of the motor is connected to the 5 V rail, and the other side of the motor is connected to the collector of the transistor. When the transistor is turned “on,” this connects the collector to the emitter of the transistor, closing the switch between the motor and ground and making it spin. Engineers call this “operating a transistor between cut-off and saturation mode.” Power for the motor is coming directly from the power rails. That means you can use the limited current provided by the Arduino
OUTPUT
pins to control devices requiring a much larger current flow.

You’ll build the transistor circuit on the top part of the breadboard, as shown in
Figure 7-14
.

FIGURE 7-14:
Adding the fan-motor transistor control circuit

Find the transistor in your kit. As you can see from
Figure 7-15
, the transistor looks a lot like the temperature sensor, though if you look closely you should see 2N2222 or BC337 on the flat side of the casing. This is an
NPN transistor
, and it will act as the motor’s on/off switch that you control with the Arduino board.

FIGURE 7-15:
NPN transistors used in this project—the 2N2222 (left) and the replacement part BC337 (right)

Hold the transistor so that the flat edge is facing to the left, and insert it into the top part of the breadboard with the top pin about six rows down, as shown in
Figure 7-14
. In this position, the top pin is the collector, the middle is the base, and the bottom is the emitter (
Figure 7-15
).

Connect a 330 Ω resistor to the base pin and stretch it across the ditch in the breadboard, as shown in
Figure 7-14
. Then, connect the other side of this resistor to pin 11 on your Arduino so that pin 11 connects to the base pin through the 330 Ω resistor. This is the low-current control signal from the Arduino that will switch the transistor on and off. When the transistor is switched on, current will flow through the motor, and it will spin.

Connect a small jumper wire from the emitter of the transistor (lower pin) to the ground rail. Lastly, connect one of the wires of the motor to the collector (top) pin of the transistor. The motor will spin either clockwise or counterclockwise depending on which wire you use, but in this case it doesn’t matter which way it spins, so you can choose either motor wire. Connect the other motor wire to the power rail, and then use a jumper wire to connect the power rail of the breadboard to 5 V on the Arduino. When a small current signal is detected on the base pin, the connection between the collector and the emitter is closed. This connects a path from 5 V through the motor to GND and completes a circuit path that will cause the motor to spin.
Figure 7-16
shows the circuit for the motor.

Other books

Broken Rainbows by Catrin Collier
Swimming Without a Net by MaryJanice Davidson
Two Crosses by Elizabeth Musser
Alice-Miranda In New York 5 by Jacqueline Harvey
Rachel by Jill Smith
Dry Bones by Margaret Mayhew
Haunted Ever After by Juliet Madison
Pharaoh by Karen Essex