Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:07:26+00:00 2026-05-28T15:07:26+00:00

I was just looking at some example code and came across a line, I

  • 0

I was just looking at some example code and came across a line, I don’t fully understand why it needs to be done. I understand that you are taking in an analog value. This value is between 0 and 1024 apparently? Why is this? Why does the output need to be mapped between 0 and 255? What dictates the arguments that are used here? The line in question :

   // map it to the range of the analog out:
      outputValue = map(sensorValue, 0, 1024, 0, 255); 

Highlighted in the code :

created 29 Dec. 2008
 Modified 4 Sep 2010
 by Tom Igoe

 This example code is in the public domain.

 */

// 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, 1024, 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 10 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(10);                     
}

Thanks very much for the replies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:07:26+00:00Added an answer on May 28, 2026 at 3:07 pm

    The analog output only has an acceptable range between 0 and 255.

    Therefore, the value has to be mapped within the acceptable range.

    Documentation for map method is here: http://arduino.cc/en/Reference/map

    Because the Arduino has an analogRead resolution of 0-1023, and an analogWrite resolution of only 0-255, this raw data from the potentiometer needs to be scaled before using it…

    This explanation comes from an Arduino sensor tutorial(under the ‘Code’ header): http://arduino.cc/en/Tutorial/AnalogInOutSerial

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking at some PETSc example code, and I came across this snippet:
I was just looking at the CodeIgniter source code and I came across a
I was looking at some example C# code, and noticed that one example wrapped
I've done some Googling, and can't find anything, though maybe I'm just looking in
I'm looking at some vb.net code I just inherited, and cannot fathom why the
To debug some JavaScript code, I am looking for JavaScript code (preferably just js,
I was looking at some example C++ code for a hardware interface I'm working
I was looking at some sample code tutorials and noticed that on Javascript-coder.com, the
I was just looking through some information about Google's protocol buffers data interchange format.
I'm just looking for some direction when it comes to NHibernate and WCF. At

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.