2009-11-03 40 views
5

Tôi đang kết nối một LilyPad Temperature sensor với một số LilyPad Arduino 328 Main Board với mục tiêu đọc các chỉ số nhiệt độ môi trường xung quanh khá chính xác. Cảm biến đang nhận điện và đưa ra một câu trả lời tôi có thể đọc qua nối tiếp.Cách nhận Nhiệt độ môi trường từ Cảm biến nhiệt độ Arduino Lilypad

Vấn đề mà tôi phải đối mặt là việc đọc từ cảm biến đang khiến tôi rất bất thường - mặc dù số lượng nhất quán. Tôi đang đọc đầu vào cảm biến analog và chuyển sang volt như thế này ...

loop(){ 
    float therm; 
    therm = analogRead(2); // Read from sensor through Analog 2 
    therm *= (5.0/1024.0); // 5 volts/1024 units of analog resolution 
    delay(100); 
} 

Điều này mang lại một đọc nhất quán của khoảng 1,1 Volts mà tài liệu cảm biến chỉ sẽ là một môi trường xung quanh nhiệt độ khoảng 60 độ C khi nhiệt độ môi trường xung quanh thực sự là khoảng 23 độ. Cảm biến không gần với bất kỳ thiết bị điện tử nào khác nên tôi không thể thấy trước đó là vấn đề.

Mã của tôi có đọc được cảm biến không chính xác không? Cảm biến của tôi có bị lỗi không?

Trả lời

7

Không phải là lilypad một Arduino 3.3V, vì vậy có nghĩa là nó phải là (3.3/1024.0), mà sẽ là 0,726V, hoặc 22,6 C?

0

Theo điều này documentation, analogRead trả về một số nguyên. Bạn đã thử đúc nó lên phao như vậy:

therm = (float)analogRead(2); 

Điện áp cảm biến đọc trên vôn kế? Liệu việc đọc thay đổi khi bạn thay đổi nhiệt độ của cảm biến? (Giữ bàn tay của bạn trên nó là đủ để thay đổi đọc.)

+1

bạn có thể an toàn cast int -> float in c (với một số tổn thất chính xác). Câu trả lời ban đầu sẽ hữu ích, mặc dù. – FryGuy

3

Hãy thử điều này. Tôi đã có chính xác cùng một vấn đề. Đọc thêm tại đây: http://www.ladyada.net/learn/sensors/tmp36.html

//TMP36 Pin Variables 
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to 
         //the resolution is 10 mV/degree centigrade with a 
         //500 mV offset to allow for negative temperatures 

#define BANDGAPREF 14 // special indicator that we want to measure the bandgap 

/* 
* setup() - this function runs once when you turn your Arduino on 
* We initialize the serial connection with the computer 
*/ 
void setup() 
{ 
    Serial.begin(9600); //Start the serial connection with the computer 
         //to view the result open the serial monitor 
    delay(500); 
} 

void loop()      // run over and over again 
{ 
    // get voltage reading from the secret internal 1.05V reference 
    int refReading = analogRead(BANDGAPREF); 
    Serial.println(refReading); 

    // now calculate our power supply voltage from the known 1.05 volt reading 
    float supplyvoltage = (1.05 * 1024)/refReading; 
    Serial.print(supplyvoltage); Serial.println("V power supply"); 

    //getting the voltage reading from the temperature sensor 
    int reading = analogRead(sensorPin); 

    // converting that reading to voltage 
    float voltage = reading * supplyvoltage/1024; 

    // print out the voltage 
    Serial.print(voltage); Serial.println(" volts"); 

    // now print out the temperature 
    float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset 
               //to degrees ((volatge - 500mV) times 100) 
    Serial.print(temperatureC); Serial.println(" degress C"); 

    // now convert to Fahrenheight 
    float temperatureF = (temperatureC * 9/5) + 32; 
    Serial.print(temperatureF); Serial.println(" degress F"); 

    delay(1000);          //waiting a second 
} 
Các vấn đề liên quan