Arduino-Based Temperature Sensor

Arduino-Based Temperature Sensor Introduction This report presents the implementation of a temperature measurement system that incorporates the Arduin...
Author: Jasmin Tate
3 downloads 2 Views 1013KB Size
Arduino-Based Temperature Sensor Introduction This report presents the implementation of a temperature measurement system that incorporates the Arduino UNO microcontroller, simply referred to here as the Arduino. The first part of this project presented in this report was implemented using a temperature sensor and made use of two 74HC595 shift register integrated chips paired with LED’s to display the temperature. To accomplish this, the temperature sensor provided an analog input to the Arduino to then be processed by the pair of shift registers outputting to the LED’s. Arduino. Shift Registers, and LED’s The hardware for the temperature measurement system included a temperature sensor that attached to an analog output of the Arduino. The temperature sensor used with the Arduino is the LM35CZ temperature sensor. The circuit was wired as seen in Figure 1 below.

Figure 1. Arduino + Temp Sensor Configuration The console output displayed in Figure 2 below shows that the temperature sensor itself worked accurately, reading the current temperature in the office.

Figure 2. Console Display of Temperature Reading Using LC35CZ Figure 3 below shows the actual circuit with the temperature sensor wired correctly with the resistor. Adding the resistor kept the temperature sensor from overheating.

Figure 3. Actual Circuit + Temp Sensor + 10kΩ Resistor Adding Shift Registers Leads to Troubleshooting The next step was to add the two shift registers to the temperature sensor, Arduino, and wire the setup for the LED display. The article used was from Mahesh Venkitachalam’s blog titled Nuts About Electronics. Mahesh’s blog provided another link to an Arduino tutorial that described in detail how to wire and code the 74HC595 shift registers as well as describing how they function. Because a total of fourteen bits were needed to display the digits correctly on the LED’s, the shift registers could be used while only using up three output pins on the Arduino. Once the shift registers and LED’s were wired according to the Arduino ShiftOut examples, the code provided in Mahesh’s blog with the code from Daniel Andrade’s blog were merged together to convert the code at an attempt to display the temperature reading in both the console and the LED’s for troubleshooting. Mahesh’s code would have to be modified slightly. Upon uploading the code to the Arduino circuit, the seven digits on the LED’s would light up. This is seen in Figure 4 below. All the digits turned on and they were functional.

Figure 4. Arduino wired to Shift Registers and LED’s

Yλοποίηση από Ανδρέα Τσιγκόπουλο Χρησιμοποίησα 2xdigit 7-segment display κοινής καθόδου το οποίο ταιριάζει απόλυτα με τον κώδικα που δίνει ο συγγραφέας παρακάτω. Το manual 2xdigit 7-segment display (MAN6940 δίνεται στο αρχείο: 7-SEGMENT DISPLAYS MAN6940.pdf). Οι 2 shift registers που χρησιμοποιούνται είναι οι 74HC595 και το manual τους δίνεται στο αρχείο: 74HC_HCT595-1.pdf. Η αντιστοιχία των Pins για τον πρώτο (αριστερό) shift-register που συνδέεται στο αριστερό digit είναι: Q0 → DP (4) Q1 → a (16) Q2 → b (15) Q3 → c (3) Q4 → d (2) Q5 → e (1) Q6 → f (18) Q7 → g (17) Η αντιστοιχία των pins για τον δεύτερο (δεξιό) shift-register που συνδέεται στο δεξιό digit είναι: Q0 → DP (9) Q1 → a (11) Q2 → b (10) Q3 → c (8) Q4 → d (6) Q5 → e (5) Q6 → f (12) Q7 → g (7) Τα pins 13 και 14 του 2xdigit 7-segment display MAN6940 συνδέονται στη γη. Η έξοδος (pin 9) serial data output του πρώτου (αριστερού) shift register 74HC595 συνδέεται με την είσοδο (pin 14) serial data input του δεύτερου (δεξιού) shift register 74HC595. To pin 8 των 74HC595 στη γη. To pin 16 των 74HC595 στα 5V. To pin 13 των 74HC595 στη γη. To pin 10 των 74HC595 στα 5V. To pin 14 του αριστερού 74HC595 στο digital pin 11 του Arduino. To pin 11 του αριστερού 74HC595 στο digital pin 12 του Arduino και στο pin 11 του δεξιού 74HC595. To pin 12 του αριστερού 74HC595 στο digital pin 8 του Arduino και στο pin 12 του δεξιού 74HC595. Το pin 9 του δεξιού 74HC595 και τα pins 15 και των δύο 74HC595 είναι στον αέρα.

Παράλληλα με τους ακροδέκτες γης και τροφοδοσίας του αισθητήρα θερμοκρασίας LM35DZ συνέδεσα αντίσταση 10 kΩ, για να μην υπερθερμανθεί και καεί. Aυτά είναι τα links που χρησιμοποίησα: http://www.arduino.cc/en/Tutorial/ShiftOut http://www.danielandrade.net/2008/07/05/temperature-sensor-arduino/ http://electro-nut.blogspot.gr/2009/07/arduino-based-temperature-display.html Στην επόμενη σελίδα είναι ο κώδικας που τελικά δούλεψε. Υπάρχουν 8 διαφορετικές περιπτώσεις, τις οποίες εξέτασα και δίνουν τα εξής αποτελέσματα: 1) Κωδικοποίηση Michelle, ΜSBFIRST, dig[1] – dig[0] : δείχνει upsightdown, και σωστά το αποτέλεσμα. 2) Κωδικοποίηση Michelle, ΜSBFIRST, dig[0] – dig[1] : δείχνει upsightdown, και ανάποδα τη σειρά των ψηφίων, δηλαδή πρώτα το LSB και μετά το MSB. 3) Κωδικοποίηση Michelle, LSBFIRST, dig[1] – dig[0] : παράγει λάθος και ακατανόητα αποτελέσματα. 4) Κωδικοποίηση Michelle, LSBFIRST, dig[0] – dig[1] : παράγει λάθος και ακατανόητα αποτελέσματα. 5) Κωδικοποίηση Mahesh, LSBFIRST, dig[1] – dig[0] : δείχνει rightsideup, αλλά ανάποδα τη σειρά των ψηφίων, δηλαδή πρώτα το LSB και μετά το MSB. 6) Κωδικοποίηση Mahesh, LSBFIRST, dig[0] – dig[1] : δείχνει rightsideup, και σωστά το αποτέλεσμα. 7) Κωδικοποίηση Mahesh, ΜSBFIRST, dig[1] – dig[0] : παράγει λάθος και ακατανόητα αποτελέσματα. 8) Κωδικοποίηση Mahesh, ΜSBFIRST, dig[0] – dig[1] : παράγει λάθος και ακατανόητα αποτελέσματα. Άρα, οι μόνες σωστές υλοποιήσεις είναι οι: (1) και (6).

/* * TempDisplay * by MV - http://electro-nut.blogspot.com/ * * Displaying ambient temeprature on a 2 x Common Cathode 7-Segment Displays * Using 74HC595 shift register and the shiftOut() built in function * Using LM35 to sense temperature. * * REFERENCES * * http://www.arduino.cc/en/Tutorial/ShiftOut * */ // set this to 1 to run 99-0 countdown #define TEST_MODE 0 int val = 0; // variable to store the value coming from the sensor int lm35Pin = 0; int DEBUG = 1; //Pin connected to ST_CP of 74HC595 int latchPin = 8; //Pin connected to SH_CP of 74HC595 int clockPin = 12; ////Pin connected to DS of 74HC595 int dataPin = 11; // 7-segment digits 0-9 // 74HC595-->7-Segment pin Mapping: // {Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7} --> {DP, a, b, c, d, e, f, g} //Κωδικοποίηση σύμφωνα με Michelle Rivera //byte digits[ ] = { //B01111110, B01100000, B10110110, B11110010, B11101000, B11011010, //B11011110, B01110000, B11111110, B11111010}; //Κωδικοποίηση σύμφωνα με Mahesh Venkitachalam byte digits[] = { B01111110, B00110000, B01101101, B01111001, B00110011, B01011011, B01011111, B01110000, B01111111, B01111011}; // set up void setup() { if(DEBUG) { Serial.begin(9600); } //set pins to output because they are addressed in the main loop

pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); } // Main loop void loop() { #ifdef _TEST_MODE countDown(); #else // read the value from LM35. // read 10 values for averaging. val = 0; for(int i = 0; i < 10; i++) { val += analogRead(lm35Pin); delay(500); } // convert to temp: // temp value is in 0-1023 range // LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C // So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt float temp = val*50.0/1023.0; int tempInt = (int)temp; //float tempf = (temp * 9)/ 5 + 32;//converts to fahrenheit //int tempInt = (int)tempf; displayNum(tempInt); // serial output for debugging if(DEBUG) { Serial.println(temp); } #endif } // Display 2 digit number void displayNum(int num) { // get digit 0 int dig0 = num % 10; // get digit 1 int dig1 = (num/10) % 10; // shift out digits digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, LSBFIRST, digits[dig0]);

shiftOut(dataPin, clockPin, LSBFIRST, digits[dig1]); digitalWrite(latchPin, HIGH); } // test function to count down from 99 to 0 void countDown() { // display numbers for(int i = 0; i < 100; i++) { displayNum(100-i); delay(500); } }

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

Serial to Parallel Shifting-Out with a 74HC595 Started by Carlyn Maw and Tom Igoe Nov, 06

Shifting Out & the 595 chip At sometime or another you may run out of pins on your Arduino board and need to extend it with shift registers. This example is based on the 74HC595. The datasheet refers to the 74HC595 as an "8-bit serial-in, serial or parallel-out shift register with output latches; 3-state." In other words, you can use it to control 8 outputs at a time while only taking up a few pins on your microcontroller. You can link multiple registers together to extend your output even more. (Users may also wish to search for other driver chips with "595" or "596" in their part numbers, there are many. The STP16C596 for example will drive 16 LED's and eliminates the series resistors with built-in constant current sources.) How this all works is through something called "synchronous serial communication," i.e. you can pulse one pin up and down thereby communicating a data byte to the register bit by bit. It's by pulsing second pin, the clock pin, that you delineate between bits. This is in contrast to using the "asynchronous serial communication" of the Serial.begin() function which relies on the sender and the receiver to be set independently to an agreed upon specified data rate. Once the whole byte is transmitted to the register the HIGH or LOW messages held in each bit get parceled out to each of the individual output pins. This is the "parallel output" part, having all the pins do what you want them to do all at once. The "serial output" part of this component comes from its extra pin which can pass the serial information received from the microcontroller out again unchanged. This means you can transmit 16 bits in a row (2 bytes) and the first 8 will flow through the first register into the second register and be expressed there. You can learn to do that from the second example. "3 states" refers to the fact that you can set the output pins as either high, low or "high impedance." Unlike the HIGH and LOW states, you can"t set pins to their high impedance state individually. You can only set the whole chip together. This is a pretty specialized thing to do -- Think of an LED array that might need to be controlled by completely different microcontrollers depending on a specific mode setting built into your project. Neither example takes advantage of this feature and you won"t usually need to worry about getting a chip that has it. Here is a table explaining the pin-outs adapted from the Phillip's datasheet. PINS 1-7, 15 Q0 " Q7 Output Pins PIN 8

GND

Ground, Vss

PIN 9

Q7"

Serial Out

PIN 10

MR

Master Reclear, active low

PIN 11

SH_CP

Shift register clock pin

PIN 12

ST_CP

Storage register clock pin (latch pin)

PIN 13

OE

Output enable, active low

PIN 14

DS

Serial data input

PIN 16

Vcc

Positive supply voltage

Example 1: One Shift Register The first step is to extend your Arduino with one shift register. The Circuit 1. Turning it on Make the following connections:

• • • •

GND (pin 8) to ground, Vcc (pin 16) to 5V OE (pin 13) to ground MR (pin 10) to 5V This set up makes all of the output pins active and addressable all the time. The one flaw of this set up is that you end up with the lights turning on to their last state or something arbitrary every time you first power up the circuit before the program starts to run. You can get around this by controlling the MR and OE pins from your Arduino board too, but this way will work and leave you with more open pins.

2. Connect to Arduino

• • •

DS (pin 14) to Ardunio DigitalPin 11 (blue wire) SH_CP (pin 11) to to Ardunio DigitalPin 12 (yellow wire) ST_CP (pin 12) to Ardunio DigitalPin 8 (green wire) From now on those will be refered to as the dataPin, the clockPin and the latchPin respectively. Notice the 0.1"f capacitor on the latchPin, if you have some flicker when the latch pin pulses you can use a capacitor to even it out.

3. Add 8 LEDs. In this case you should connect the cathode (short pin) of each LED to a common ground, and the anode (long pin) of each LED to its respective shift register output pin. Using the shift register to supply power like this is called sourcing current.Some shift registers can't source current, they can only do what is called sinking current. If you have one of those it means you will have to flip the direction of the LEDs, putting the anodes directly to power and the cathodes (ground pins) to the shift register outputs. You should check the your specific datasheet if you aren"t using a 595 series chip. Don"t forget to add a 220-ohm resistor in series to protect the LEDs from being overloaded.

Circuit Diagram

The Code Here are three code examples. The first is just some "hello world" code that simply outputs a byte value from 0 to 255. The second program lights one LED at a time. The third cycles through an array.

595 Logic Table

595 Timing Diagram

The code is based on two pieces of information in the datasheet: the timing diagram and the logic table. The logic table is what tells you that basically everything important happens on an up beat. When the clockPin goes from low to high, the shift register reads the state of the data pin. As the data gets shifted in it is saved in an internal memory register. When the latchPin goes from low to high the sent data gets moved from the shift registers aforementioned memory register into the output pins, lighting the LEDs. Code Sample 1.1 Hello World Code Sample 1.2 One by One Code Sample 1.3 Two shift registers

Example 2 In this example you'll add a second shift register, doubling the number of output pins you have while still using the same number of pins from the Arduino. The Circuit 1. Add a second shift register. Starting from the previous example, you should put a second shift register on the board. It should have the same leads to power and ground.

2. Connect the 2 registers. Two of these connections simply extend the same clock and latch signal from the Arduino to the second shift register (yellow and green wires). The blue wire is going from the serial out pin (pin 9) of the first shift register to the serial data input (pin 14) of the second register.

3. Add a second set of LEDs. In this case I added green ones so when reading the code it is clear which byte is going to which set of LEDs

Circuit Diagram

The Code Here again are three code samples. If you are curious, you might want to try the samples from the first example with this circuit set up just to see what happens. Code Sample 2.1 Dual Binary Counters There is only one extra line of code compared to the first code sample from Example 1. It sends out a second byte. This forces the first shift register, the one directly attached to the Arduino, to pass the first byte sent through to the second register, lighting the green LEDs. The second byte will then show up on the red LEDs. Code Sample 2.2 2 Byte One By One Comparing this code to the similar code from Example 1 you see that a little bit more has had to change. The blinkAll() function has been changed to the blinkAll_2Bytes() function to reflect the fact that now there are 16 LEDs to control. Also, in version 1 the

pulsings of the latchPin were situated inside the subfunctions lightShiftPinA and lightShiftPinB(). Here they need to be moved back into the main loop to accommodate needing to run each subfunction twice in a row, once for the green LEDs and once for the red ones. Code Sample 2.3 - Dual Defined Arrays Like sample 2.2, sample 2.3 also takes advantage of the new blinkAll_2bytes() function. 2.3's big difference from sample 1.3 is only that instead of just a single variable called "data" and a single array called "dataArray" you have to have a dataRED, a dataGREEN, dataArrayRED, dataArrayGREEN defined up front. This means that line

data = dataArray[j]; becomes

dataRED = dataArrayRED[j]; dataGREEN = dataArrayGREEN[j]; and

shiftOut(dataPin, clockPin, data); becomes

shiftOut(dataPin, clockPin, dataGREEN); shiftOut(dataPin, clockPin, dataRED);

http://electro-nut.blogspot.gr/2009/07/arduino-based-temperature-display.html W

E

L

C

O

M

E

Namaste and Welcome to the "Nuts About Electronics" Blog. I am an electronics hobbyist based in India. Recently, I rediscovered my childhood passion in tinkering with electronic circuits. I created this blog to share some of my projects, as well as provide some useful information to other hobbyists in India, where many electronic components are hard to find. Please note that the projects as well as code in this blog are provided "as is", with no guarantees. Working with electricity, electronic components, soldering irons, etc. requires safety precautions - please use your common sense. Good luck with your projects, and above all, have FUN! MV

THU R SD AY,

JU LY

9,

2 0 0 9

Arduino based Temperature Display

This project is part of a bigger project of mine, and it displays the ambient temperature on a set of 2 Common Cathode 7-Segment LED displays. The temperature sensor is the LM35 IC, which outputs voltage calibrated to degrees centigrade.

The LM35 is connected to the analog input of the Arduino board. The code running on Arduino then averages 10 temperature readings to reduce jitter, and then outputs the 2 digit integer temperature value to the 7-Segment display. To achieve the latter, it uses a set of 2 shift registers (IC 74HC595). This is a technique that minimizes the number of output pins required by Arduino to drive the display. You can read about it in detail here:

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

Code

The following code shows how to read the analog input from the LM35 and use shiftOut() to send data to IC 74HC595. There is also an optional countDown() method here which runs down from 99 to 0, for testing.

/* * TempDisplay * by MV - http://electro-nut.blogspot.com/ * * Displaying ambient temeprature on a 2 x Common Cathode 7-Segment Displays * Using 74HC595 shift register and the shiftOut() built in function * Using LM35 to sense temperature. * * REFERENCES * * http://www.arduino.cc/en/Tutorial/ShiftOut * */

// set this to 1 to run 99-0 countdown #define TEST_MODE 0

int val = 0; int lm35Pin = 0; int DEBUG = 1;

// variable to store the value coming from the sensor

//Pin connected to ST_CP of 74HC595 int latchPin = 8; //Pin connected to SH_CP of 74HC595 int clockPin = 12; ////Pin connected to DS of 74HC595 int dataPin = 11;

// 7-segment digits 0-9 // 74HC595-->7-Segment pin Mapping: // {Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7} --> {DP, a, b, c, d, e, f, g} byte digits[] = { B01111110, B00110000, B01101101, B01111001, B00110011, B01011011, B01011111, B01110000, B01111111, B01111011 };

// set up void setup() { if(DEBUG) { Serial.begin(9600); }

//set pins to output because they are addressed in the main loop pinMode(latchPin, OUTPUT); pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT);

}

// Main loop void loop() {

#ifdef _TEST_MODE countDown(); #else

// read the value from LM35. // read 10 values for averaging. val = 0; for(int i = 0; i < 10; i++) { val += analogRead(lm35Pin); delay(500); }

// convert to temp: // temp value is in 0-1023 range // LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C // So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt float temp = val*50.0/1023.0; int tempInt = (int)temp; displayNum(tempInt);

// serial output for debugging if(DEBUG) { Serial.println(temp); } #endif

}

// Display 2 digit number void displayNum(int num) { // get digit 0 int dig0 = num % 10; // get digit 1 int dig1 = (num/10) % 10;

// shift out digits digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, digits[dig1]);

shiftOut(dataPin, clockPin, MSBFIRST, digits[dig0]); digitalWrite(latchPin, HIGH); }

// test function to count down from 99 to 0 void countDown() { // display numbers for(int i = 0; i < 100; i++) { displayNum(100-i); delay(500); } }

Conclusion

There is not much original here, the Arduino page on ShiftOut pretty much explains everything.

As you can see above, the temperature does change.(But don't try this and burn down your IC or worse, your house!)

References

For details on 74HC595 and shiftOut:

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

For details on LM35:

http://www.facstaff.bucknell.edu/mastascu/elessonshtml/Sensors/TempLM35.html POSTED BY MV AT 6:48 AM LABELS: 7-SEGMENT, ARDUINO, DISPLAY, ELECTRONICS, LM35, TEMPERATURE

http://www.danielandrade.net/2008/07/05/temperature-sensor-arduino/ ARDUINOELECTRONICSENGINEERINGHARDWAREHOWTO

Jul 5, 2008 DanielAndrade 112 Comments

Temperature Sensor + Arduino Hello people, it’s been a while since I have posted projects on this website. This semester was really busy, I didn’t have time to much else, but soon I will have my winter holiday (Here in south our summer holiday is from December to February).

• • •

Today I am going to show you how to build a simple temperature sensor using one LM35 Precision Temperature Sensor and Arduino, so you can hookup on your future projects. The circuit will send serial information about the temperature so you can use on your computer, change the code as you will. I’m planning to build a temperature sensor with max/min + clock + LCD, and when I get it done, I will post here. Parts: Arduino (You can use other microcontroller, but then you will need to change the code). LM35 Precision Centigrade Temperature Sensor, you can get from any electronic store. Here is theDATA SHEET. BreadBoard Assembling: This is a quick and simple step. Just connect the 5V output from arduino to the 1st pin of the sensor, ground the 3rd pin and the 2nd one, you connect to the 0 Analog Input. Down goes some pictures that may help you, click to enlarge:

Here is the Arduino Code, just upload it and check the Serial Communication Option. You can also download the .pde HERE.

/* An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis (cc) by Daniel Spillere Andrade , http://www.danielandrade.net http://creativecommons.org/license/cc-gpl */

int pin = 0; // analog pin int tempc = 0,tempf=0; // temperature variables int samples[8]; // variables to make a better precision int maxi = -100,mini = 100; // to start max/min temperature int i;

void setup() { Serial.begin(9600); // start serial communication }

void loop() {

for(i = 0;i< =7;i++){ // gets 8 samples of temperature

samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0; tempc = tempc + samples[i]; delay(1000);

}

tempc = tempc/8.0; // better precision tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit

if(tempc > maxi) {maxi = tempc;} // set max temperature if(tempc < mini) {mini = tempc;} // set min temperature

Serial.print(tempc,DEC); Serial.print(" Celsius, ");

Serial.print(tempf,DEC); Serial.print(" fahrenheit -> ");

Serial.print(maxi,DEC); Serial.print(" Max, "); Serial.print(mini,DEC); Serial.println(" Min");

tempc = 0;

delay(1000); // delay before loop }