Introduction
In this article I have explained about controlling LED using Arduino Mega 2560. Also, controlling the brightness of LED using Arduino Mega 2560 will be described.
Parts
- Arduino Mega 2560
- Potentiometer
- BreadBoard
- LED
Connection
Step 1: Connection from Potentiometer to Arduino Mega 2560:
- The first pin of the potentiometer can be connected to the Gnd of the Arduino Mega 2560.
- The second pin of the potentiometer can be connected to the Analog pin A0 of the Arduino Mega 2560.
- The third pin of the potentiometer can be connected to the 5V of the Arduino Mega 2560.
Step 2: Connection From Potentiometer To Arduino Mega 2560:
- Postive pin = 09
- Negative pin = Gnd
Programming
- const byte POTENTIOMETER = 0;
- const byte CONTROL = 9;
- int reading;
- int value;
- void setup()
- {
- pinMode(CONTROL, OUTPUT);
- }
- void loop()
- {
- reading = analogRead(POTENTIOMETER);
- value = map(reading, 0, 1024, 0, 255);
- analogWrite(CONTROL, value);
- }
Explanation
- When the potentiometer is adjusted the LED can be low to high
- In the potentiometer we can see the brightness of the LED.
Output
Figure 1: Output
Read more articles on Arduino: