2013年4月5日 星期五

Arduino LM35 Sensor


源自
http://pscmpf.blogspot.tw/2008/12/arduino-lm35-sensor.html

tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature

My son Paul and I recently finished a project using the Arduino Diecimila microcontroller in conjunction with the Processing open source programming environment to monitor temperature.

The project contains 3 parts:
1. The Arduino board with sensor circuit.
2. The Arduino program.
3. The Processing program.

The Arduino Board with Sensor Circuit
The Arduino circuit board is connected to the LM35 Celsius Temperature sensor. Here is a picture of the project circuit with illustrated wires connected to the temperature sensor.


We used the on board power source (5v and Gnd) to power the LM35 and analog pin 0 (zero) to read the analog output from the sensor. Here's a picture of the circuit wired on a breadboard.



The LM35 temperature sensor's pin-out and package information is as follows:


The Arduino Program

The open-source Arduino environment allows us to write code and load it onto the Arduino board's memory. The development environment is written in Java and based on Processing, avr-gcc, and other open source software.

The Arduino code loops every second to read output from the LM35, converting the analog output into Celsius and sending the data to the computer via a serial communication connection (USB).

Here's the code used to run the Arduino board:


//declare variables
float tempC;
int tempPin = 0;

void setup()
{
Serial.begin(9600); //opens serial port, sets data rate to 9600 bps
}

void loop()
{
tempC = analogRead(tempPin);           //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature
Serial.print((byte)tempC);             //send the data to the computer
delay(1000);                           //wait one second before sending new data
}
The Processing Program
The software client portion of this project runs on a PC and is written in Processing. Processing is a language and development environment similar to Arduino and designed for creating visual effects programs. We used Processing to create a small client that can read the serial data from the Arduino board and display the temperature on a slider and in both Celsius and Fahrenheit. We also added a rolling 100 data point graph to display historical temperature data. Here's a screen shot of the Processing application:


Here is the code used for the visual portion of the project:


//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, "COM10", 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
    background(123);

    //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
    stroke(0);
    fill(255,255,255);
    rect(90,80,100,100);
    for (int index = 0; index<100; index++)
    {  
      if(index == 99)
        tempHistory[index] = tempC;
      else
        tempHistory[index] = tempHistory[index + 1];
      point(90 + index, 180 - tempHistory[index]); 
    }
  
    //write reference values
    fill(0,0,0);
    textFont(font12); 
    textAlign(RIGHT);
    text("212 F", 45, 25); 
    text("32 F", 45, 187);
  
    //draw triangle pointer
    yDist = int(160 - (160 * (tempC * 0.01)));
    stroke(0);
    triangle(75, yDist + 20, 85, yDist + 15, 85, yDist + 25);
  
    //write the temp in C and F
    fill(0,0,0);
    textFont(font24); 
    textAlign(LEFT);
    text(str(int(tempC)) + " C", 115, 37);
    tempF = ((tempC*9)/5) + 32;
    text(str(int(tempF)) + " F", 115, 65);
  }
}
End.


下載 Processing 



Download Processing. Processing is available for Linux, Mac OS X, and Windows. Select your choice below to download the software.

THE Processing SOFTWARE IS PROVIDED TO YOU "AS IS," AND WE MAKE NO EXPRESS OR IMPLIED WARRANTIES WHATSOEVER WITH RESPECT TO ITS FUNCTIONALITY, OPERABILITY, OR USE, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR INFRINGEMENT. WE EXPRESSLY DISCLAIM ANY LIABILITY WHATSOEVER FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR SPECIAL DAMAGES, INCLUDING, WITHOUT LIMITATION, LOST REVENUES, LOST PROFITS, LOSSES RESULTING FROM BUSINESS INTERRUPTION OR LOSS OF DATA, REGARDLESS OF THE FORM OF ACTION OR LEGAL THEORY UNDER WHICH THE LIABILITY MAY BE ASSERTED, EVEN IF ADVISED OF THE POSSIBILITY OR LIKELIHOOD OF SUCH DAMAGES.
By downloading the software from this page, you agree to the specified terms.

Download 2.0 Beta 8 (24 February 2013)

» Windows 64-bit
» Windows 32-bit
» Linux 64-bit
» Linux 32-bit
» Mac OS X
 
The list of revisions covers the differences between releases in detail. Please read the changes if you're new to the 2.0 series. Also check the known problems for this release.
Processing is Open Source Software. The PDE (Processing Development Environment) is released under the GNU GPL (General Public License). The export libraries (also known as 'core') are released under the GNU LGPL (Lesser General Public License). There's more information about Processing and Open Source in the FAQ and more information about theGNU GPL and GNU LGPL at opensource.org. Please contribute to Processing!
Resources
Announcements
If you are interested in receiving updates about Processing, submit your email through this form.Your email will only be used to send infrequent updates about Processing. It will not be sold or shared.
Pre-Releases
The 2.0 pre-releases contain significant changes, be sure to read about them. Note that Android mode no longer works in Processing 1.5, you'll need to use an alpha or beta release to do Android development.
2.0b8 | 2013 02 24 Windows 32-bit | Windows 64-bit | Mac OS X | Linux 32-bit | Linux 64-bit | more fixes
2.0b7 | 2012 12 07 Windows 32-bit | Windows 64-bit | Mac OS X | Linux 32-bit | Linux 64-bit |475,382 bug fixes
Stable Releases
1.5.1 | 2011 05 15 Standard or without Java | Mac OS X | Linux x86 | fix regressions in 1.5
Earlier releases have been removed because we can only support the current versions of the software. To update old code, read the changes page. Per-release changes can be found inrevisions.txt. If you have problems with the current release, please file a bug so that we can fix it. Older releases can also be built from the source.

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...