Project 8: Calibrate a Sensor Automatically (Calibration)

Created on: 2 August 2012
Updated on: 17 January 2023

Tutorial 10 – Pages

During the first 5 seconds that the Arduino sketch for this project runs, it reads the value on analog pin A0 which has an LDR connected to it. The highest and lowest values read on A0 are saved. After the 5 second period, the Arduino will expect to get values between the highest and lowest values that it saved. It has thus been "calibrated" to these values.

After calibration, the value on the A0 analog pin will be continually read. The value that is read will be converted to a value between 0 and 255. When the minimum saved (calibrated) value is read, it will be converted to 0. When the maximum saved (calibrated) value is read, it will be converted to 255. Any value between the maximum and minimum calibrated values will be converted to a value between 255 and 0.

Project 8 Video

The video below shows the Arduino calibration project in action.

The converted value is used to change the brightness of the LED connected to Arduino pin 9.

As an example: If the values read from A0 during calibration were between 20 and 100, then in the main program when values between 20 and 100 are received, values between 0 and 255 will be output to the LED.

Without calibration, the LED will never reach full dimness (0) when the lowest value (20) is read from A0. The LED would also never reach full brightness (255) when the highest value (100) is read from A0.

Parts Needed

Besides an Arduino Uno or compatible board, USB cable, breadboard and wire links, you will need:

  • 1 × LDR (Light Dependent Resistor)
  • 1 × LED
  • 1 × 10k resistor
  • 1 × 220 ohm resistor

Arduino Website Page


In the Arduino IDE

File → Examples → 3.Analog → Calibration

Building the Circuit

The circuit diagram and breadboard connections are shown on the Arduino website Calibration page. The LED is connected to pin 9 and the LDR to A0.

Programming and Operating the Circuit

In the Arduino IDE, open and upload the Calibration sketch (File → Examples → 3.Analog → Calibration).

Press the Arduino reset button and then move your hand above the LDR so that a minimum value is sampled and saved. Move your hand away from the LDR so that it receives more light and a maximum value is sampled and saved. After calibration, the LED will switch on. You can now move your hand around the LDR and see the brightness of the LED change.