Sensor Calibration and Data
This is the final topic in your sensor curriculum.
Understanding calibration and sensor data is extremely important because simply connecting a sensor to Arduino or ESP32 does not guarantee that the measurement is accurate.
For example:
Real Temperature = 30°C Sensor Reading = 32°C
The sensor is working, but its measurement has an error.
Calibration helps us make sensor measurements more meaningful and reliable.
1 What Is Sensor Calibration?
Calibration is the process of comparing a sensor's output with a known reference and adjusting the system so that the measured value is closer to the true/reference value.
Simple example:
Known reference ↓ 25°C ↓ Temperature Sensor ↓ 27°C
The sensor has an error:
Error = 27 - 25 = 2°C
Calibration can be used to compensate for this difference.
2 Why Is Calibration Necessary?
Sensors can have errors because of:
- Manufacturing differences
- Temperature
- Humidity
- Aging
- Electrical noise
- Supply voltage
- Installation
- Sensor placement
- Environmental conditions
For example, two sensors of the same model might produce slightly different readings.
Sensor A → 24.8°C Sensor B → 25.3°C Sensor C → 25.1°C
This doesn't necessarily mean one sensor is broken.
3 Accuracy vs Precision
These two terms are very important.
Accuracy
How close a measurement is to the actual/reference value.
Example:
Actual = 25°C Reading = 25.1°C
Very accurate.
Precision
How consistently repeated measurements are grouped together.
Example:
24.9 25.0 24.9 25.0
These readings are very consistent.
4 Accuracy vs Precision Example
Imagine the real value is:
100
Sensor A:
100 101 99 100
Good accuracy and precision.
Sensor B:
110 110 111 110
High precision but poor accuracy.
Sensor C:
95 105 98 103
Lower precision and potentially lower accuracy.
5 Sensor Resolution
Resolution describes the smallest change that a sensor/system can distinguish.
For example:
Resolution = 0.1°C
means the system can represent temperature changes around that scale.
For an ADC, resolution depends on the number of bits.
6 ADC Resolution
An ADC converts analog voltage into a digital value.
For an N-bit ADC:
Number of levels = 2^N
For example:
10-bit ADC
2^10 = 1024 levels
Typically:
0 → 1023
12-bit ADC
2^12 = 4096 levels
Typically:
0 → 4095
The actual usable behavior depends on the microcontroller and ADC configuration.
7 Arduino Analog Reading
On many Arduino Uno boards, analogRead() commonly produces:
0 → 1023
Example:
int value = analogRead(A0); Serial.println(value);
Possible output:
245 512 780
These are raw ADC values, not automatically physical units.
8 ESP32 Analog Reading
ESP32 ADC behavior depends on the particular ESP32 family, board, attenuation configuration, and framework settings.
A common conceptual workflow is:
Sensor ↓ Analog Voltage ↓ ESP32 ADC ↓ Raw ADC value ↓ Calibration/conversion ↓ Physical value
Example:
int raw = analogRead(34); Serial.println(raw);
You should not assume that every ESP32 ADC reading maps perfectly to a specific voltage without considering ADC configuration and calibration.
9 Raw Data
Raw data is the direct output from a sensor or ADC before converting it into a meaningful physical quantity.
Example:
Raw ADC = 1870
This isn't very useful by itself.
After calibration/conversion:
Temperature = 28.4°C
Now the value has physical meaning.
10 Sensor Conversion
Suppose a sensor has a known relationship:
Raw ADC ↓ Mathematical conversion ↓ Temperature
A simple linear conversion can be written as:
y = mx + c
Where:
x= raw sensor valuey= converted measurementm= scalec= offset
11 Two-Point Calibration
Two-point calibration is one of the most useful calibration techniques.
Suppose we have:
Sensor reading A = 200 Known value A = 10°C Sensor reading B = 800 Known value B = 50°C
We can derive a linear relationship.
Formula:
m = (Y2 - Y1) / (X2 - X1)
Therefore:
m = (50 - 10) / (800 - 200) = 40 / 600 = 0.0667
Then:
Y = mX + c
This gives a calibration equation for converting sensor readings.
12 Zero-Point Calibration
Some sensors need only an offset correction.
Suppose:
Reference = 0 Sensor = 5
Offset:
Offset = Reference - Sensor = 0 - 5 = -5
Corrected value:
Corrected = Sensor + Offset
Example:
Sensor = 25 Offset = -5 Corrected = 20
This is useful when the sensor has a relatively constant offset error.
13 Calibration of a Temperature Sensor
Suppose a reference thermometer says:
Reference = 30°C
Your sensor says:
Sensor = 32°C
Difference:
Error = 2°C
You may use an offset:
Correction = -2°C
Then:
Corrected temperature = Sensor reading - 2
So:
32 - 2 = 30°C
This is only appropriate if the error behaves approximately like a constant offset over the range of interest.
14 Multi-Point Calibration
Sometimes a sensor is not perfectly linear.
Then two points may not be enough.
For example:
Reference Sensor 10°C 12°C 20°C 21°C 30°C 32°C 40°C 44°C
The error changes across the range.
In such cases, you may use:
- Multi-point calibration
- Polynomial fitting
- Lookup tables
- Piecewise linear interpolation
15 Lookup Table
A lookup table stores known calibration values.
Example:
Raw Value | Real Value ----------|----------- 100 | 10°C 200 | 15°C 300 | 20°C 400 | 25°C 500 | 30°C
The program finds the closest range and estimates the physical value.
Lookup tables are useful when a sensor's response is nonlinear.
16 Soil Moisture Calibration
This is especially important for agriculture.
Suppose a capacitive soil sensor produces:
Dry soil = 3000 Wet soil = 1200
A basic percentage conversion could be:
Moisture % = (DryValue - CurrentValue) / (DryValue - WetValue) × 100
Suppose:
Current = 2100 Dry = 3000 Wet = 1200
Then:
Moisture = (3000 - 2100) / (3000 - 1200) × 100 = 900 / 1800 × 100 = 50%
But remember:
There is no universal ADC value that always means a particular soil-moisture percentage.
Calibration depends on:
- Soil type
- Sensor
- Depth
- Temperature
- Salinity
- Sensor placement
17 pH Sensor Calibration
pH sensors generally require proper calibration using standard buffer solutions.
Common reference buffers include:
pH 4 pH 7 pH 10
Depending on the application, a two-point calibration might use:
pH 4 + pH 7
or:
pH 7 + pH 10
A three-point calibration can cover a wider range.
Conceptually:
Buffer Solution ↓ Known pH ↓ Sensor reading ↓ Calibration
pH probes also require appropriate maintenance and storage.
18 TDS and EC Calibration
TDS and EC sensors require careful calibration because conductivity depends on the measurement system and the solution.
Calibration solutions with known conductivity can be used.
Known EC solution ↓ Sensor ↓ Measured value ↓ Correction/calibration
Temperature compensation can also be important because conductivity changes with temperature.
19 Sensor Drift
Drift means a sensor's output changes over time even when the actual measured condition remains similar.
Example:
Year 1 → 25.0°C Year 2 → 25.4°C Year 3 → 25.8°C
Possible causes:
- Aging
- Environmental exposure
- Material changes
- Electronics aging
Therefore, some systems require periodic recalibration.
20 Sensor Hysteresis
Hysteresis means a sensor can give slightly different outputs depending on whether the input is increasing or decreasing.
Example:
Increasing: 10 → 20 → 30 Decreasing: 30 → 20 → 10
The readings may not follow exactly the same path.
This matters in precision measurement and control systems.
21 Sensor Noise
Noise is unwanted variation in the measurement.
Example:
Actual signal:
100 100 100 100
Measured:
99 103 98 101 100 104
The fluctuations may be caused by:
- Electrical interference
- Power supply noise
- Long wires
- EMI
- Sensor limitations
- Environmental changes
22 Averaging Sensor Data
One simple method of reducing random noise is averaging.
Suppose:
100 102 98 101 99
Average:
(100 + 102 + 98 + 101 + 99) / 5 = 100
Arduino example:
long total = 0; for (int i = 0; i < 10; i++) { total += analogRead(34); delay(10); } float average = total / 10.0; Serial.println(average);
This can make readings more stable, although averaging also adds delay and doesn't remove all types of noise.
23 Moving Average
Instead of averaging only once, a moving average continuously updates.
Example:
10, 12, 11, 13, 12
Take groups of three:
(10 + 12 + 11) / 3 = 11.0 (12 + 11 + 13) / 3 = 12.0 (11 + 13 + 12) / 3 = 12.0
This produces a smoother signal.
24 Median Filtering
Median filtering is useful when there are occasional abnormal spikes.
Example:
100 101 99 250 100
The value:
250
is probably an outlier.
Sorted:
99 100 100 101 250
Median:
100
So the median filter can reject isolated spikes better than a simple average.
25 Sensor Outliers
An outlier is a measurement that is significantly different from surrounding values.
Example:
30.1 30.2 30.0 85.0 ← suspicious 30.1
A program can detect unusual values and decide whether to reject or flag them.
Example:
if (temperature > 60) { Serial.println("Possible invalid reading"); }
The correct threshold depends on the application.
26 Data Validation
Never blindly trust every sensor reading.
A good system can check:
Read sensor ↓ Is value valid? ↓ Is it within expected range? ↓ Filter noise ↓ Convert/calibrate ↓ Store/use data
For example:
Temperature = 25°C ↓ Valid range? ↓ YES ↓ Store
But:
Temperature = 999°C ↓ Invalid ↓ Reject/flag
27 Sensor Sampling Rate
Sampling rate is how frequently you measure the sensor.
Example:
1 sample/second
means:
Read wait Read wait Read
For a temperature sensor:
1 Hz
may be enough.
For a fast signal like ECG:
Much higher sampling rate
is generally required.
The correct sampling rate depends on the dynamics and bandwidth of the signal being measured.
28 Sampling Too Slowly
Suppose a signal changes very quickly:
Actual: /\/\/\/\/\/\/\/\/\
But you sample very slowly:
• • •
You may miss important information.
This is related to the Nyquist-Shannon sampling theorem.
As a basic rule, the sampling frequency should be greater than twice the highest frequency component you need to capture:
Fs > 2 × Fmax
In practical systems, engineers often sample considerably faster to allow filtering and improve signal reconstruction.
29 Data Logging
Sensor data can be stored for later analysis.
For example:
Time Temperature Humidity --------------------------------- 10:00 28.1°C 62% 10:01 28.3°C 61% 10:02 28.5°C 60%
This is called data logging.
30 CSV Data
A very simple data format is CSV.
CSV = Comma-Separated Values
Example:
time,temperature,humidity 10:00,28.1,62 10:01,28.3,61 10:02,28.5,60
CSV can be opened using:
- Excel
- Google Sheets
- Python
- Pandas
- Data-analysis software
31 ESP32 Data Logging
ESP32 can send sensor readings through:
- Serial
- Wi-Fi
- Bluetooth
- MQTT
- HTTP
- WebSocket
Architecture:
Sensor ↓ ESP32 ↓ Processing ↓ Wi-Fi ↓ Server ↓ Database ↓ Dashboard
32 Real-Time Sensor Dashboard
You can create a dashboard showing:
┌────────────────────────────┐ │ SENSOR DASHBOARD │ ├────────────────────────────┤ │ Temperature : 28.4°C │ │ Humidity : 62% │ │ Soil : 48% │ │ Water Level : 76% │ │ Gas : Normal │ │ Light : 540 lux │ └────────────────────────────┘
For your ESP32 projects, the architecture can be:
Sensors ↓ ESP32 ↓ Wi-Fi ↓ Web API ↓ Database ↓ React / Next.js Dashboard
33 Sensor Data Pipeline
A professional sensor system often follows this pipeline:
SENSOR ↓ Raw Measurement ↓ ADC/I²C ↓ Calibration ↓ Filtering ↓ Data Validation ↓ Unit Conversion ↓ Data Processing ↓ Storage/Database ↓ Visualization ↓ Decision
For example:
Soil Sensor ↓ ADC = 2140 ↓ Calibration ↓ Moisture = 48% ↓ Check threshold ↓ Soil is dry ↓ Pump ON
34 Sensor Accuracy Specifications
When selecting a sensor, don't look only at the price.
Important specifications include:
Accuracy
Example:
±0.5°C
Resolution
Example:
0.1°C
Operating range
Example:
-40°C to +125°C
Response time
How quickly the sensor reacts to a change.
Repeatability
How consistently it gives the same result under the same conditions.
Sensitivity
How much the output changes for a change in input.
35 Sensitivity
Suppose:
Temperature increases by 1°C Sensor output increases by 10 mV
Then sensitivity is:
10 mV/°C
Conceptually:
Input change ↓ Sensor ↓ Output change
Higher sensitivity doesn't automatically mean higher accuracy.
36 Offset Error
Suppose the real value is:
0
but the sensor always reads:
5
This is an offset error.
Graphically:
Ideal: / / / / Actual: / / / / +5
An offset correction can compensate for this if it is stable.
37 Gain Error
Gain error means the sensor's scale is incorrect.
Example:
Actual: 10 → Sensor 11 20 → Sensor 22 30 → Sensor 33
The sensor is consistently scaling the measurement incorrectly.
Calibration can correct both:
- Offset
- Gain
A simple linear calibration model is:
Corrected = Gain × Raw + Offset
38 Sensor Calibration Example in Arduino
Suppose:
Raw = analogRead(A0)
and after calibration:
Corrected = 0.05 × Raw - 10
Code:
int raw = analogRead(A0); float value = 0.05 * raw - 10; Serial.print("Raw: "); Serial.println(raw); Serial.print("Calibrated: "); Serial.println(value);
The coefficients must come from actual calibration data; they should not be copied blindly between different sensors.
39 Automatic Calibration
Some systems can perform calibration automatically under controlled conditions.
For example:
System Start ↓ Known reference condition ↓ Read sensor ↓ Calculate correction ↓ Save calibration value ↓ Normal operation
This can be useful for some sensors, but automatic calibration should only be performed when the reference condition is genuinely known.
40 Storing Calibration Values
Calibration values can be stored in:
- EEPROM
- Flash
- Preferences/NVS on ESP32
- SD card
- Database
For example:
DryValue = 3000 WetValue = 1200 Offset = -2.1 Gain = 1.03
After restarting the ESP32, it can load the stored calibration values.
41 Sensor Calibration Example: Soil Moisture
A practical system:
Step 1 Insert sensor into dry soil ↓ Record ADC ↓ Dry = 3000 Step 2 Put sensor in wet soil ↓ Record ADC ↓ Wet = 1200 Step 3 Measure normal soil ↓ ADC = 2100 Step 4 Convert to percentage ↓ ≈ 50%
This is much better than assuming a universal value from an online tutorial.
42 Sensor Calibration Example: LDR
An LDR gives a changing electrical signal depending on light.
You can calibrate it against a known light source or lux meter.
Light level ↓ LDR ↓ ADC ↓ Calibration ↓ Estimated lux
For accurate lux measurement, a dedicated light sensor designed to measure illuminance is generally preferable to simply converting an LDR ADC value to lux.
43 Sensor Calibration Example: Ultrasonic
Suppose the actual tank distance is:
100 cm
but the sensor reports:
103 cm
You can investigate the systematic error and apply a correction if it remains consistent.
However, ultrasonic measurements can vary with:
- Temperature
- Surface angle
- Water movement
- Foam
- Sensor position
So calibration alone may not eliminate all errors.
44 Sensor Calibration Example: Water Level
Suppose:
Tank height = 100 cm
Measured distance:
30 cm
Then:
Water height = 100 - 30 = 70 cm
Percentage:
70 / 100 × 100 = 70%
You should still validate the calculated value against actual tank levels.
45 Sensor Data Units
Always attach the correct unit to data.
Bad:
Temperature = 28
Better:
Temperature = 28°C
Examples:
| Measurement | Unit |
|---|---|
| Temperature | °C / °F |
| Humidity | %RH |
| Pressure | Pa / kPa / bar |
| Distance | mm / cm / m |
| Speed | km/h / m/s |
| Flow | L/min |
| pH | pH units |
| Light | lux |
| Weight | g / kg |
| Voltage | V |
| Current | A |
| Soil moisture | calibrated % |
46 Never Confuse Raw Value With Physical Value
For example:
ADC = 2500
does not automatically mean:
2500°C
Instead:
ADC = 2500 ↓ Calibration/conversion ↓ Temperature = 28.4°C
This distinction is extremely important when programming sensors.
47 Sensor Fusion
Sometimes one sensor isn't enough.
Example:
GPS + IMU
or:
Soil moisture + rain + tank level
or:
Heart-rate + temperature
The system combines multiple measurements to make a better decision.
Example:
Soil moisture = low Rain = no Tank = 80% ↓ Pump ON
But:
Soil moisture = low Rain = detected ↓ Wait
48 Smart Sensor Decision System
A complete IoT system can work like:
SENSOR ↓ Raw Data ↓ Calibration ↓ Filtering ↓ Validation ↓ Decision Logic ↓ ┌────────┴────────┐ ↓ ↓ Actuator Storage ↓ ↓ Pump/Relay Database ↓ Dashboard
This is the basic architecture behind many smart IoT systems.
49 Example: Complete Smart Agriculture System
Combining everything from the previous topics:
SMART FARM │ ESP32 │ ┌─────────────────┼─────────────────┐ ↓ ↓ ↓ Soil Moisture DHT22 Rain Sensor │ │ │ └─────────────────┼─────────────────┘ ↓ DATA PROCESSING │ ┌──────────┴──────────┐ ↓ ↓ Calibration Validation │ │ └──────────┬──────────┘ ↓ Decision Logic │ ┌──────────┴──────────┐ ↓ ↓ Pump Dashboard │ │ ↓ ↓ Irrigation IoT/Cloud
The result is not just a collection of sensors.
It becomes a complete intelligent sensing system.
50 Best Practices for Sensor Projects
1. Read the datasheet
Don't rely only on random internet diagrams.
2. Check voltage
Especially with ESP32:
ESP32 GPIO logic ≈ 3.3V
A sensor/module output should be verified before connecting it directly.
3. Calibrate
Don't assume factory values are perfect for your application.
4. Filter
Remove reasonable amounts of random noise.
5. Validate
Reject impossible readings.
6. Record raw data
Keeping raw data helps debug the system later.
7. Store calibration values
Don't force the user to recalibrate every reboot.
8. Use proper units
Always document units.
9. Test under real conditions
A sensor working on your desk doesn't necessarily mean it will work correctly outdoors.
10. Don't confuse resolution with accuracy
A sensor displaying:
25.001°C
doesn't mean it is accurate to 0.001°C.
51 Complete Sensor Data Example
Imagine an ESP32 receives:
Temperature ADC = 1870 Humidity raw = 621 Soil raw = 2150 Water distance = 28 cm
The processing pipeline becomes:
Raw Data ↓ Calibration ↓ Temperature = 28.4°C Humidity = 62% Soil = 47% Water = 72% ↓ Validation ↓ Store ↓ Display ↓ Decision
Dashboard:
┌──────────────────────────────┐ │ SMART SENSOR HUB │ ├──────────────────────────────┤ │ Temperature : 28.4°C │ │ Humidity : 62% │ │ Soil : 47% │ │ Water : 72% │ │ │ │ Pump : OFF │ │ System : NORMAL │ └──────────────────────────────┘
52 The Complete Sensor Learning Flow
Now you have covered the entire sensor curriculum.
The overall learning path is:
1. Introduction ↓ 2. Analog & Digital Sensors ↓ 3. Temperature & Humidity ↓ 4. Pressure & Environmental ↓ 5. Gas & Air Quality ↓ 6. Light & Color ↓ 7. Motion & Distance ↓ 8. Magnetic & Position ↓ 9. Touch & Input ↓ 10. Force, Load & Flex ↓ 11. Sound & Vibration ↓ 12. Soil & Agriculture ↓ 13. Water & Liquid ↓ 14. Biometric & Health ↓ 15. RFID, NFC & Identification ↓ 16. GPS & Location ↓ Calibration & Data
53 Final Summary
A sensor system is not simply:
Sensor → Arduino
A proper system is closer to:
┌──────────────┐ │ SENSOR │ └──────┬───────┘ ↓ Raw Signal ↓ ┌──────────────┐ │ ADC / I²C / │ │ SPI / UART │ └──────┬───────┘ ↓ ┌──────────────┐ │ Calibration │ └──────┬───────┘ ↓ ┌──────────────┐ │ Filtering │ └──────┬───────┘ ↓ ┌──────────────┐ │ Validation │ └──────┬───────┘ ↓ ┌──────────────┐ │ Data Convert │ └──────┬───────┘ ↓ ┌──────────────┐ │ Decision │ └──────┬───────┘ ↓ ┌─────┴──────┐ ↓ ↓ Actuator Database ↓ ↓ Pump Dashboard Motor IoT Relay Analytics