Go Down

Topic: LM35 problem (Read 1 time) previous topic - next topic


I am using a LM35 to monitor the temperature in my room and have run into a problem.  When I open the sketch in IDE and open the serial monitor the Arduino outputs the correct temp., 22 deg. C, but when I run this
Code: [Select]
    //import Serial communication library
    import processing.serial.*;

    //init variables
    Serial commPort;
    float tempC;
    float tempF;
    int yDist;
    PFont font12;
    PFont font24;
    float[] tempHistory = new float[100];

    void setup()
      //setup fonts for use throughout the application
      font12 = loadFont("Verdana-12.vlw");
      font24 = loadFont("Verdana-24.vlw");
      //set the size of the window
      size(210, 200);
      //init serial communication port
      commPort = new Serial(this, "COM17", 9600);
      //fill tempHistory with default temps
      for(int index = 0; index<100; index++)
        tempHistory[index] = 0;

    void draw()
      //get the temp from the serial port
      while (commPort.available() > 0)
        tempC = commPort.read();
        //refresh the background to clear old data

        //draw the temp rectangle
        colorMode(RGB, 160);  //use color mode sized for fading
        stroke (0);
        rect (49,19,22,162);
        //fade red and blue within the rectangle
        for (int colorIndex = 0; colorIndex <= 160; colorIndex++)
          stroke(160 - colorIndex, 0, colorIndex);
          line(50, colorIndex + 20, 70, colorIndex + 20);
        //draw graph
        for (int index = 0; index<100; index++)
          if(index == 99)
            tempHistory[index] = tempC;
            tempHistory[index] = tempHistory[index + 1];
          point(90 + index, 180 - tempHistory[index]);
        //write reference values
        text("212 F", 45, 25);
        text("32 F", 45, 187);
        //draw triangle pointer
        yDist = int(160 - (160 * (tempC * 0.01)));
        triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
        //write the temp in C and F
        text(str(int(tempC)) + " C", 115, 37);
        tempF = ((tempC*9)/5) + 32;
        text(str(int(tempF)) + " F", 115, 65);

in processing, my temp returns as 50 degrees C. The sensor works fine, when I hold it the temp. rises, and is correct in the serial monitor, but is 28 deg off in processing.
What is going on?


Sep 13, 2013, 08:26 pm Last Edit: Sep 13, 2013, 08:32 pm by jonrobertd Reason: 1
Never mind I fixed it by changing this
Code: [Select]
text(str(int(tempC)) + " C", 115, 37);
       tempF = ((tempC*9)/5) + 32;
       text(str(int(tempF)) + " F", 115, 65);

to this
Code: [Select]
text(str(int(tempC-28)) + " C", 115, 37);
       tempF = (((tempC-28)*9)/5) + 32;
       text(str(int(tempF)) + " F", 115, 65);

Edit: Now when it reaches 84F 29C it drops instantly to 68F 20C and stays instead of going any higher.


Did you know that the 5V of the Arduino is used as a reference when reading the analog inputs ? If the 5V changes to 4.5V, the analogRead returns a higher value.
You can avoid that by using a power supply (or wall wart), or by using the internal voltage reference.

To solve the problem, use Serial.println() to print the raw adc value of analogRead() to the serial monitor. If that is a valid and good value, you might change the way you convert the strings.


Sep 15, 2013, 04:22 pm Last Edit: Sep 15, 2013, 05:05 pm by pito Reason: 1
tempC = commPort.read();

read() returns a number between 0 and 255 for the next byte that's waiting in the buffer..

So you will not get your temperature in the integer or float format.. You will get the first byte coming from the serial only..

..my temp returns as 50 degrees C

You got the number 50 - in the ASCII it means "2" - that is the first char (first byte) of your temperature sent as string "22"  ;)

You may try (maybe it works with processing) provided your temperature has been sent as an integer:

Code: [Select]
if (commPort.available()>0) {
tempC = (float)(commPort.parseInt());


Code: [Select]
if (commPort.available()>0) {
tempC = commPort.parseFloat();

when sent as a float.


Sep 16, 2013, 12:00 am Last Edit: Sep 16, 2013, 12:02 am by jonrobertd Reason: 1
Everything works fine in serial monitor, and fine in processing using the -28 formula, but when it hits that temp, it drops. How do I use that bit of code to see if it fixes it. I am onty using other peoples coding to learn what to do and have really not got any clue as to what I am doing.

It still shows the right temp in serial monitor after processing drops it,  keeps rising  to 30, 31, ect while nothing in processing.

Go Up