Skip to main content

Arduino Experiments: LM35 and Graph plotting

HELLO THERE!!!


     How are you guys? I hope everyone is fine always. This week, I am going to share to you guys about an experiment with LM35 (temperature sensor). Our main objectives are:

1. Obtain temperature value using LM35
2. Plot graphs from the value using Arduino Serial plotter and Processing

Ok, before we begin we need to know what hardware and software are used in this experiment.

Hardware:
1.Arduino UNO or Leonardo( I am using Leonardo)

2.LM35 temperature sensor

Software:
1. Arduino IDE ( Version 1.6.8)
2.Processing IDE


After obtaining the hardware and the software, we will assemble the circuit. The circuit assembly is pretty simple (use LM35 image above as reference):




The schematics of the circuit


Now for the Arduino code to read the temperature sensor:

int Temp = A0;//sensor output pin at A0
int val;



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

void loop() {
 val = (5.0* analogRead(Temp)*100.0)/1024;//equation that is used to produce temperature value
 Serial.println(val);//serial print the temperature value
 delay(100);//a little delay

}


Upload the code above to the Arduino. For graph plotting, you guys can either use the Arduino serial plotter or use the processing IDE to plot the graph. There also other ways to plot graphs from Arduino such as using makerplot, python(instrumentino), and Matlab. For the simplest graph plotting, you can use Arduino serial plotter. If you don't have Arduino serial plotter in your IDE, I would recommend you guys to download the newer version of Arduino:

1.Open Up the serial plotter (tool ---> Serial plotter)

2. And your graphs will be plotted for you!!!!



You can also Processing IDE to plot a simple graph. Here is the code that is used to plot graph from Arduino output:

import processing.serial.*;

Serial myPort;        // The serial port
int xPos = 1;         // horizontal position of the graph
float inByte = 0;

void setup () {
  // set the window size:
  size(800, 500);

  // List all the available serial ports
  // if using Processing 2.1 or later, use Serial.printArray()
  println(Serial.list());

  // Open whatever port is the one you're using.
   myPort = new Serial(this, "COM16", 9600);

  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');

  // set inital background:
  background(255,255,255);
}
void draw () {
  // draw the line:
  stroke(255, 0, 0);//rgb
  line(xPos, height-250, xPos, height-250 - inByte);// you can adjust the 250 into 0 or any value
                                                        //it will adjust vertical position of graph
  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    background(255,255,255);//background color
  } else {
    // increment the horizontal position:
    xPos++;
  }
}


void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    inByte = float(inString);
    println(inByte);
    inByte = map(inByte, 0, 1023, 0, height);
  }
}


The output is shown below:
The value of the output received is constant (31 degrees) That is why the graph looks constant. Notice a small peak in the graph, that is when the temperature value rises to 32 degrees.



Allright folks!! That's all from me, there are many ways on upgrading or modifying the code. Maybe you guys can use bluetooth to send data to smartphone and plot the graph using MIT app inventor, or log the graph values to a .csv file and plot the graph in Excel or you can also send the .csv file to MySQl and SQlite. All I want to say is see you guys next week and HAPPY TINKERING!!!











Comments