Scaling and calibrating data

You may have noticed that it can sometimes be difficult to interpret data read from an ADC, since the value is just a number. A number isn't much help on its own; all it can tell you is that the environment is slightly hotter or slightly darker than the previous sample. However, if you can use another device to provide comparable values (such as the current room temperature), you can then calibrate your sensor data to provide more useful real-world information.

To obtain a rough calibration, we shall use two samples to create a linear fit model that can then be used to estimate real-world values for other ADC readings (this assumes the sensor itself is mostly linear in its response). The following diagram shows a linear fit graph using two readings at 25 and 30 degrees Celsius, providing estimated ADC values for other temperatures:

Samples are used to linearly calibrate temperature sensor readings

We can calculate our model using the following function:

def linearCal(realVal1,readVal1,realVal2,readVal2): 
  #y=Ax+C 
  A = (realVal1-realVal2)/(readVal1-readVal2) 
  C = realVal1-(readVal1*A) 
  cal = (A,C) 
  return cal 

This will return cal, which will contain the model slope (A) and offset (C).

We can then use the following function to calculate the value of any reading by using the calculated cal values for that channel:

def calValue(readVal,cal = [1,0]): 
  realVal = (readVal*cal[0])+cal[1] 
  return realVal 

For more accuracy, you can take several samples and use linear interpolation between the values (or fit the data to other, more complex mathematical models), if required.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.146.155