Tutorial.Calibration History

Hide minor edits - Show changes to output

October 25, 2012, at 11:28 AM by Roberto Guido - minor typo
Changed lines 5-6 from:
This example semonstrates one techinque for calibrating sensor input. The Arduino takes sensor readings for five seconds during the startup, and tracks the highest and lowest values it gets. These sensor readings during the first five seconds of the sketch execution define the minimum and maximum of expected values for the readings taken during the loop.
to:
This example demonstrates one techinque for calibrating sensor input. The Arduino takes sensor readings for five seconds during the startup, and tracks the highest and lowest values it gets. These sensor readings during the first five seconds of the sketch execution define the minimum and maximum of expected values for the readings taken during the loop.
May 02, 2012, at 03:41 AM by Scott Fitzgerald -
Changed line 86 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/Calibration/Calibration.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/03.Analog/Calibration/Calibration.ino lang=arduino tabwidth=4:)
November 16, 2011, at 04:11 AM by Scott Fitzgerald -
Changed line 86 from:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/3.Analog/Calibration/Calibration.ino lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/Calibration/Calibration.ino lang=arduino tabwidth=4:)
September 30, 2011, at 03:11 AM by Scott Fitzgerald -
Changed line 86 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/Calibration/Calibration.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/new-extension/build/shared/examples/3.Analog/Calibration/Calibration.ino lang=arduino tabwidth=4:)
September 24, 2010, at 07:05 AM by Tom Igoe -
Changed line 47 from:
(:source lang=arduino class=code tabwidth=4:)
to:
(:source lang=arduino tabwidth=4:)
September 24, 2010, at 07:00 AM by Tom Igoe -
Changed line 47 from:
(:source lang=arduino tabwidth=4:)
to:
(:source lang=arduino class=code tabwidth=4:)
September 24, 2010, at 06:07 AM by Tom Igoe -
Added lines 9-10:
(:div class=BOM :)
Changed lines 18-19 from:

to:
(:divend:)
Added lines 22-23:
(:div class=circuit :)
Changed lines 34-35 from:
to:
(:divend:)
September 24, 2010, at 06:06 AM by Tom Igoe -
Changed lines 75-76 from:
to:
(:divend:)
Added lines 79-80:
(:div class=code :)
September 24, 2010, at 06:05 AM by Tom Igoe -
Added lines 69-70:
(:div class=code :)
Deleted lines 77-78:
(:div class=code :)
September 24, 2010, at 06:03 AM by Tom Igoe -
Changed lines 74-75 from:
Here's the whole program:
to:
Here's the whole program:
September 24, 2010, at 06:02 AM by Tom Igoe -
Changed lines 51-64 from:
// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
to:
// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
September 23, 2010, at 10:33 PM by Christian Cerrito -
Changed lines 3-4 from:
!!Calibration
to:
!!!Calibration
September 23, 2010, at 09:32 PM by Christian Cerrito -
Changed lines 89-90 from:
to:
* [[Reference/If]]()
September 23, 2010, at 09:25 PM by Christian Cerrito -
Changed lines 35-37 from:
%width=400px%[[Attach:calibration2_sch.png | Attach:calibration2_sch.png]]

to:
%width=400px%[[Attach:calibration_sch.png | Attach:calibration_sch.png]]

September 23, 2010, at 09:24 PM by Christian Cerrito -
Changed lines 35-37 from:
%width=400px%[[Attach:calibration2_sch.png | Attach:calibration_sch.png]]

to:
%width=400px%[[Attach:calibration2_sch.png | Attach:calibration2_sch.png]]

September 23, 2010, at 09:22 PM by Christian Cerrito -
Changed lines 35-37 from:
%width=400px%[[Attach:calibration_schem.png | Attach:calibration_schem.png]]

to:
%width=400px%[[Attach:calibration2_sch.png | Attach:calibration_sch.png]]

September 17, 2010, at 06:20 PM by Tom Igoe -
Changed lines 67-68 from:
This way, any further readings you take can be mapped to the range between this minimum and maximum like so:
to:
This way, any further readings you take can be mapped to the range between this minimum and maximum like so:
Changed lines 74-75 from:

to:
Here's the whole program:
September 17, 2010, at 06:19 PM by Tom Igoe -
Changed lines 5-6 from:
Demonstrates one techinque for calibrating sensor input. The sensor readings during the first five seconds of the sketch execution define the minimum and maximum of expected values.
to:
This example semonstrates one techinque for calibrating sensor input. The Arduino takes sensor readings for five seconds during the startup, and tracks the highest and lowest values it gets. These sensor readings during the first five seconds of the sketch execution define the minimum and maximum of expected values for the readings taken during the loop.
Added lines 40-75:
Before the setup, you set initial values for the minimum and maximum like so:

(:source lang=arduino tabwidth=4:)
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
(:sourceend:)


These may seem backwards. Initially, you set the minimum high and read for anything lower than that, saving it as the new minimum. Likewise, you set the maximum low and read for anything higher as the new maximum, like so:

(:source lang=arduino tabwidth=4:)
// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
(:sourceend:)

This way, any further readings you take can be mapped to the range between this minimum and maximum like so:

(:source lang=arduino tabwidth=4:)
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
(:sourceend:)


September 16, 2010, at 10:08 PM by Tom Igoe -
Changed line 42 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/Calibration/Calibration.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/Calibration/Calibration.pde lang=arduino tabwidth=4:)
August 26, 2010, at 08:40 AM by Christian Cerrito -
Deleted lines 50-51:
* [[Reference/analogRead}]()
* [[Reference/analogWrite()]]()
August 26, 2010, at 08:39 AM by Christian Cerrito -
Changed lines 49-51 from:
* [[Reference/while]()
* [[Reference/millis]()
* [[Reference/analogRead}()
to:
* [[Reference/while]]()
* [[Reference/millis]]()
* [[Reference/analogRead}]()
August 26, 2010, at 08:39 AM by Christian Cerrito -
Changed lines 28-30 from:
to:
Connect an LED to digital pin 9 with a 220 ohm current limiting resistor. Connect a photocell to 5V and then to analog pin 0 with a 10K ohm resistor as a reference to ground.

Changed lines 44-60 from:
(:divend:)
to:
(:divend:)


!!!See Also:

* [[Reference/while]()
* [[Reference/millis]()
* [[Reference/analogRead}()
* [[Reference/analogWrite()]]()
* [[Reference/constrain]]()
* [[Reference/map]]()

* [[Tutorial/AnalogInput]] - use a potentiometer to control the blinking of an LED
*[[Tutorial/AnalogInOutSerial]] - read an analog pin, map the result, and use that data to dim or brighten an LED
* [[Tutorial/Fade]] - use an analog input to fade an LED
* [[Tutorial/Smoothing]] - smooth multiple readings of an analog input
August 26, 2010, at 08:28 AM by Christian Cerrito -
Changed lines 7-17 from:
to:
!!!Hardware Required

* Arduino board
* (1) LED
* (1) analog sensor (a photocell will do)
* (1) 10K ohm resistor
* (1) 220 ohm resistor
* breadboard
* hook-up wire

August 24, 2010, at 08:19 AM by Christian Cerrito -
Changed line 30 from:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Analog/Calibration/Calibration.pde lang=arduino tabwidth=4:)
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/3.Analog/Calibration/Calibration.pde lang=arduino tabwidth=4:)
February 23, 2010, at 07:57 PM by Tom Igoe -
Changed lines 30-54 from:
%color=#7e7e7e%/*
%color=#7e7e7e% Calibration
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates one techinque for calibrating sensor input. The
%color=#7e7e7e% sensor readings during the first five seconds of the sketch
%color=#7e7e7e% execution define the minimum and maximum of expected values
%color=#7e7e7e% attached to the sensor pin.
%color=#7e7e7e%
%color=#7e7e7e% The sensor minumum and maximum initial values may seem backwards.
%color=#7e7e7e% Initially, you set the minimum high and listen for anything
%color=#7e7e7e% lower, saving it as the new minumum. Likewise, you set the
%color=#7e7e7e% maximum low and listen for anything higher as the new maximum.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * Analog sensor (potentiometer will do) attached to analog input 0
%color=#7e7e7e% * LED attached from digital pin 9 to ground
%color=#7e7e7e%
%color=#7e7e7e% created 29 Oct 2008
%color=#7e7e7e% By David A Mellis
%color=#7e7e7e% Modified 17 Jun 2009
%color=#7e7e7e% By Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/Calibration
%color=#7e7e7e%
%color=#7e7e7e% */
to:
(:source http://arduino.cc/en/pub/code/master/build/shared/examples/Analog/Calibration/Calibration.pde lang=arduino tabwidth=4:)
Deleted lines 31-78:
%color=#7e7e7e%// These constants won't change:
const %color=#cc6600%int%% sensorPin = 2; %color=#7e7e7e%// pin that the sensor is attached to
const %color=#cc6600%int%% ledPin = 9; %color=#7e7e7e%// pin that the LED is attached to

%color=#7e7e7e%// variables:
%color=#cc6600%int%% sensorValue = 0; %color=#7e7e7e%// the sensor value
%color=#cc6600%int%% sensorMin = 1023; %color=#7e7e7e%// minimum sensor value
%color=#cc6600%int%% sensorMax = 0; %color=#7e7e7e%// maximum sensor value


%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// turn on LED to signal the start of the calibration period:
%color=#cc6600%pinMode%%(13, %color=#006699%OUTPUT%%);
%color=#cc6600%digitalWrite%%(13, %color=#006699%HIGH%%);

%color=#7e7e7e%// calibrate during the first five seconds
%color=#cc6600%while%% (%color=#cc6600%millis%%() < 5000) {
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// record the maximum sensor value
%color=#cc6600%if%% (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

%color=#7e7e7e%// record the minimum sensor value
%color=#cc6600%if%% (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

%color=#7e7e7e%// signal the end of the calibration period
%color=#cc6600%digitalWrite%%(13, %color=#006699%LOW%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// apply the calibration to the sensor reading
sensorValue = %color=#cc6600%map%%(sensorValue, sensorMin, sensorMax, 0, 255);

%color=#7e7e7e%// in case the sensor value is outside the range seen during calibration
sensorValue = %color=#cc6600%constrain%%(sensorValue, 0, 255);

%color=#7e7e7e%// fade the LED using the calibrated value:
%color=#cc6600%analogWrite%%(ledPin, sensorValue);
}
August 27, 2009, at 08:46 PM by Tom Igoe -
Added lines 16-18:
[-image developed using [[http://www.fritzing.org |Fritzing]]. For more circuit examples, see the [[http://fritzing.org/projects/|Fritzing project page]] -]

July 05, 2009, at 07:20 PM by Tom Igoe -
Changed lines 23-27 from:
!!!!Code

[@
/*
Calibration
to:
!!!Code

(:div class=code :)

%color=#7e7e7e%/*
%color=#7e7e7e% Calibration
%color=#7e7e7e%
%color=#7e7e7e% Demonstrates one techinque for calibrating sensor input. The
%color=#7e7e7e% sensor readings during the first five seconds of the sketch
%color=#7e7e7e% execution define the minimum and maximum of expected values
%color=#7e7e7e% attached to the sensor pin.
%color=#7e7e7e%
%color=#7e7e7e% The sensor minumum and maximum initial values may seem backwards.
%color=#7e7e7e% Initially, you set the minimum high and listen for anything
%color=#7e7e7e% lower, saving it as the new minumum. Likewise, you set the
%color=#7e7e7e% maximum low and listen for anything higher as the new maximum.
%color=#7e7e7e%
%color=#7e7e7e% The circuit:
%color=#7e7e7e% * Analog sensor (potentiometer will do) attached to analog input 0
%color=#7e7e7e% * LED attached from digital pin 9 to ground
%color=#7e7e7e%
%color=#7e7e7e% created 29 Oct 2008
%color=#7e7e7e% By David A Mellis
%color=#7e7e7e% Modified 17 Jun 2009
%color=#7e7e7e% By Tom Igoe
%color=#7e7e7e%
%color=#7e7e7e% http://arduino.cc/en/Tutorial/Calibration
%color=#7e7e7e%
%color=#7e7e7e% */
Changed lines 53-56 from:
Demonstrates one techinque for calibrating sensor input. The
sensor readings during the first five seconds of the sketch
execution define the minimum and maximum of expected values
attached to the sensor pin.
to:
%color=#7e7e7e%// These constants won't change:
const %color=#cc6600%int%% sensorPin = 2; %color=#7e7e7e%// pin that the sensor is attached to
const %color=#cc6600%int%% ledPin = 9; %color=#7e7e7e%// pin that the LED is attached to
Changed lines 57-60 from:
The sensor minumum and maximum initial values may seem backwards.
Initially, you set the minimum high and listen for anything
lower, saving it as the new minumum. Likewise, you set the
maximum low and listen for anything higher as the new maximum.
to:
%color=#7e7e7e%// variables:
%color=#cc6600%int%% sensorValue = 0; %color=#7e7e7e%// the sensor value
%color=#cc6600%int%% sensorMin = 1023; %color=#7e7e7e%// minimum sensor value
%color=#cc6600%int%% sensorMax = 0; %color=#7e7e7e%// maximum sensor value
Deleted lines 61-63:
The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0
* LED attached from digital pin 9 to ground
Changed lines 63-66 from:
created 29 Oct 2008
By David A Mellis
Modified 17 Jun 2009
By Tom Igoe
to:
%color=#cc6600%void%% %color=#cc6600%'''setup'''%%() {
%color=#7e7e7e%// turn on LED to signal the start of the calibration period:
%color=#cc6600%pinMode%%(13, %color=#006699%OUTPUT%%);
%color=#cc6600%digitalWrite%%(13, %color=#006699%HIGH%%);
Changed lines 68-70 from:
http://arduino.cc/en/Tutorial/Calibration
to:
%color=#7e7e7e%// calibrate during the first five seconds
%color=#cc6600%while%% (%color=#cc6600%millis%%() < 5000) {
sensorValue = %color=#cc6600%analogRead%%(sensorPin);
Changed lines 72-124 from:
*/

// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to


// variables:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value

int sensorValue = 0; // the sensor value



void setup() {
// turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the first five seconds
while (millis() < 5000) {
sensorValue = analogRead(sensorPin);

// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

// signal the end of the calibration period
digitalWrite(13, LOW);
}

void loop() {
// read the sensor:
sensorValue = analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}
@]
to:
%color=#7e7e7e%// record the maximum sensor value
%color=#cc6600%if%% (sensorValue > sensorMax) {
sensorMax = sensorValue;
}

%color=#7e7e7e%// record the minimum sensor value
%color=#cc6600%if%% (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

%color=#7e7e7e%// signal the end of the calibration period
%color=#cc6600%digitalWrite%%(13, %color=#006699%LOW%%);
}

%color=#cc6600%void%% %color=#cc6600%'''loop'''%%() {
%color=#7e7e7e%// read the sensor:
sensorValue = %color=#cc6600%analogRead%%(sensorPin);

%color=#7e7e7e%// apply the calibration to the sensor reading
sensorValue = %color=#cc6600%map%%(sensorValue, sensorMin, sensorMax, 0, 255);

%color=#7e7e7e%// in case the sensor value is outside the range seen during calibration
sensorValue = %color=#cc6600%constrain%%(sensorValue, 0, 255);

%color=#7e7e7e%// fade the LED using the calibrated value:
%color=#cc6600%analogWrite%%(ledPin, sensorValue);
}

(:divend:)
June 30, 2009, at 02:06 PM by Paul Badger -
Added lines 56-57:

// variables:
Deleted line 60:
// variables:
June 30, 2009, at 02:05 PM by Paul Badger -
Changed lines 56-58 from:
const int sensorMin = 1023; // minimum sensor value
const int sensorMax = 0; // maximum sensor value
to:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
June 25, 2009, at 11:47 PM by Tom Igoe -
Changed lines 14-15 from:
%width=400px%[[Attach:while.png | Attach:while.png]]
to:
%width=400px%[[Attach:calibration.png | Attach:calibration.png]]
Changed lines 20-22 from:
%width=400px%[[Attach:while_schem.png | Attach:while_schem.png]]

to:
%width=400px%[[Attach:calibration_schem.png | Attach:calibration_schem.png]]

June 25, 2009, at 11:35 PM by Tom Igoe -
Changed lines 7-8 from:
!!!!Circuit
to:

!!!Circuit
Added lines 12-22:
[-click the image to enlarge-]

%width=400px%[[Attach:while.png | Attach:while.png]]

!!!Schematic

[-click the image to enlarge-]

%width=400px%[[Attach:while_schem.png | Attach:while_schem.png]]

June 17, 2009, at 11:26 PM by Tom Igoe -
Changed lines 14-19 from:
int sensorPin = 2;
int ledPin = 9;

int val = 0;
int sensorMin = 1023, sensorMax = 0;
to:
/*
Calibration

Demonstrates one techinque for calibrating sensor input. The
sensor readings during the first five seconds of the sketch
execution define the minimum and maximum of expected values
attached to the sensor pin.

The sensor minumum and maximum initial values may seem backwards.
Initially, you set the minimum high and listen for anything
lower, saving it as the new minumum. Likewise, you set the
maximum low and listen for anything higher as the new maximum.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0
* LED attached from digital pin 9 to ground

created 29 Oct 2008
By David A Mellis
Modified 17 Jun 2009
By Tom Igoe

http://arduino.cc/en/Tutorial/Calibration

*/

// These constants won't change:
const int sensorPin = 2; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to

const int sensorMin = 1023; // minimum sensor value
const int sensorMax = 0; // maximum sensor value

// variables:
int sensorValue = 0; // the sensor value


Changed line 53 from:
// signal the start of the calibration period
to:
// turn on LED to signal the start of the calibration period:
Changed line 56 from:
to:
Changed lines 59-60 from:
val = analogRead(sensorPin);
to:
sensorValue = analogRead(sensorPin);
Changed lines 62-63 from:
if (val > sensorMax) {
sensorMax = val;
to:
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
Changed line 65 from:
to:
Changed lines 67-68 from:
if (val < sensorMin) {
sensorMin = val;
to:
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
Changed line 71 from:
to:
Changed lines 77-78 from:
val = analogRead(sensorPin);
to:
// read the sensor:
sensorValue = analogRead(sensorPin);
Changed lines 81-82 from:
val = map(val, sensorMin, sensorMax, 0, 255);
to:
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
Changed lines 84-86 from:
val = constrain(val, 0, 255);

analogWrite(ledPin, val);
to:
sensorValue = constrain(sensorValue, 0, 255);

// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
October 29, 2008, at 10:29 PM by David A. Mellis -
Changed lines 1-2 from:
''Examples > Analog''
to:
''Examples > Analog I/O''
October 29, 2008, at 10:28 PM by David A. Mellis -
Added lines 1-55:
''Examples > Analog''

!!Calibration

Demonstrates one techinque for calibrating sensor input. The sensor readings during the first five seconds of the sketch execution define the minimum and maximum of expected values.

!!!!Circuit

Analog sensor (e.g. potentiometer, light sensor) on analog input 2. LED on digital pin 9.

!!!!Code

[@
int sensorPin = 2;
int ledPin = 9;

int val = 0;
int sensorMin = 1023, sensorMax = 0;

void setup() {
// signal the start of the calibration period
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the first five seconds
while (millis() < 5000) {
val = analogRead(sensorPin);

// record the maximum sensor value
if (val > sensorMax) {
sensorMax = val;
}

// record the minimum sensor value
if (val < sensorMin) {
sensorMin = val;
}
}

// signal the end of the calibration period
digitalWrite(13, LOW);
}

void loop() {
val = analogRead(sensorPin);

// apply the calibration to the sensor reading
val = map(val, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
val = constrain(val, 0, 255);

analogWrite(ledPin, val);
}
@]

Share