Friday, July 26, 2013

Building an Arduino Powered BBQ Thermometer

A guest post by , who is a Developer Advocate for , a developer, speaker, trainer, screen-caster and much more. Derick blogs at , produces screencasts at and tweets as .



In the post, you walked through the basics of getting an Uno R3 prototyping platform up and running with and the framework. After configuring Arduino with the "StandardFirmata" sketch, you wrote code with JavaScript and NodeJS, running various versions of LED configurations. This showed several of the core principles of both electronics and software development for the Arduino platform. Having a few blinking LEDs with a button to press, though, isn't the most exciting thing in the world. So now, in this post, it is time to tackle something fun: a food grade thermometer to monitor the temperature of a steak (or tofu or whatever else) on a grill, and light up an LED when it's done!




THE PARTS LIST



Before you get rolling, you will need a small parts list, in addition to what you purchased for the last article. If you don't already have an Arduino Uno and a general assortment of jumper wires, buttons, and LEDs, you should refer to the for that list.



Additional items needed are:



* 10K Ohm or 100K Ohm food grade thermometer

* 10K Ohm or 100K Ohm, 1/4 watt resistor

* 3/32audio input jack (mono or stereo)

* Alligator clamp connectors

* Food Grade Thermometer



The food grade thermometer that you want can be found at any grocery store or hardware store with grills. I purchased the cheapest, store-bought food thermometer with a 3-foot-long cord that plugs into the side of a base unit. Unplug the thermometer probe (called a "thermistor" - a "thermal resistor") from the base and you can use it in your project.



For this project to work, you must know if it is a 10K Ohm or 100K Ohm thermistor probe. This will inform you of the secondary resistor that you need to use in your circuit. If you have a 10K thermistor, you will need a 10K resistor. If you have a 100K thermistor, you will need a 100K resistor (this post will assume a 10K Ohm thermistor).



3/32AUDIO INPUT



The connector used for the thermistor probe is most likely going to be a 3/32audio input jack. It does not matter if the audio jack is mono or stereo: 2 wires or 3. Your thermistor should be 2 wire, but a 3 wire input jack will work just fine. You'll simply ignore the 3rd connector on the input jack.



You will be connecting this to your breadboard with jumper wires and alligator clamps to create stability, while being able to move the input jack around. You'll see why later, when you hook everything up.



THERMISTOR CONFIGURATION: VOLTAGE DIVIDER



Think back to the pull-down resistor configuration for the button, in the previous post. It looks something like this, as a schematic:



The configuration for the thermistor is going to look the same, except for the use of a thermistor instead of a button.



This small configuration changes the circuit from a pull-down resistor to a voltage divider circuit. If you'd like to know more, do a little research on your own to see how a voltage divider works. It involves Ohm's law, and knowing the input voltage and both of the resistor values.



DESIGN PATTERNS: VOLTAGE DIVIDER VERSUS PULL-DOWN RESISTOR



If the difference between the button circuit and thermistor circuit is just those two parts, then what is the difference between a pull-down resistor and a voltage divider? Nothing, other than the button versus thermistor. A button with a pull-down resistor can be calculated as a voltage divider circuit. The difference is that the button either provides 0 Ohm or infinite Ohm resistance, while the thermistor provides variable resistance through temperature changes.



Much like software design patterns, circuit design patterns are often differentiated by intent or use, and not by implementation.



CONNECTING THE THERMISTOR PROBE AND LED



For this configuration, make sure that you have a red wire running from the Arduino power (5v) to the red line of the breadboard. Also use a black wire to run from the Arduino ground (gnd) to the breadboard's blue line. This will let you power any number of components, and ground any number of circuits, on the breadboard.



RESISTOR PROTECTED LED



Now add a simple resistor protected LED circuit, similar to what you built last time. Use a 330K Ohm or 1K Ohm resistor, to create a nice bright LED. Connect Arduino digital pin 8 to the input of the resistor circuit. The pin number doesn't really matter, as long as it's a digital pin. Just remember what pin you chose, so the software can match. Then connect the output of the circuit to the breadboard's ground line.



VOLTAGE DIVIDING A THERMISTOR



Set up the same configuration for the thermistor that you had used for the button and pull-down resistor. Only in this case, connect a red and black jumper wire where the button leads were. On the unconnected ends of the jumper wires, use your alligator clamps to connect the two leads on the back of the 3/32audio input jack (the extra set of jumper wires and alligator clamps are used because if you tried to plug the audio jack directly in to the breadboard, it wouldn't fit).



Run a red wire from the power line of the breadboard to the input line of the thermistor. Then connect a line out of the thermistor's output to analog pin 0 of the Arduino. Connect another wire from the same thermistor output to the thermistor's matching resistor. Then connect the resistor to the ground line of the breadboard.



In diagram form, it looks like this:



And the actual breadboard and Arduino setup should look something like this:



The signal line of the thermistor is connected to an analog input on the Arduino because you will need to read the actual voltage from the line. If you plugged it into a digital input pin, you would only get a 1 or 0 for the signal - on or off. It would behave like an oddly designed pull-down resistor. By using an analog input, you will get variations in the input signal - the actual voltage coming out of the thermistor's signal line.



LET THE CODE FLOW!



With the breadboard configured and everything plugged in to the Arduino Uno, it's time to write some code, do some math, and determine when your steak is done! With SCIENCE!



READING THE THERMISTOR; CALCULATING TEMPERATURE



Remember the first version of the blinking LED code, and how it used a loop on the board? You're going to be pulling that back off the shelf for the thermistor. In this case, you will read the current voltage from an analog input pin and then run a calculation on it to determine the temperature. This will be done using a Pin object from Johnny-Five, and a little bit of math, triggered on a loop.



Start with the basic configuration of the board and pins for the Arduino, a loop to get the current thermistor voltage and a call to calculate the current temperature.



var j5 = require("johnny-five"); var board = new j5.Board(); var LEDPIN = 8; var THMPIN = "A0"; board.on("ready", function(){ var led = new j5.Led(LEDPIN); var thm = new j5.Sensor({ pin: THMPIN, freq: 500 }); var currentTemp; thm.on("change", function(err, thmVoltage){ currentTemp = convertVoltToTemp(thmVoltage); // do stuff w/ the temperature, here }); });



The "sensor" provides its own loop to check for changes. In this case, it is run every half second (500 milliseconds), because there isn't much need to do it more often than that. In fact, you wouldn't have any problem if you changed this to 1 or 2 seconds. It is nice to see temperature change a little more often than that, when testing, though.



Now you'll need a convertVoltToTemp function that can convert the voltage in to a temperature. In this case, the return value will be a structure that contains Fahrenheit, Celsius, and Kelvin temperatures. That way you can use the temperature scale you are most comfortable with, or all of them if you want.



function convertVoltToTemp(volt){ var tempK, tempC, tempF; // get the Kelvin temperature tempK = Math.log(((10240000/volt) - 10000)); tempK = 1 / (0.001129148 + (0.000234125 * tempK) + (0.0000000876741 * tempK * tempK * tempK)); // convert to Celsius and round to 1 decimal place tempC = tempK - 273.15; tempC = Math.round(tempC*10)/10; // get the Fahrenheit temperature, rounded tempF = (tempC * 1.8) + 32; tempF = Math.round(tempF*10)/10; // return all three temperature scales return { tempK: tempK, tempC: tempC, tempF: tempF }; }



For a complete and detailed understanding of these calculations, see the WikiPedia .



LIGHT UP THE LED



With the calculation to convert voltage into temperature in place, you can use this information to determine when your food is cooked!



Go back to the board "ready" callback and set up a simple check to turn the LED on if the temperature is greater than or equal to 135 degrees Fahrenheit. This is the "prime" temperature for a medium-rare steak, according to the USDA.



// ... existing variables, methods, etc. board.on("ready", function(){ var led = new j5.Led(LEDPIN); var thm = new j5.Sensor({ pin: THMPIN, freq: 500 }); var alertTemperatureF = 135; var currentTemp; thm.on("change", function(err, thmVoltage){ currentTemp = convertVoltToTemp(thmVoltage); if (currentTemp.tempF >= alertTemperatureF) { led.on(); } else { led.off(); } console.log("Current TempF: ", currentTemp.tempF); }); });



TEST YOUR NEW THERMOMETER



The code you just wrote requires the temperature to get well over the point where it would be difficult to test without actually cooking a steak. For now, though, it will be easier to lower this temperature to something that can be tested at your desk.



For example, if you lower the alertTemperatureF to 90, then you can check whether or not you are generating a temperature above 90 degrees:



var alertTemperatureF = 90;



PLUG IT IN AND TEST IT



Plug the thermistor probe in to the input jack. Plug the Arduino in to your computer, and run the code:



node thermistor.js



The LED should be off when the code first starts up, with the actual temperature being logged to the console window:



Now take the thermistor probe, having cleaned it so that it does not have any old food or anything else on it, and hold it tightly between two fingers. The tip is where the temperature is read, so be sure it is firmly in your fingers. The console window should start logging an increase in temperature. Within a few seconds, it should be showing something above 90 degrees and the light will turn on:



INACCURATE, BUT CONSISTENT



Don't worry if your thermistor doesn't report 98.6 degrees when measuring yourself. It likely won't, but that's okay. Your store bought food thermometer won't, either.



If you disconnect your Arduino, unplug the thermistor probe and then plug it back in to your store bought thermometer base - the store bought version should show the same temperature as your Arduino version. I've done this multiple times and verified the accuracy of my code, each time. I've even purchased additional digital food thermometers from the store and verified that all of them produce the same result as my Arduino version.



The thermistor you are using is designed to be more accurate at temperatures where food safety is important. But, the thermistor will be consistent above all else. As long as your connections to the breadboard, input jack and Arduino are good, and the thermistor itself is clean and in good condition, you will continue to get the same temperature reading from the thermistor for years to come.



CHALLENGES: SOLDER A PERFBOARD, BUILD A WEB APP



You now have an Arduino powered food thermometer, worthy of any BBQ or cooking station. Well, almost worthy of that. If you really want to use this for some cooking, you'll want to do a few things to make it stand up to real use:



* Solder the circuits to a perfboard

* Build a rocking mobile website so you can monitor the temperatures from your phone, and not just your computer or the LED

* Put it in a project box



A "perfboard" is a circuit board with hundreds of holes drilled in it, sized for electronics components. Buy a handful of boards, a soldering iron and practice soldering wires in to it before you solder actual components.



For the web application: you're already using NodeJS, the hipster's web server. Don't stop with Johnny-Five. Checkout and . See if you can get a web page to show you the current temperature, reported through a websocket.



Once you have it working the way you want, buy a small plastic project box and mount the parts in it. This will help protect the parts, prevent short circuits from things accidentally touching solder joints and leads, and keep everything safe from food splatters while cooking.



SAFARI BOOKS ONLINE HAS THE CONTENT YOU NEED



These books in Safari Books Online will help you enhance your Arduino project:



gives you lots of ideas for Arduino projects and helps you get started with them right away. From getting organized to putting the final touches on your prototype, all the information you need is right in the book.



helps you create your own toys, remote controllers, alarms, detectors, robots, and many other projects with the Arduino device.



In teaches by using an amazing set of 50 cool projects. You'll progress from a complete beginner regarding Arduino programming and electronics knowledge to intermediate skills and the confidence to create your own amazing Arduino projects.



will show you how to use your Arduino to control a variety of different robots, while providing step-by-step instructions on the entire robot building process.



ABOUT THIS AUTHOR



is a Developer Advocate for , a developer, speaker, trainer, screen-caster and much more. He's been slinging code since the late 80's and doing it professionally since the mid 90 s. These days, Derick spends his time primarily writing JavaScript with back-end languages of all types, including Ruby, NodeJS, .NET and more. Derick blogs at , produces screencasts at , tweets as and provides support and assistance for JavaScript, BackboneJS, MarionetteJS and much more around the web.
Full Post

No comments:

Post a Comment