Learn to code

ARDUINO & CODES

Previous Post
Reading analog values with Arduino
Next Post
Introduction into visual output with Arduino

Using the map function for analog results with Arduino

in Arduino Digital / Analog

Recall that analogRead() returns a value between 0 and 1023. If you don’t then please read Reading analog values. We will use the input value we are going to read from a potentiometer as the output value for the analogWrite() function. In the last tutorial, we divided the value received by analogRead() by 4. Since analogWrite() only accepts values in the range of 0 through 255 and the value that can be recorded by analogRead() is much larger we need to solve this problem.

One solution to this problem is to rescale the values within the analogWrite() function instead of dividing the received value by 4. We call this the map() function.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Dupont Wires Dupont Wires × 1
10K Potentiometer 10K Potentiometer × 7
220 ohm resistor 220 ohm resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Generic Code Map Function

So, the map() function will remap a number from one range to another.

map(value, fromLow, fromHigh, toLow, toHigh)

value: the number to map.
fromLow: the lower bound of the values range
fromHigh: the upper bound of the values range
toLow: the lower bound of the values target range.
toHigh: the upper bound of the value’s target range.

In the case of using analogRead() to write the values with analogWrite() we are going to convert the range 0 -1023 to 0 -255. This is demonstrated in the example code below.

/* Map an analog value to 8 bits (0 to 255) */
void setup() {}
void loop() {
  int val = analogRead(0);
  val = map(val, 0, 1023, 0, 255);
  analogWrite(9, val);
}

Now we know the basics of the map() function we can apply this to our code made in the previous tutorial. We will use the same circuit diagram.

BreadBoard Layout

  1. Place the potentiometer into the breadboard pictured in the circuit diagram above.
  2. Run a hook up wire from the 5V pin of the Arduino to the positive power rail of the breadboard
  3. Run a hook-up wire from the GND pin of the Arduino Board to the negative rail of the breadboard.
  4. Connect hook-up wires from the power rails to the potentiometer.
  5. Final for the potentiometer, attach a hook-up wire from pin A0 on the Arduino Board to the middle pin of the potentiometer.
  6. For the LED, place the 220-ohm resistor on the breadboard.
  7. Connect a hook-up wire from Pin 3 of the breadboard to one end of the resistor.
  8. Place the LED with the long leg to the other end of the resistor.
  9. Connect the short leg with a hook-up wire to the GND power rail of the breadboard.
  10. Connect the Arduino Board with your computer.
  11. Open the Arduino IDE.
  12. Open a new sketch and copypast the code sketch code below
  13. Click the Verify button on the top left side of the screen to be sure that there are no problems
  14. Click the Upload button (next to the Verify button).
  15. Open the serial monitor window, you should see sensor data on the Serial Monitor.
  16. As you adjust the shaft of the potentiometer in order to control the brightness of the LED.

The Code

// code for controlling the brightness of LED by a Potentiometer with the map() function.
int potPin = A0 ; // analog pin from the potentiometer
int LED = 3; // pin that is connected to the LED
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(potPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(LED, outputValue);
  // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.println(sensorValue);
  Serial.print("output = ");
  Serial.println(outputValue);
 
  delay(2);
}

Code Explanation

Our sketch begins with declaring two variables, one for the potentiometer and one for the LED.

int potPin = A0 ; // analog pin from the potentiometer
int LED = 3; // pin that is connected to the LED

Next, we create variables for storing the input and output data. We need those since we are converting input to output data for controlling the britghtness of the LED.

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

A quite easy part of this sketch is the setup() part. We only initiate serial communication for printing to the Serial Monitor.

// initialize serial communications at 9600 bps:
  Serial.begin(9600);

The last part of our sketch is the loop(). Firstly, the loop() reads the analog pin A0 and assigns the value that is read to sensorValue.

// read the analog in value:
  sensorValue = analogRead(potPin);

Now it is time to apply the our the knowledge we have learned by remapping our sensorValue to values to can be used by analogWrite(). As you may recall, analogWrite() takes two arguments 1) a pin number 2) the value to write. This function utilizes pulse width modulation. if you are unfumilier with this you can read it back at Arduino Tutorial: 5.2 Adjust the brightness of an LED with PWM

 
// map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value:
  analogWrite(LED, outputValue);

Let us summarize what we have done:

  1.  we read the input from analog pin 0
  2. adjusted the value with the map() function to use it for analogWrite()
  3. Passed the value to analogWrite() which adjust the brightness of the LED.

But what is a program without some type of data output? We get our fix by writing the input and output values to the serial port.

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

The println() function prints the outputValue variable and then starts a new line in the serial monitor window.

The last line of the sketch is the delay() function, which allows the analog-to-digital converter to stabilize before we ask a new sensor read.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu