Posts | Comments

Planet Arduino

Archive for the ‘TMP36’ Category

Img_0055When working in extreme temperatures it is important to monitor your body temperature. In freezing weather, your fingers and toes get numb and you can develop frostbite without even noticing it. Likewise, if your core body temperature drops too low, you can start to start to suffer the effects of […]

Read more on MAKE

Apr
14

Getting Started with Arduino! – Chapter Two

arduino, Arduino Tutorial, education, LCD, learning electronics, microcontrollers, thermometer, TMP36 Commenti disabilitati su Getting Started with Arduino! – Chapter Two 

This is part of a series titled “Getting Started with Arduino!” – A tutorial on the Arduino microcontrollers, to be read with the book “Getting Started with Arduino” (Massimo Banzi). The first chapter is here.

Welcome back fellow arduidans!

I hope you have been enjoying these posts and learning and creating and making many things. If not, you soon should be! :)

Today’s post has several things: taking temperatures, sending data from the arduino back to your PC, opening a library, using LCD screens, and some exercises that will help to consolidate your previous knowledge and help revise it.

First of all, we shall investigate another analogue sensor – a temperature sensor. As you can imagine, there are many applications for such a thing, from just telling you the current temperature, to an arduino-based thermostat control system. And it is much easier to create than most people would think – so get ready to impress some people!

Let’s say hello to the Analog Devices TMP36 Low-voltage temperature sensor


Tiny, isn’t it? It looks just like a standard transitor (e.g. a BC548) due the use of the same TO-92 case style. The TMP36 is very simple in its use – looking at the flat face, pin 1 is for +5V supply (you can connect this to the 5V socket on your arduino), pin 2 is the output voltage (the reading), and pin three is ground/earth (connect this to the GND socket on your arduino). Furthermore, have a look at the data sheet, it is quite easy to read and informative. TMP36 data sheet

The TMP36 will return a voltage that is proportional to temperature. 10 mV for each degree Celsius, with a range of -40 to 125 degrees.

There isn’t any need for extra resistors or other components – this sensor must be the easiest to use out there. However there is one situation that requires some slight complexity – remote positioning of the sensor. It is all very well to have the sensor on your breadboard, but you might want it out the window, in your basement wine cellar, or in your chicken coop out back… As the voltage output from the sensor is quite low, it is susceptible to outside interference and signal loss. Therefore a small circuit needs to be with the sensor, and shielded cable between that circuit and the home base. For more information on long runs, see page eight of the data sheet.

At this point we’re going to have some mathematics come into the lesson – sorry about that. Looking again at page eight of the data sheet, it describes the output characteristics of the sensor. With our TMP36, the output voltages increases 10 millivolts for every degree Celsius increase; and that the output voltage for 25 degrees Celsius is 750 mV; and that there is an offset voltage of 500 mV. The offset voltage needs to be subtracted from the analogRead() result, however it is not without vain – having the offset voltage allows the sensor to return readings of below freezing without us having to fool about with negative numbers.

Quick note: A new type of variable. Up until now we have been using int for integers (whole numbers)… but now it is time for real numbers! These are floating point decimals, and in your sketches they are defined as float.

Now we already know how to measure an analogue voltage with our arduino using analogRead(), but we need to convert that figure into a meaningful result. Let’s look at that now…

analogRead() returns a value between 0 and 1023 – which relates to a voltage between 0 and 5V (5000 mV). It is easier on the mind to convert that to a voltage first, then manipulate it into temperature. So, our raw analogRead() result from the TMP36 multiplied by (5000/1024) will return the actual voltage [we're working in millivolts, not volts] from the sensor. Now it’s easy – subtract 500 to remove the offset voltage; then divide it by 10 [remember that the output is 10 mV per degree Celsius]. Bingo! Then we have a result in degrees Celsius.

If you live in the Fahrenheit part of the world, you need to multiply the Celsius value by 1.8 and add 32.

Quick note: You may have seen in earlier sketches that we sent a value to the serial output in order for the arduino software to display it in the serial monitor box. Please note that you cannot use digital pins 0 or 1 if using serial commands. We used Serial.begin(9600); in the void setup(); part of the sketch. You can also send text to the serial monitor, using Serial.print(); and Serial.println();. For example, the following commands:

Serial.print(“The temperature is: “);

Serial.print(temperature, 2);

Serial.println(” degrees Celsius”);

would create the following line in the serial monitor (assuming the value of temperature is 23.73):

The temperature is 23.73 degrees Celsius

and send a carriage return to start a new line. That is the difference between the Serial.print(); and Serial.println(); commands, the extra -ln creates a carriage return (that is, sends the cursor to the start of the next line. Did you notice the 2 in the Serial.print(); above? You can specify the number of decimal places for float variables; or if you are using an integer, you can specify the base of the number being displayed, such as DEC, HEX, OCT, BIN, BYTE – decimal, hexadecimal, octal, binary, or byte form. If you don’t use the second paramater of Serial.print();, it defaults to decimal numbers for integers, or two decimal places for floating-point variables.

Now let’s read some temperatures! All you need to do is connect the TMP36 up to the arduino board. pin 1 to 5v, pin 2 to analog 0, pin 3 to GND. Here is a shot of the board setup:


And here is the sketch:

/*
example 2.1 – digital thermometer
Created 14/04/2010 —  By John Boxall — http://tronixstuff.wordpress.com —  CC by-sa v3.0
Uses an Analog Devices TMP36 temperature sensor to measure temperature and output values to the serial connection
Pin 1 of TMP36 to Arduino 5V power socket
Pin 2 of TMP36 to Arduino analog 0 socket
Pin 3 of TMP36 to Arduino GND socket
*/
void setup()
{
Serial.begin(9600);   // activate the serial output connection
}
float voltage = 0; // setup some variables
float sensor = 0;
float celsius = 0;
float fahrenheit = 0;

void loop()
{              // let’s get measurin’
sensor = analogRead(0);
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-500;        // remove voltage offset
celsius = voltage/10;         // convert millivolts to Celsius
fahrenheit = ((celsius * 1.8)+32); // convert celsius to fahrenheit
Serial.print(“Temperature: “);
Serial.print(celsius,2);
Serial.println(” degrees C”);
Serial.print(“Temperature: “);
Serial.print(fahrenheit,2);
Serial.println(” degrees F”);
Serial.println(“_ _ _ _ _ _ _ _ _ _ _ _ _ _  ”);
delay (1000); // wait a second, otherwise the serial monitor box will be too difficult to read
}

And there’s nothing like a video, so here it is. The measurements start at room temperature, then an ice cube in a plastic bag is pushed against the TMP36 for a moment, them held between two fingers to warm up again…


Quick note: the while() command. Sometimes you want a sketch to wait for user input, or wait for a sensor to reach a certain state without the sketch moving forward. The solution to this problem is the use of the while() command. It can cause a section of a sketch to loop around until the expression in the while command becomes true.
For example:

while (digitalRead(3) == LOW)
{
Serial.writeln(“Button on digital pin 3 has not been pressed”);
}

Anyhow, back to the next exercise – it’s now your turn to make something!

Exercise 2.1

Recently the cost of energy has spiralled, and we need to be more frugal with our heating and cooling devices. Unfortunately some members of the family like to use the air conditioner or central heating when it is not really necessary; many arguments are caused by the need for the heating/cooling to be on or off. So we need an adjudicator that can sit on the lounge room shelf and tell us whether it’s ok to use the heating or cooling.

So, create a device  that tells us when it is ok to use the air conditioner, heater, or everything is fine. Perhaps three LEDs, or a single RGB LED. Red for turn on the heat, blue for turn on the air conditioner, and green or white for “You’re fine, you don’t need anything on”. You will need to define your own temperature levels. Living here in north-east Australia, I’m going to have the air conditioner on above 28 degrees C; and the heat can come on at 15 degrees C.

Hopefully you are thinking about the voltmeter we made in chapter one, that should give you a starting point. If not, go back now and have a look. Otherwise, hop to it…
Anyway, here is my board layout…
You will need:
  • Your standard Arduino setup (computer, cable, Duemilanove)
  • Either three LEDs or an RGB LED
  • 3 x 390 ohm 0.25 W resistors. They are to reduce the current to protect the LEDs.
  • a breadboard and some connecting wire
  • a camera (optional) – to document your success!
And a sketch to solve the exercise:
/*
exercise 2.1 – Climate Control Judge
Created 14/04/2010 —  By John Boxall — http://tronixstuff.wordpress.com —  CC by-sa v3.0 Share the love!
Measures temperature with Analog Devices TMP36 and compares against minimum temperature to use a heater or air conditioner
*/
int redLED = 13; // define which colour LED is in which digital output
int greenLED = 12;
int blueLED = 11;
float voltage = 0; // set up some variables for temperature work
float sensor = 0;
float celsius = 0;
float heaterOn = 15; // it’s ok to turn on the heater if the temperature is below this value
float airconOn = 26; // it’s ok to turn on the air conditioner if the temperature is above this value
void setup()
{
pinMode(redLED, OUTPUT);    // set the digital pins for LEDs to outputs
pinMode(greenLED, OUTPUT);  // not necessary for analogue input pin
pinMode(blueLED, OUTPUT);
}
void loop()
{
digitalWrite(redLED, LOW);    // switch off the LEDs
digitalWrite(greenLED, LOW);
digitalWrite(blueLED, LOW);
// read the temperature sensor and convert the result to degrees Celsius
sensor = analogRead(0);       // TMP36 sensor output pin is connected to Arduino analogue pin 0
voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
voltage = voltage-500;        // remove voltage offset
celsius = voltage/10;         // convert millivolts to Celsius
// now decide if it is too hot or cold.
if (celsius>=airconOn)
{
digitalWrite(blueLED, HIGH); // ok to turn on the air conditioner
} else if (celsius<=heaterOn)
{
digitalWrite(redLED, HIGH);
} else
{
digitalWrite(greenLED, HIGH); // everything normal
}
delay(1000); // necessary to hold reading, otherwise the sketch runs too quickly and doesn’t give the LEDs enough time to
// power up before shutting them down again
}
And of course a video. For demonstration purposes, I have altered the values by making them very close, so it’s easier to show you the LEDs changing. The plastic bag used in the video is full of ice water.

Well that was interesting, I hope you enjoyed it and have some ideas on how to put temperature sensors to use. But now it is time to look at a library or two…
Quick note: As you know, there are several commands in the processing language to control your arduino and its thoughts. However, as time  goes on, people can write more commands and make them available for other arduino users. They do this by creating the software that allows the actual Atmel microcontroller interpret the author’s new commands. And these commands are contained in libraries. Furthermore, as Arduino is open-source, you can write your own libraries and publish them for the world (or keep ‘em to yourself…) In the arduino IDE software, have a look at the Sketch>>Import Library… menu option. Also, check out here for more libraries! Now to put one to good use…

Now it is time to look at liquid crystal displays! (Finally…)

As you are aware, LCDs are everywhere, in almost anything electronic that requires user-input or information display. You could even be looking at a really large one right now! But in the world of electronics enthusiasts, they have often been somewhat difficult to use. Until now! Once more the arduino makes it simple to use a variety of LCDs in your projects.

However, to keep it simple at this stage, you need to use an LCD that has a parallel interface and a Hitachi HD44780 controller (or 100% compatible) inbuilt. Don’t panic, these are very common, however you may find some shady sellers on eBay who will sell you something else, so keep an eye out (not literally). Identifying such LCDs is easy, as they almost always have the same type of interface. Normally they will have a row of 16 holes, clearly labelled. In the example below, I have soldered in some header pins to make it easier to use with a breadboard. If you are going to solder some pins in, don’t rest the LCD on the pins – they will be crooked. Instead, put the pins into a breadboard, then sit the LCD on top of the pins. Anyway, here’s our LCD:



To help identify the pinouts on your LCD and other useful facts, it is always wise to consult the data sheet. Furthermore, never buy an LCD if you cannot get a data sheet - you might think “Oh, I don’t really need one”, but unless you already have the same one you will need that sheet. One day. For example, like this:


In the examples for this chapter, you will notice that my LCD doesn’t have a back light. Yes, I’m a cheapskate. However many LCDs do, so take note about how to use their back light. In the near future I will update this chapter using an LCD with a back light. Subscribe to the email list (top right) to keep in touch. But now, back to the plan…
Firstly, hardware. Let’s connect our LCD to the arduino, using the following: LCD pin >>> Arduino pin (You may want to use a common ground on the breadboard then connect that to the Arduino)
  • 1 (ground) to GND
  • 2  (supply voltage) to 5V
  • 4 (RS) to digital 7
  • 5 (RW) to GND
  • 6 (E) to digital 8
  • 14 (DB7) to digital 12
  • 13  (DB6) to digital 11
  • 12 (DB5) to digital 10
  • 11 (DB4) to digital 9
  • connect a 10k linear potentiometer – the outer pins to +5V and ground, the centre pin to LCD pin 3. This is the contrast adjustment

We don’t use LCD pins  7~10, not in this case 15 or 16 – the back light pins.

Take note of the pin configuration above, as we need to know which LCD pin connects to which arduino pin. We tell the arduino this using the LiquidCrystal lcd(a, b, c, d, e, f); command. So in out wiring example above, we would use LiquidCrystal lcd(7, 8, 9, 10, 11, 12);. Here is a photo of our example board layout:

The difficult part is the wiring, the easy part is the programming. Once you have setup the pins using the LiquidCrystal lcd(); command, you can happily write to or control the LCD. The rest of the LCD commands are quite simple, so I will leave you to examine them on your own using the example hardware setup with the commands at arduino.cc/en/Reference/LiquidCrystal. But for the meanwhile, here is an example sketch for you:

/*

Example 2.2 – our first LCD sketch

Created 14/04/2010 —  By John Boxall — http://tronixstuff.wordpress.com —  CC by-sa v3.0

Just send some data to the LCD to experiment with it

*/

#include <LiquidCrystal.h> // we need this library for the LCD commands

// initialize the library with the numbers of the interface pins

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

float noisy = 0;

void setup()

{

lcd.begin(16, 2);             // need to specify how many columns and rows are in the LCD unit

lcd.println(“tronixstuff!    ”);

lcd.setCursor(0,1);

delay(2000);

lcd.clear();

randomSeed(analogRead(0)); // reseed the random number generator with some noise

}

void loop()

{

noisy=random(1000);

lcd.setCursor(0,0);

lcd.print(“Random Numbers!”);

lcd.setCursor(0,1);

lcd.print(“Number: “);

lcd.print(noisy,0);

delay(1000);

}

And now of course a video clip of example 2.2:

Now, it’s break time once more. When you return, we have…
Exercise 2.2

This is our most complex project to date, but don’t let that put you off. You have learned and practised all the commands and hardware required for this exercise. You only need a little imagination and some time. Your task is to build a digital thermometer, with LCD readout. As well as displaying the current temperature, it can also remember and display on request the  minimum and maximum temperatures – all of which can be reset. Furthermore, the thermometer works in degrees C or F.
First of all, don’t worry about your hardware or sketch. Have a think about the flow of the operation, that is, what do you want it to do? For a start, it needs to constantly read the temperature from our TMP36 sensor. You can do that. Each reading will need to be compared against a minimum and maximum value. That’s just some decision-making and basic maths. You can do that. The user will need to press some buttons to display and reset stored data. You can do that – it’s just taking action if a digital input is high. I will leave the rest up to you.
So off you go!

You will need (this may vary depending on your design, but this is what we used):

  • Your standard Arduino setup (computer, cable, Duemilanove)
  • Water (you need to stay hydrated)
  • 2 x 10k 0.25 W resistors. They work with the buttons to the arduino
  • Analog Devices TMP36 temperature sensor (Farnell part number 143-8760)
  • 2 little push buttons
  • breadboard and some connecting wire
  • 16×2 character LCD module and a 10k linear potentiometer or trimpot (For LCD contrast)
  • a camera (optional) – to document your success!

For inspiration, here is a photo of my board layout:




and a video clip of the digital thermometer in action.

And here is the sketch for my example – Exercise 2.2 sketch example
Wow. Congratulations to all those who took part and built something useful!

Please subscribe (see the top right of this page) to receive notifications of new articles. If you have any questions at all please leave a comment (below). If you would like to showcase your work from this article, email a picture or a link to john at tronixstuff dot com. You might even win a prize.

Don’t forget to check out the interesting and cool Arduino gear from Little Bird Electronics.


So have fun, stay safe and see you soon for our next instalment!




  • Newsletter

    Sign up for the PlanetArduino Newsletter, which delivers the most popular articles via e-mail to your inbox every week. Just fill in the information below and submit.

  • Like Us on Facebook