Lesson 14/31-4: Arduino coding map() example 1
This tutorial is part of a series on Arduino programming and focuses on the practical use of the map() function. In this lesson, you will learn how to convert a percentage value (0–100) into a PWM (Pulse Width Modulation) value (0–255) to control the brightness of an LED. Instead of manually calculating that 60% equals 153, the map() function handles the linear conversion for you, making your code cleaner and more intuitive.
This technique is essential for many real-world applications where you need to translate sensor readings or user inputs into different ranges. For example, you could use it to:
- Control the speed of a DC motor based on a potentiometer reading.
- Display a temperature sensor's raw value on a 0-100% scale.
- Map joystick coordinates to servo motor angles.
- Adjust LED brightness based on an ambient light sensor.
- Convert a 0-5V analog reading to a 0-1023 digital value or vice versa.
Hardware/Components
To follow along with this project, you will need the following components:
- Arduino board (Uno, Nano, etc.)
- An LED
- A 220-ohm resistor
- Jumper wires
- A breadboard (optional, but recommended)
Wiring Guide
In the video, the instructor demonstrates the project using a wiring setup from a previous lesson on LED fading. The circuit is straightforward: connect the anode (long leg) of the LED to a 220-ohm resistor, and connect the other end of that resistor to digital pin 3 on the Arduino. Connect the cathode (short leg) of the LED directly to the ground (GND) pin on the Arduino.
Code Explanation
The code for this example is very concise, with the core logic residing in the setup() function. Let's break down the user-configurable parts.
int LEDpin = 3; //defind pin for LED
This line defines the Arduino pin to which your LED is connected. In this example, it is set to pin 3, which is a PWM-capable pin, necessary for analog output. You can change this number if you connect your LED to a different PWM pin.
int b=8;//60% brightness
This is the key variable you will modify to test different brightness levels. It represents the desired brightness as a percentage. The comment in the code says "60%", but the value is set to 8 for demonstration purposes in the video. You can change this to any integer between 0 and 100. For example, setting b = 60 would represent a 60% brightness level.
int brightness = map(b, 0, 100, 0,255);
This is the heart of the lesson. The map() function takes your percentage value (b) and converts it from its original range (0 to 100) to a new target range (0 to 255). The result is stored in the brightness variable, which is then used to write an analog value to the LED pin.
analogWrite(LEDpin, brightness);// turn the LED on with 60% brightness
Finally, this line sends the mapped value to the LED pin using analogWrite(). This sets the LED's duty cycle, effectively controlling its brightness. The beauty of this approach is that you only need to change the b variable to control the LED, and the map() function handles all the necessary calculations.
Live Project/Demonstration
In the video, the instructor uploads the code and demonstrates the effect of changing the b variable. When b is set to 8, the serial monitor prints the mapped value of 20, and the LED is visibly dim. Changing b to 90 results in a mapped value of 229, making the LED much brighter. Setting b to 100 maps to 255, which is full brightness. This practical demonstration clearly shows how the map() function simplifies the process of controlling analog outputs with user-friendly percentage values.
Related projects from this video
- Lesson 14/31-1: Arduino Millis (no delay) and map functions | SunFounder Robojax
- Lesson 14/31-2: Arduino coding millis() example 2 with event
- Lesson 14/31-3: Arduino coding millis() example 3 with push button
- Lesson 14/31-5: Arduino coding map() example 2
Chapters
- [00:00] Introduction to the Arduino Course by Robojax
- [01:19] What is millis() and why use it
- [05:46] Example 1: Using millis() for a timed event
- [08:29] Example 2: Using millis() with a push button
- [12:14] Example 3: Detecting a second button press after a delay
- [17:20] Introduction to the map() function
- [19:47] Code walkthrough for the map() example
- [20:41] Live demonstration of the map() function
/*
Lesson 14/31: Arduino coding map( ) example-1
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);//initialize serial monitor with 9600 baud
pinMode(LEDpin, OUTPUT);//set pin as output
int b=8;//60% brightness
int brightness = map(b, 0, 100, 0,255);
Serial.println(brightness);//print it on serial monitor
analogWrite(LEDpin, brightness);// turn the LED on with 60% brightness
}
void loop() {
}
Things you might need
-
AmazonSunFounder Arduino Learning Kit on AliExpresss.click.aliexpress.com
-
Amazon
-
Amazon
-
Amazon
-
Amazon
Resources & references
-
DocumentationDocumentation for SunFounder 3 in 1 IoT/Smart Car/Learning Kitdocs.sunfounder.com
-
ExternalPurchase SunFounder's 3-in-1 Smart car learning kitsunfounder.com
Files📁
User’s Manual
-
SunFounder 3-in-1 Arduino Smart Car assembly manualThis document shows step by step assembly of SunFounder 3-in-1 Arduino Smart Car
SunFounder_car_assembly_manual.pdf1.20 MB