Pages: [1]   Go Down
Author Topic: help please... temperature sensor nearly finished  (Read 777 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am doing a project for my uni, i am making a temperature sensor using the arduino uno r3 board....

I have got a temperature sensor LM35, TO-92 (casing), when i use it on terminal I get the following result, 143, when i put my hand it reaches like 160, but what is this? how do i turn it to celcius? please help my code is:

  int val1 = 0;
  int count = 0;
  float tempC;
  int tempPin = 0;
  void setup(){ 
  Serial.begin(19200);    //initializes serial port with a baudrate of 19200bps (same as XBee chip)
}

// function to send data

void sendData(int id,int num, int data1){
 
  Serial.print("@");
  Serial.print(id);
  Serial.print("|");
 
  Serial.print(num);
 
 
  Serial.print("|data1-");
  Serial.print(data1);
 
 
   
  Serial.println("#r");      // end of message

}

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);
 
  while (count <= 10000){
   
    val1 = analogRead(sens1);
   
   
    sendData(3,count, val1);

    delay(5000);
     count++;
    } 
 
  count = 0;

}
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 486
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The first analog read in your loop does the conversion(tempC = 5.0*tepC..), but you then have a while loop later in your loop and that one doesn't go through the conversion on each analog read as it prints out the value.(just sends the analog value straight to the send function)

also, please use code bracket things
Code:
etc.
Logged

http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

 float tempC;

tempC = analogRead(tempPin);           //read the value from the sensor
tempC = (5.0 * tempC * 100.0)/1024.0;  //convert the analog data to temperature


analogRead() return an interger. Not a float.

do this :

Code:

tempC = float( analogRead( tempPin ) );


Try that.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 221
Posts: 13848
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


please use the # button when posting code, it provides [ code] tags so the code looks better.
Also use the CTRL-T in the IDE to do an auto layout

here a refactor of your code
Code:
int val1 = 0;
int count = 0;
float tempC = 0;
int tempPin = 0;

void setup()
{
  Serial.begin(19200);    // initializes serial port same as XBee chip
}

void sendData(int id, int num, int data1)
{
  Serial.print("@");
  Serial.print(id);
  Serial.print("|");
  Serial.print(num);
  Serial.print("|data1-");
  Serial.print(data1);
  Serial.println("#r");      // end of message
}

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
 
  sendData(3, count, tempC);
  delay(5000);
 
  count++;
  if (count == 10000) count = 0;
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26488
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know why you've got all that stuff about printing results every five seconds; if I were you, I'd have a dead simple test sketch getting and printing the temperature continuously to the serial monitor.
Once you've fixed that, then add all the other junk.

Code:
const int tempPin = 0;

void setup()
{
  Serial.begin(19200);
}

void loop()
{
  Serial.println (readTemp (tempPin), 1);
  // Optional delay of say 100 milliseconds.
}

float readTemp (const int sensorPin)
{
  float tempC = analogRead(sensorPin);               //read the value from the sensor
  // Serial.println (tempC, 0);  // optional print of the raw value
  return (5.0 * tempC * 100.0)/1024.0;   //convert the analog data to temperature
}
« Last Edit: April 11, 2012, 02:19:45 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: