Wednesday, 6 May 2015

Arduino Tips & Tricks

Understanding Sensing & Interfacing Temperature Sensor with Arduino.

Sensors are very important part of electronic systems. A sensor is a transducer whose purpose is to sense /detect some  some characteristics of its surroundings and provide a suitable corresponding output in the form of  electrical signal.
Example: Blood Sugar Detection Machine
There is a commonly available blood glucose measurement machine which is used by the patients. For measuring the blood sugar first you  prick the skin and put it on the strip that comes with the machine,there is a protein on  the strip which when  comes in contact with the blood sugar produces electrons and hence a voltage is created which is then read by the machine,the machine is pre calibrated to give a corresponding reading. 
Different sensors may have different ways of sensing a same characteristic. In a general  in any sensing device we make use of change in inductance,capacitance or resistance to get the desired output,however the technology used in the sensor affects some important features of a sensor like response time,accuracy,repeatability,lifetime. Power consumption is another important factor that needs to be taken care of while selecting a sensor,because lesser power consumption will give more up time.
ADC
When we talk about the sensors the next thing without which we cannot live are the ADC. These convert the analog voltage into digital voltage. There are many types of ADC available  like Counter type ADC,Parallel comparator type ADC(this is the fastest ),Successive approximation type ADC, Dual slope integrated type ADC. Each one of above have its pros and cons. The atmega328P on the arduino have a 10bit sussesive approximation type ADC. I am not going to go into the details of how it works,you can go here  if you want to read more.

CALIBRATION
This is the next buzz word you will here while dealing with the sensors,right calibration of sensor is very important for accurate measurement,different sensors  may require different ways for calibration,some sensors also come precalibrated.
 As a hobbiest it might not be super critical thing and we can do away with it at times  but on more important areas and in serious implementations like in bio medical instrumentation (like the example above)it is very important to have right calibration. 

HANDS ON 
Interfacing LM35
LM35 has a measurement range of -55*c to 150 *c. It is commonly available sensor that you can find for a price range between 35 to 45 INR. Below is a picture with its pin description.


STEP 1:
Connect the sensor to arduino as below.


STEP 2:
writing the software:
we have to read the analog value at the pin A5 of the arduino and the multiply it with a magic number
int val;
int tempPin = A5;
float reading,temperature
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  val = analogRead(tempPin);
  reading= (val*4.88)
  temperature= reading/10;
  Serial.print("TEMPRATURE = ");
  Serial.print(temperature);
  delay(1000);

}
The magic is in these two lines below but where did these lines come from? Lets understand
  reading= (val*4.88)
  temperature= reading/10;
The ADC on the arduino gets a 5v as reference if you dont change it 
First thing we have done here is we have calculated the step size
Stepsize= Vref/2^n     n=no of bits  here we have a 10bit ADC on arduino.
stepsize= 5/1024 =0.0048828 = 4.88mV

from data sheet of LM35 we know for every 1 degree C rise there is increase of 10mv in the output.

temperature= (analogRead(A0)*4.88mV.)/10mv

If you want to increase the accuracy you can  decrease the Vref. Hope you enjoyed the post and gained something out of it.

No comments:

Post a Comment