Search Code

Lesson 14/31-5: Arduino coding map() example 2

Lesson 14/31-5: Arduino coding map() example 2

This tutorial demonstrates how to use the Arduino map() function to control an LED's brightness using a percentage value (0–100%) instead of the raw 0–255 range required by analogWrite(). This is a fundamental technique that simplifies your code and makes it much more intuitive, as you can think in terms of percentages rather than arbitrary byte values.

Sunfounder car assembled

This skill is essential for any project where you need to scale sensor readings or control signals. For example, you could use it to:

  • Control the brightness of an LED or the speed of a motor based on a potentiometer's reading.
  • Convert a temperature sensor's output (e.g., 0–1023) to a Celsius or Fahrenheit range.
  • Map a joystick's analog input to control the position of a servo motor.
  • Create a simple user interface where a percentage value is used to set a device's power level.

By the end of this lesson, you'll be able to write cleaner, more maintainable code that is easier to read and adjust.

Hardware/Components

This project uses a minimal set of components, making it perfect for beginners. You will need:

  • An Arduino board (e.g., Uno).
  • An LED.
  • A 220-ohm resistor.
  • Jumper wires.
  • A breadboard (optional but recommended).

Wiring Guide

The wiring for this project is very simple, as demonstrated in the video (in video at 20:47). The LED is connected to pin 3 of the Arduino through a 220-ohm current-limiting resistor.

Connect the anode (longer leg) of the LED to pin 3 through the resistor. Connect the cathode (shorter leg) of the LED to the ground (GND) pin on the Arduino.

Code Explanation

This example focuses on a user-defined function that simplifies controlling the LED's brightness. The code is built around a custom function called pwmValue(), which is the key to making the code intuitive.

Let's break down the configurable parts of the code:

int LEDpin = 3; //defind pin for LED

This line defines the Arduino pin to which the LED is connected. You can change the number 3 to any other PWM-capable pin on your Arduino board.

The core of the project is the custom function pwmValue(). This function takes a percentage value as its input and converts it to the 0–255 range used by analogWrite(). This is where the map() function does its magic.

int pwmValue(int percent)
{
  if(percent >=0 && percent <=100)
  {
    return map(percent, 0,100,  0,255);
  }else
  {
    return 0;
  }
}

To use this function, you simply call it with your desired brightness percentage. The function will return the correct value for analogWrite(). For example, pwmValue(60) will return 153, which corresponds to 60% of 255. The function also includes a safety check to ensure the input percentage is between 0 and 100, returning 0 if it is outside this range.

In the setup() function, you can see how this is used:

int b=5;//60% brightness
Serial.println( pwmValue(b)   );//print it on serial monitor
analogWrite(LEDpin, pwmValue(b));// turn the LED on with 60% brightness

Here, the variable b is set to your desired brightness percentage. The function pwmValue(b) is then used to both print the mapped value to the Serial Monitor and to set the LED's brightness. To change the LED's brightness, you only need to modify the value of b. The map() function handles all the necessary calculations for you.

Live Project/Demonstration

In the video, the presenter demonstrates the project by changing the value of the b variable and uploading the code to the Arduino. When b is set to 60, the code maps it to 153 and the LED lights up at 60% brightness. Changing b to 90 maps to 229, making the LED noticeably brighter. Setting it to 100 maps to 255, which is the maximum brightness. The demonstration clearly shows how the map() function allows you to control the LED using an intuitive percentage value, while the Serial Monitor confirms the exact 0–255 value being sent to the pin (in video at 21:29).

Related projects from this video

Chapters

  • [00:00] Introduction to Millis and map functions
  • [01:19] What is Millis and why use it
  • [05:46] Using Millis with an event to take action
  • [08:35] Millis example 2 with a push button
  • [12:14] Millis example 3 checking for two button presses
  • [17:20] Introduction to the map() function
  • [19:47] Code example for mapping a percentage to LED brightness
  • [20:47] Live demonstration of the map() function
  • [22:41] Creating a custom function to handle mapping

Images

Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
Sunfounder car assembled
920-Lesson 14/31-5: Arduino coding map() example 2
Language: C++
/*
Lesson 14/31-5: Arduino coding map( ) example-2
Get this code and watch video Download and resource page https://robojax.com/RJT597
YouTube video https://www.youtube.com/watch?v=lqgzQohNiIM
 
  
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
int LEDpin = 3; //defind pin for LED
                   
 
 void setup() {
    Serial.begin(9600);//initialzie serial monitor with 9600 baud
    pinMode(LEDpin, OUTPUT);//set pin as output
    int b=5;//60% brightness

    Serial.println( pwmValue(b)   );//print it on serial monitor
    analogWrite(LEDpin, pwmValue(b));// turn the LED on with 60% brightness   
 }

 void loop() {

 
 }

/*
 * pwmValue(int percent)
 * @param precent is value between 0 to 100
 * @return it returns the mapped value from 0 to 255
 * written by Ahmad Shamshiri on Dec 9, 2022
 * 
 */
 int pwmValue(int percent)
 {
  if(percent >=0 && percent <=100)
  {
    return map(percent, 0,100,  0,255);
  }else
  {
    return 0;
  }
 } //pwmValue end
 

Resources & references

Files📁

User’s Manual