2013年4月5日 星期五

Arduino LM35 溫度感測器



        LM35 是很常用且易用的溫度感測器元件,在應用上也只需要一個LM35元件,只利用一個類比端口就可以,困難點在於將讀取的類比值轉換為實際的溫度。


溫度感測器LM35

LM35是由National Semiconductor所生產的溫度感測器,其輸出電壓與攝氏溫標呈線性關係,轉換公式如式(1)0度C 時輸出為0V,每升高1度C,輸出電壓增加10mVLM35有多種不同封裝型式,外觀如圖1所示。在常溫下,LM35不需要額外的校準處理即可達到 + - 1/4度C的準確率。其電源供應模式有單電源與正負雙電源兩種,其接腳如圖2所示,正負雙電源的供電模式可提供負溫度的量測;兩種接法的靜默電流-溫度關係如圖3所示,單電源模式在25度C下靜默電流約50mA,非常省電。

                                             

2.     實際測試

接下來實際對LM35進行測試,測試使用單電源模式,並且將輸出以非反相放大器放大十倍,如圖4的電路。以10Hz的頻率記錄放大後的電壓值,得到如圖5的溫度-時間圖。





LM35 Temperature Sensor:
LM35 is a Three-Pins (Vcc,Output,GND) high precision temperature sensor having a resolution of 10mV/C starting at 0V (i.e. an output of 0V represents a temperature of 0C).
So,
10mV ---> 1C
20mV ---> 2C
370mV ---> 37.0C and so on.

Converting ADC Reading to Celsius degrees:
Knowing that our ADC has a step size of 4.883mV, converting our digital reading back to voltage is simply done by multiplying the digital reading by the step size:
Vin (in Volts) = DigitalReading * 0.004883
Now, knowing our sensor's sensitivity is 10mV/C, converting this voltage to Celsius is simply done by dividing the input voltage by 0.01, So:
Temperature (C) = Vin/0.01 = DigitalReading * 0.4883




int potPin = 0; //定義類比端口0 連接LM35 溫度感測器


void setup()
{
Serial.begin(9600);//設置Baud rate
}


void loop()
{
   int val;//定義變數
   int dat;//定義變數

   val=analogRead(0);// 讀取感測器的模擬值並傳值給val

   dat=(125*val)>>8;   //溫度計算公式     125/2^8 = 125/256=0.4882812

  Serial.print("Celsius Temperature:");  //輸出顯示Tep 字串代表溫度
  Serial.print(dat);          //輸出顯示dat 的值
  Serial.println("C");    //輸出顯示C 字符串
  delay(500);//延時0.5
}





下載完程序打開searial monitor視窗就可以看見目前的溫度了。


// 另一方法    5*100/1024=0.4428125 



//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

// 5*100/1024=0.4428125 
Serial.print((byte)tempC); //send the data to the computer delay(1000); //wait one second before sending new data }

3 則留言:

  1. 請問我想將溫度顯示在兩個七段顯示器上
    程式碼該如何修改??

    回覆刪除
  2. 我想請問一下 我的也是LM35而接法也完全相同 但是數據顯示卻不正確 把LM35拔掉還是一樣會跳數據 可以請問是什麼問題嗎?

    回覆刪除

Node-Red & ModeBus FC=1

Node-Red & ModeBus FC=1 write address=0..7   8bits read address=16..23 8bits  [{"id":"cf7c1253d4d997dd","type&q...