Showing posts with label Beginning Arduino. Show all posts
Showing posts with label Beginning Arduino. Show all posts

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);
}

Monday, January 21, 2013

ProtoCylon: Telling the Temperature


ProtoCylon: Telling the Temperature

By Bobby Neal Winters
I remember reading old documents, ages ago, about the computing model that was in three parts: Input, Process, and Output.  I thought it was kind of goofy at the time, but I was younger and smarter then.  Now that I am old and stupid I’ve gained an appreciation of how separating a project into smaller parts--even into three parts--can make doing the project a reality. (When I was younger and smarter, I never actually did anything.)
Thus is the tale of teaching my ProtoCylon to tell the temperature.  It comes in three parts and there is a part where I grow just a little bit.
The Input part I referred to in my post ProtoCylon Senses. This involved the DS18B20 digital temperature sensor.  This is a tiny thing.  It has three pins like a transistor.  Heck, for all I know, it might be a transistor.  The power goes in one side, out the other, and the center pin is connected to the Arduino board.  You can check the wiring at this tutorial.
For the Output, I used an LCD display.  This required growth for me.  I will work with the most obvious part first. This display has 16 pins.  That is a lot of pins. This is eased a bit by the fact that four of them are used for power and one is just grounded out.  This leaves eleven, though, and that is nothing to sneeze at.  This wasn’t the major growth inducer, however.  No, the big this is that I had to solder--yes, solder--those 16 pins to a set of pins that would allow me to plug it into the breadboard before I could even start to hook up the rest. (I will leave out the part where I attempted to hook it up without soldering. Not a happy time.)
After I got it soldered, I hooked it up and it didn’t work.  This is becoming a theme with me, but I am learning from this and recovering quicker each time.  The first stage is not to allow myself to be crushed by each failure.  I stop, take a deep breath, let it out, and try to figure out where I goofed.  
One useful technique is to take it apart and put it together again--slowly from scratch.  For wiring up the LCD display, I recommend the tutorial at Adafruit Learning System.
In between the Input and the Output is the Process.  The process in this case involved the use of libraries.  Libraries are specialized functions that make using hardware easier.  While there is a young turk deep inside me that would like to write my own libraries, there is an old goat who would like to actually get something done without taking it apart electron by electron.  Therefore, I happily use the libraries.
In creating this project, I actually used my creativity to merge two programs.  One of these was a “Hello, world!” program for the LCD that was an example that came with my Arduino compiler.  The other was in the book Beginning Arduino by Michael MacRoberts. I lay claim to the creativity crown because I did have to modify their code mutatis mutandis for the project to work.  The completed project follows:


This is the code:
/*
The uses temperature sensors to take the temperature
and outputs it to an LCD display.
I fused two programs by other people and modified them.

The first was:

The LCD Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystal

The second was from Begining Arduino by Michael MacRoberts
*/

// include the library code:
#include
#include
#include

#define ONE_WIRE_BUS 3

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7,8,9,10,11,12);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer, outsideThermometer;

void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
 lcd.clear();
 // Get the temperature sensors running.
 sensors.begin();
 sensors.getAddress(insideThermometer,0);
 sensors.getAddress(outsideThermometer,1);

}

void loop() {
 //Get the Temperatures
 sensors.requestTemperatures();
 //Print it on the first row, i.e. row 0
 lcd.setCursor(0,0);
 printTemperature(insideThermometer);
 //Print it on the second row, i.e. row 1
 lcd.setCursor(0, 1);
 printTemperature(outsideThermometer);
 //enjoy the weather for one second
 delay(1000);
}


void printTemperature(DeviceAddress deviceAddress){
 //Get the temperature
 float tempC=sensors.getTempC(deviceAddress);
 //print it out.
 lcd.print("TempF:  ");
 lcd.print(DallasTemperature::toFahrenheit(tempC));
}

Sunday, January 20, 2013

ProtoCylon Senses

ProtoCylon Senses

By Bobby Winters

I’ve spent the last week on a voyage of discovery in which I’ve discovered myself the same as I’ve always been.
Let me explain.  
Last weekend I tried to get a UltraSonic distance sensor hooked up to my Arduino Uno.  The procedure is easy to find on the Internet.  I wired it, and it didn’t work.  I required it, and...it didn’t work.  In my reading of the reviews in the process of buying this sensor, one of the reviewers had complained the one he bought was trash. I figured I’d had a bit of bad luck, so I bought another one.  Yesterday, I wired it up again.
It worked great.
Then my curiosity got the better of me, so I plugged in the other one to the same wiring.  It worked too.  It had been miswired the first time.  
Now, I am fully capable of screwing up that badly.  I admit that freely.  In my fiddling around, I discovered that one of the jumper wires I had been using was bad.  This is a possibility that hadn’t occurred to me before.  In math your results don’t just go belly-up because your pencil has a crack in its lead.
This is a practice where the real world will check your work for you.
Another project I’d been working on from Beginning Arduino was to connect temperature sensors.  The sensors I’d purchased for this were DS18B20 sensors. You get 10 for that price.  I worked through the book, and, having learned my lesson, I wired the boards painstakingly. I hooked it up and discovered it was 285 degrees centigrade in my house. (For you non-metric types, water boils at 100 degrees centigrade.)  I discovered my error when I looked further in the book and discovered the DS18B20 are a different type of sensor and must be wired in a different way and require a different library of functions to operate.  I hooked them up, and got the temperature down to 85 degrees centigrade.  I changed a resistor in my wiring and it came down to 74 Fahrenheit.
Like I said, the real world checks your work for you.