Skip to main content
Post Undeleted by Nick Gammon
Post Deleted by Sonali_B

In the main loop, sensorValuesensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has an analogReadanalogRead range from 0 to 1023, and an analogWriteanalogWrite range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called map()map():

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValueoutputValue is assigned to equal the scaled value from the potentiometer. map()map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the analogOutPinanalogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.

// These constants won't change. They're used to give names to the pins used: const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out)

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital // converter to settle after the last reading: delay(2); }

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}

fewFew useful commands while mapping any analog output values:

map()

analogRead()

analogWrite()

serial()

AnalogInput - Use a potentiometer to control the blinking of an LED.

AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.

Calibration - Define a maximum and minimum for expected analog sensor values.

/* Calibration

Demonstrates one technique 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 minimum and maximum initial values may seem backwards. Initially, you set the minimum high and listen for anything lower, saving it as the new minimum. 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

    map()

  • LED attached from digital pin 9 to ground

    analogRead()

  • analogWrite()

  • serial()

  • AnalogInput - Use a potentiometer to control the blinking of an LED.

  • AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.

  • Calibration - Define a maximum and minimum for expected analog sensor values.

      /*
        Calibration
    
       Demonstrates one technique 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 minimum and maximum initial values may seem backwards. Initially,
        you set the minimum high and listen for anything lower, saving it as the new
        minimum. 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 30 Aug 2011
        by Tom Igoe
    
        This example code is in the public domain.
    
        http://www.arduino.cc/en/Tutorial/Calibration
      */
    
      // These constants won't change:
      const int sensorPin = A0;    // pin that the sensor is attached to
      const int ledPin = 9;        // pin that the LED is attached to
    
      // variables:
      int sensorValue = 0;         // the sensor value
      int sensorMin = 1023;        // minimum sensor value
      int sensorMax = 0;           // maximum 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);
      }
    
  • Fading - Use an analog output (PWM pin) to fade an LED.

  • Smoothing - Smooth multiple readings of an analog input.

created 29 Oct 2008 by David A Mellis modified 30 Aug 2011 by Tom Igoe

This example code is in the public domain.

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

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

// variables: int sensorValue = 0; // the sensor value int sensorMin = 1023; // minimum sensor value int sensorMax = 0; // maximum 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); }

Fading - Use an analog output (PWM pin) to fade an LED. Smoothing - Smooth multiple readings of an analog input.

In the main loop, sensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has an analogRead range from 0 to 1023, and an analogWrite range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called map():

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.

// These constants won't change. They're used to give names to the pins used: const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out)

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital // converter to settle after the last reading: delay(2); }

few useful commands while mapping any analog output values:

map()

analogRead()

analogWrite()

serial()

AnalogInput - Use a potentiometer to control the blinking of an LED.

AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.

Calibration - Define a maximum and minimum for expected analog sensor values.

/* Calibration

Demonstrates one technique 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 minimum and maximum initial values may seem backwards. Initially, you set the minimum high and listen for anything lower, saving it as the new minimum. 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 30 Aug 2011 by Tom Igoe

This example code is in the public domain.

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

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

// variables: int sensorValue = 0; // the sensor value int sensorMin = 1023; // minimum sensor value int sensorMax = 0; // maximum 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); }

Fading - Use an analog output (PWM pin) to fade an LED. Smoothing - Smooth multiple readings of an analog input.

In the main loop, sensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has an analogRead range from 0 to 1023, and an analogWrite range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called map():

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  // wait 2 milliseconds before the next loop for the analog-to-digital
  // converter to settle after the last reading:
  delay(2);
}

Few useful commands while mapping any analog output values:

  • map()

  • analogRead()

  • analogWrite()

  • serial()

  • AnalogInput - Use a potentiometer to control the blinking of an LED.

  • AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.

  • Calibration - Define a maximum and minimum for expected analog sensor values.

      /*
        Calibration
    
       Demonstrates one technique 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 minimum and maximum initial values may seem backwards. Initially,
        you set the minimum high and listen for anything lower, saving it as the new
        minimum. 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 30 Aug 2011
        by Tom Igoe
    
        This example code is in the public domain.
    
        http://www.arduino.cc/en/Tutorial/Calibration
      */
    
      // These constants won't change:
      const int sensorPin = A0;    // pin that the sensor is attached to
      const int ledPin = 9;        // pin that the LED is attached to
    
      // variables:
      int sensorValue = 0;         // the sensor value
      int sensorMin = 1023;        // minimum sensor value
      int sensorMax = 0;           // maximum 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);
      }
    
  • Fading - Use an analog output (PWM pin) to fade an LED.

  • Smoothing - Smooth multiple readings of an analog input.

Source Link
Sonali_B
  • 443
  • 4
  • 19

In the main loop, sensorValue is assigned to store the raw analog value read from the potentiometer. Arduino has an analogRead range from 0 to 1023, and an analogWrite range only from 0 to 255, therefore the data from the potentiometer needs to be converted to fit into the smaller range before using it to dim the LED.

In order to convert this value, use a function called map():

outputValue = map(sensorValue, 0, 1023, 0, 255);

outputValue is assigned to equal the scaled value from the potentiometer. map() accepts five arguments: The value to be mapped, the low range and high values of the input data, and the low and high values for that data to be remapped to. In this case, the sensor data is mapped down from its original range of 0 to 1023 to 0 to 255.

The newly mapped sensor data is then output to the analogOutPin dimming or brightening the LED as the potentiometer is turned. Finally, both the raw and scaled sensor values are sent to the Arduino Software (IDE) serial monitor window, in a steady stream of data.

// These constants won't change. They're used to give names to the pins used: const int analogInPin = A0; // Analog input pin that the potentiometer is attached to const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out)

void setup() { // initialize serial communications at 9600 bps: Serial.begin(9600); }

void loop() { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the Serial Monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 2 milliseconds before the next loop for the analog-to-digital // converter to settle after the last reading: delay(2); }

few useful commands while mapping any analog output values:

map()

analogRead()

analogWrite()

serial()

AnalogInput - Use a potentiometer to control the blinking of an LED.

AnalogWriteMega - Fade 12 LEDs on and off, one by one, using an Arduino or Genuino Mega board.

Calibration - Define a maximum and minimum for expected analog sensor values.

/* Calibration

Demonstrates one technique 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 minimum and maximum initial values may seem backwards. Initially, you set the minimum high and listen for anything lower, saving it as the new minimum. 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 30 Aug 2011 by Tom Igoe

This example code is in the public domain.

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

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

// variables: int sensorValue = 0; // the sensor value int sensorMin = 1023; // minimum sensor value int sensorMax = 0; // maximum 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); }

Fading - Use an analog output (PWM pin) to fade an LED. Smoothing - Smooth multiple readings of an analog input.