Saturday, January 16, 2010

Practical Arduino

My copy of Practical Arduino came in yesterday, hot off the press from Amazon. This book really lives up to its title with a host of projects including a USB virtual keyboard, speech synthesizer, temperature and water level monitoring, and the final, most ambitious project, a car monitoring system.
Each project clearly lists the required materials and the code provided is clearly written, well documented, and modular. Each project can be easily expanded beyond what is described in the book, and the clear explanations of how each project works certainly will help build confidence for the relative newbie when they set out to adapt the projects to their own needs. The modular aspect of each project is particularly appealing, especially for the more complex projects in the book. For example, the car monitoring system is comprised of several modules including a GPS, an ODB-II interface, an LCD display and a 4 buttons for a user interface. Each module could easily be implemented on its own for use in other projects.
This book might not be the best for an absolute beginner, it does assume basic familiarity with the Arduino system. If you've worked your way through Getting Started with Arduino you should be able to handle the projects in Practical Arduino. If you have basic programming and electronics experience you may be able to jump straight into Practical Arduino, it has a well written section on basic electronics titled "Electronics for Programmers" and clear descriptions of techniques such as charlie-plexing.
This is a very well put together book full of unique projects. If you've wanted to use an Arduino for home automation, monitoring your car, to build a weather station, or to interface with a computer this book will be a great resource!

Saturday, January 2, 2010

Arduino and the LM335A temperature sensor.

I've been playing around with the LM335A temperature sensor. I chose it over the one-wire sensors strictly due to cost - it's only $1.50. The first project was connecting it to the arduino and converting the output into a valid temperature. I used a 2.2K resistor and connected it using the simple temperature sensor schematic in the datasheet. My first results were a little screwy - I knew it was not 700 kelvin in my office. Turns out I was reading the sensor wrong, I was expecting the raw value to be the temp in kelvin but it is not. You have to do a conversion.

The analog to digital converter in the Arduino basically divides the reference voltage (5 volts) into 1024 "steps" from 0-1023. This is the number that you get when you read from an analog pin. To convert this number into the voltage you need to do a little math - input value / 1023 * 5. That's the input value, divided by the max value, and multiplied by the reference voltage. Simple, but it took some time to track that tidbit down.

To convert the voltage into kelvin for the LM335A you simply multiply by 100. The LM335A outputs 10mv for each unit kelvin, so 296 kelvin is 2.96 volts. Simple. To convert to celsius subtract 273. So degrees celsius = kelvin - 273. To convert to farenheit multiply the celsius temp by 1.8 and add 32.

At the moment my code simply reads the value from analog pin 0, does the conversions, and outputs the results to the serial port.

// temp probe
// Jonathan Smith 2009
// simple program that reads the value from a LM335A, converts to
// kelvin, celcius, and farenheit, and outputs the result.

int TempPIN = 0; // analog pin LM335A is connected to
int LedPIN = 13; // in case we want to flash an LED
float TempVAL = 0; // holds the temp val, we need this to be a float

void setup(){
Serial.begin(9600); // set baud rate
}

void loop(){
TempVAL = analogRead(TempPIN); // get value from LM335A
Serial.print("Raw Value: ");
Serial.println(TempVAL);
Serial.print("Kelvin: ");
TempVAL = (((TempVAL / 1023) * 5) * 100); // convert value to kelvin
Serial.println(TempVAL);
Serial.print("Celsius: ");
TempVAL = (TempVAL - 273); // convert value to celsius
Serial.println(TempVAL);
Serial.print("Farenheit: ");
TempVAL = (TempVAL * 1.8 + 32); // convert value to farenheit
Serial.println(TempVAL);
delay(1000);
}


For phase 2 I built the simple adjustment circuit in the data sheet using a 330 ohm resistor and a 10K ohm potentiometer. I used a cheap radio shack trimmer/potentiometer, I'd recommend a better trimmer, it's hard to adjust the values precisely on the radio shack one. Using a larger resistor would probably have helped as well, like the 2.2K ohm I was originally using.

Even though it's less than ideal, I slapped the circuit together on a scrap of project board. I was tired of building the circuit and tearing it back down on a breadboard, this way I have a temperature module ready to plug in whenever I want it.



This has been a very simple project, but useful. In addition to a functional temperature sensor, I also have a better idea how to interface sensors using the analog ports. Next up is a light-level sensor using a CdS photocell. I'm also trying to get my hands on a pre-paid cell phone that I can use to send SMS messages, the ultimate goal is to build a environmental data logging device that will record several different types of data including temperature and location (from a GPS unit) and log it to an SD card and transmit it to a twitter account via SMS. I may have to move up to an arduino mega, eventually, but at the moment I am building and testing one sub-system at a time, so my little Diecimila has been sufficient.