Arduino Mega - Potentiometer fade LED
This guide shows you how to use the Arduino Mega to make an LED brighter or dimmer based on the potentiometer’s setting.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Buy Note: Use the LED Module for easier wiring. It includes an integrated resistor.
Overview of LED and Potentiometer
If you're new to using the LED, Potentiometer, and Arduino Mega, please check out these tutorials:
- Getting Started with Arduino Mega tutorial
These tutorials explain how LED and Potentiometer work, their pinouts, how to connect them to the Arduino Mega, and how to program Arduino Mega to work with the LED and Potentiometer.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
The diagram below shows the same connections without a breadboard:

This image is created using Fritzing. Click to enlarge image
How To Program
- Reads the input from the analog pin A0 (values from 0 to 1023).
- Turn it into a brightness level (a number from 0 to 255)
- Sets how bright the LED on pin 2 is.
Arduino Mega Code
Detailed Instructions
Follow these steps one by one.
- Connect the parts according to the diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Choose the right Arduino Mega board (for example, Arduino Mega) and the COM port.
- Copy the code and open it in the Arduino IDE.
- Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
- Open the Serial Monitor.
- Turn the potentiometer.
- Watch the LED.
- Check the result in the Serial Monitor.