Analog sensors are run by measuring a physical object, chemicals, pressure, temperature, weight, etc. This device works by converting analog voltage and converted to digital signals that your computer or microcontroller can understand. Many sensors have a respective formula that describes the connection between their voltage output functionality and reading values. All analog sensors have documentation of how to operate including formulas high enough to get exact correct readings. However, some analog sensors already define the purpose, but it requires calibrating before it can be used and gathering the proper precise readings, some are required you to generate your own formula for each individual sensor, this process called calibration is a very important stage to achieve precise accuracy.
How to calibrate an Analog Sensor
Basically, the connection between the output voltage of the analog sensor and the measuring parameter can be defined by some functions. In order to determine what is the given values of readings. First thing first we need to gather first data from the sensor before we can calculate the precise and accurate data to fits-in. Below is the test code for gathering data and calibrating an analog sensor using your microcontroller.
Analog Sensor Calibration
First, you need to get the highest and lowest reading value from the sensor to find its operating range at the current requirements. For this example, we can test using an analog sensor such as a photoresistor or potentiometer will fit. Don’t forget that you need to get readings from Analog 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// ARDUINO CODE // int sensorValue = 0; // This is the sensor value int sensorMin = 1023; // Set to the Minimum sensor value int sensorMax = 0; // Set to the Maximum sensor value void setup() { Serial.begin(9600); // Set to operate at the serial 9600 Serial.println("14CORE | Analog Sensor Test Radings"); Serial.println("..................................."); delay(2000); Serial.println("14CORE | Starting calibration for 10 seconds"); // calibrate during the first five seconds while (millis() < 10000) { sensorValue = analogRead(0); // set to read sensor connected to analog pin 0 if (sensorValue > sensorMax) // set to save the maximum sensor value found { sensorMax = sensorValue; } if (sensorValue < sensorMin) // set to save the minimum sensor value found { sensorMin = sensorValue; } } Serial.println("Finished calibration"); } void loop() { // Set to read sensor connected to analog pin 0 sensorValue = analogRead(0); //Set to use the calibration to print values between 0 and 100 sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 100); // needs to keep the value in the desired range sensorValue = constrain(sensorValue, 0, 100); // Set fade the LED using the calibrated value: Serial.println(sensorValue, DEC); } |
Precise Analog Sensor Reading
The example below will demonstrate how to read any analog sensor such as a Photoresistor or Potentiometer accurately, The analog pin with is will be marked as 0 of the sensor and will be read, and calculated according to its average using an array to keep track of the last reading value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#define numTest 10 // this variable samples to keep track of int samples[numTest]; //set to the readings from the sensor int index = 0; // set to index of the current reading int total = 0; // set to the sum of all samples int average = 0; // set to calculate the average of all samples void setup() { Serial.begin(9600); // serial begin set to 9600 Serial.println("14CORE | Analog Sensor Test"); Serial.println("Starting ..... "); delay(1000); // Set to Initialize the array's elements to 0 for (int i = 0; i < numTest; i++) { samples[i] = 0; } } void loop() { // Set to Subtract the value in current position from the total total = total - samples[index]; // Set to read from the sensor a new value and store it samples[index] = analogRead(0); // Set to add the new reading to the total total = total + samples[index]; // Set to advance to the next position in the array: index = index + 1; // and if index is at the end of the array go back to position 0 if (index >= numTest) { index = 0; } // then calculate the average of all elements average = total / numTest; // and print average to the serial terminal Serial.println(average, DEC); } |