2009-11-03 25 views
5

Sto collegando un LilyPad Temperature sensor a un LilyPad Arduino 328 Main Board con l'obiettivo di leggere letture della temperatura ambiente abbastanza precise. Il sensore riceve energia e fornisce una risposta che riesco a leggere su seriale.Come ottenere temperatura ambiente dal sensore di temperatura Arduino Lilypad

Il problema che ho di fronte è che la lettura dal sensore mi sta dando inusuali, anche se numeri consistenti. Sto leggendo l'ingresso del sensore analogico e conversione in volt come questo ...

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); 
} 

questo produce una lettura costante di circa 1.1 Volt che la documentazione sensore indica sarebbe una temperatura ambiente di circa 60 gradi Celsius quando la la temperatura ambiente reale è di circa 23 gradi. Il sensore non è vicino in prossimità di qualsiasi altra elettronica, quindi non posso prevedere che questo sia il problema.

Il mio codice per la lettura del sensore è errato? Il mio sensore potrebbe essere difettoso?

risposta

7

Non è il lilypad un arduino 3,3 V, quindi significa che dovrebbe essere (3.3/1024.0), che sarebbe 0,726 V o 22,6 C?

0

In base a questo documentation, analogRead restituisce un numero intero. Hai provato a lanciare ad un galleggiante in questo modo:

therm = (float)analogRead(2); 

Che cosa la tensione del sensore letto su un voltmetro? La lettura cambia quando si modifica la temperatura del sensore? (Tenendo la mano su di esso dovrebbe essere sufficiente per cambiare la lettura.)

+1

si può tranquillamente lanciare int -> float in C (con una certa perdita di precisione). La risposta originale sarebbe utile, però. – FryGuy

3

Prova questo. Ho avuto esattamente lo stesso problem.read di più qui: 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 
} 
Problemi correlati