Your TMP 36 will need to be wired up as shown (with the change to 3.3 V supply voltage) in the image on the main lab page but otherwise that's it.
Step 1: Upload the Code (and Save As!):
Here's the code for exploring the TMP36. You will notice that it uses the serial monitor to communicate with you and reports the temperature in C and F. I was very tempted to put the wrong conversion formula in the code and make you figure it out from the plot above. Decided not to but you should convince yourself that the graph above and the code in the sketch say the same thing.
/* TMP36 Test This is initial test code for learning how to read analog data from a pin connected to some external device. In this case the extenal device is a TMP36 temperature sensor. We will use the capability of the Arduino to read the actual voltage (called an analogRead) on one of the special purpose pins labelled A0 - A5. Look at the documentation for analogRead the Arduino website for deeper insight. Basically the analogRead reports a value between 0 and 1024 covering the range between 0V and 5 V (some Arduini run on 3.3 V and for those the range is 0 V to 3.3V). The analogRead() value needs to be converted to actual volts first and then to temperature according to the characteristics of your TMP sensor. Many thanks to Adafruit from which the framework for this sketch was taken and for their fabulous work over the years to support the Arduino community. Original Adafruit modified 7/6/20 Bruce Emerson */ // TMP36 Pin Variables -- see data sheet for pin assignments on the // sensor itself. Here we must define the analog pin on the Arduino // that is connected to the output pin of the TMP36 int sensorPin = A0; //the analog pin the TMP36's Vout (sense) pin is connected to //the resolution is 10 mV / degree centigrade with a //500 mV offset to allow for negative temperatures int pinLED = 13; /* * setup() - this function runs once when you turn your Arduino on * We initialize the serial connection with the computer */ void setup(){ Serial.begin(9600); //Start the serial connection with the computer //to view the result open the serial monitor while (!Serial) { ; // wait for serial port to connect. Needed for native USB } Serial.println(); Serial.println("The Serial Monitor is ready for business!"); Serial.println(); // tell the pins what mode they are in. LED is an OUTPUT and sensorPin is an INPUT pinMode(sensorPin, INPUT); pinMode(pinLED, OUTPUT); delay(2000); } void loop() { // run over and over again // getting the voltage reading from the temperature sensor // the reading is a value between 0 and 1024. // 1024 represents HIGH on the pin which is usually 5V on an Uno int sensorOut = analogRead(sensorPin); // converting that sensorOut to a voltage float voltage = sensorOut * (5.0/1024); // print out the voltage read by Uno Serial.print(voltage); Serial.println(" volts"); // now determine the temperature from plot on datasheet float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset //to degrees ((voltage - 500mV) times 100) Serial.print(temperatureC); Serial.println(" degrees C"); // now convert to Fahrenheit float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0; Serial.print(temperatureF); Serial.println(" degrees F"); Serial.println(); delay(5000); //waiting 5s }
Step 2: Learning
The various setup and operational commands for the serial monitor should be clear from your earlier explorations. You hopefully also remember the pinMode() command from your Blinky lab. In this lab we introduce the INPUT pinMode as well as the analog pins on the Arduino.
pinMode([pin number],[INPUT/OUTPUT]): Any pin can be used as either an output or an input. It can be changed back and forth during your sketch although it usually isn't. In this lab we need to receive information from the temperature sensor so we need an appropriate pin to be configured as an input. Notice where it is in the sketch.
Analog and Digital pins: As you have seen on your Arduino there are two sets of pins on the board. 0-13 on one side and A0-A5 on the other side. 0-13 are digital pins which means they have only two possible states - HIGH (about 5 V) or LOW (0 V). Some of these digital pins are capable of being controlled as PWM (Pulse Wide Modulated) pins means we have a way to switch them back and forth really fast in different ways. More on that in the future. For now what is important is that the digital pins are either HIGH or LOW. If they are inputs the input is limited to the same two choices. The analog pins (A0-A5) on the other hand can distinguish a full range of values from 0 V to 5 V with a resolution of about 5 mV. The analog pins A0- A5 are also numbered 14-19 as in A0 = 14...A5 = 19. Because the output of the temperature sensor (TMP36) is a voltage between 0 and 5 volts we need to connect that output to an analog pin to be able to detect the voltage with useful resolution. Notice in the sketch that 'sensorPin = A0'. Do you understand why now?
analogRead([pin number]): This is the command (function) we use to read the voltage that is connected to an analog pin. This is a command which goes out to the analog pin and brings back (called 'returns') the value of the voltage. In the sketch when I say 'sensorOut = analogRead(sensorPin)' the value that is 'returned' by the command is stored in the varible that is set equal to it, in this case the variable sensorOut. The definition of the type of that variable we will discuss next.
float or int: When we create a variable name we need to let the Arduino IDE know what sort of information we will be storing in this variable. Numerical information comes in one of two general categories. Numbers which might be decimals are called 'float' data for floating point. Number which will always be integers are identified as 'int'. If you give the sketch an input which is 2 it will be stored as 2.0 in a float variable and 2 in an int variable. If you give the sketch an input which is 1.34 it will be stored as 1.34 in a float variable and 1 in an int variable. Whew!!
Step 3: Testing
You can watch the temperature change as you hold onto the TMP36 and raise it up to body temp (do you know what that is in C? -- you should know this!) You can hold something cold to it like a beverage and watch the temperature drop.The TMP36 is NOT particularly sensitive. I can often hold it for a long time and it never registers a normal body temperature. If I hold the sensor on something from my freezer it doesn't get as low as I would expect. Don't worry about this as long as the temperature changes in the right direction.
For now if you want to watch the temperature you have to carry around your computer so the Arduino can talk to the serial monitor. In the long run we will write the data to an SD card which we can read later to get the data. Consider what to change if you want to take data at relatively long intervals of time.