Sunday, January 27, 2013

ProtoCylon: Control

ProtoCylon: Control

By Bobby Neal Winters

Again and again I am reminded of the importance of the basic model:  Input information, Process that information, and Output (or Act on!) the processed information.  This simple model helps us to keep the processes separate and keeps us from confusing them.  Indeed, once we understand various, separate processes, we can mix and match ways of inputting with ways of outputting.
This brings us to today’s project: Temperature control.  One way I justify this new hobby of mine to the Other Half is that one day I can use it to control the fan for the greenhouse next door.  The fan has to be turned on when it gets too hot.  
During the summer, there are some days we simply have to anticipate that it will be hot before it actually gets hot and think to turn on the fan before we go to work.  If there were a temperature control for the fan, we would neither have to remember to turn it on in the morning or turn it off in the evening.  
Today’s project will turn a fan on and off at a certain threshold temperature.  It won’t control a fan as big as the one in the greenhouse, but I am into the “baby step” thing.
For the input part, I refer my reader to my last blog entry on Telling the Temperature.  In that, I construct a temperature sensor that connects to the Arduino with one wire.  The sensor itself is a surprisingly tiny component.  You can get ten of them for fifteen dollars.  This is input at its best.  You don’t have to type in anything; you don’t have to turn a dial; you just connect it and let the software do the rest.
The control in this case is easy as well once you have the transistor wired in.  To control the fan, I refer the reader to Beginning Arduino by Michael MacRoberts in the section where he shows how to use a TIP120 transistor to control a motor.  
Transistors are kind of scary to me.  I still don’t understand them very well.  This I know: They have three prongs.  One of the prongs goes to power, one goes to ground, and the third (actually the middle one) connects to the Arduino where it gets instructions for controlling the flow of current.
All of the complication of this project took place on the breadboard.  Wiring the temperature sensor and the transistor are still non-intuitive to me.
The code is simple:

// include the library code:
#include
#include

//Temperature Sensor pin
#define ONE_WIRE_BUS 3

//Pin that controls the transistor

int transistorPin=A1;

// initialize the library with the numbers of the interface pins
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer, outsideThermometer;
//place to put the temperature
float tempC=20;

void setup() {
 //Set up transistor
 pinMode(transistorPin, OUTPUT);
 // Get the temperature sensors running.
 sensors.begin();
/*The 0 in the second argument is because this is the first transistor.  Computers start counting at 0.*/
 sensors.getAddress(insideThermometer,0);
}

void loop() {
 //Get the Temperatures
 sensors.requestTemperatures();
 tempC=sensors.getTempC(insideThermometer);
 //Is it hot enough for the fan?
 if(tempC>25){
     //Turn the fan on
     analogWrite(transistorPin,255);
 }
 else{
     //Turn the fan off
     analogWrite(transistorPin,0);
 }
 //No reason to be impatient
 delay(1000);
}

No comments: