Devices & Components
LM35 Temperature sensing module
Arduino Uno Case - Blue
Software & Tools
Arduino IDE
Project description
Code
LM35 sensor reading
cpp
The code uses an algorithm to smooth out readings
1const int sensor=A5; // Assigning analog pin A5 to variable 'sensor' 2int tempc=0; //variable to store temperature in degree Celsius 3int delayread=100; //delay of 0.1 secs between readings 4float vout=0; //temporary variable initialise 5float voutread=0; //temporary variable to hold sensor reading 6float voutmax; //temporary variable to hold max sensor reading 7float voutmin; //temporary variable to hold min sensor reading 8 9void setup() { 10pinMode(sensor,INPUT); // Configuring sensor pin as input 11Serial.begin(9600); 12} 13 14// LM35 is very sensitive so an "electric inertia" is applied before its output. 15// For this, 100 readings are taking at the "delay time" interval and 16// then the average between max and min is used as output 17 18void loop() { 19startcycle: 20voutmax=0; 21voutmin=1024; 22for (int i=1; i<=100;i++) // 100 readings in total 23{ 24 voutread=analogRead(sensor); //Reading the value from sensor 25 if (voutread>voutmax) voutmax=voutread; // Checking max value 26 if (voutread<voutmin) voutmin=voutread; // Checking min value 27 delay(delayread); // Delay time before taking the next reading 28} 29 30vout=(voutmax+voutmin)/2; 31vout=(vout*5/1023); // transpose to voltage (V) 32vout=(vout*1000/10); // transpose to temperature since 1oC = 10mV (for LM35) 33tempc=round(vout); // Storing value in Degree Celsius 34Serial.print("Temp="); 35Serial.print("\t"); 36Serial.print(tempc); // Print out the value 37Serial.print("\t"); 38Serial.print("oC"); 39Serial.println(); 40 41// The procedure starts over again 42goto startcycle; 43}
Comments
Only logged in users can leave comments