list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!
Description of the Arduino LED Brightness Control Code
This Arduino code allows you to control the brightness of an LED using a potentiometer. The potentiometer is connected to analog pin A0, and the LED is connected to digital pin 6. The program reads the analog value from the potentiometer, which ranges from 0 to 1023, and maps this value to a range suitable for controlling LED brightness (0 to 255). This is necessary because the analogWrite()
function used for controlling the LED can only accept values from 0 (completely off) to 255 (maximum brightness).
In the setup()
function, the LED pin is configured as an output. The loop()
function continuously reads the potentiometer value, maps it to the appropriate range, and then uses PWM (Pulse Width Modulation) to set the brightness of the LED accordingly. A small delay is included to ensure the processor is not overloaded with rapid readings.
This project is an excellent introduction to using analog inputs and PWM with Arduino, making it a valuable learning experience for beginners interested in electronics and programming.
SCHEMATIC:

CODE:
int led_pin = 6; // Define the pin number connected to the LED
int pot_pin = A0; // Define the pin number connected to the potentiometer
int output; // Variable to store the raw analog reading from the potentiometer
int led_value; // Variable to store the mapped LED brightness value
void setup() {
pinMode(led_pin, OUTPUT); // Set the LED pin as an output
}
void loop() {
// Reading the potentiometer value (ranges from 0 to 1023)
output = analogRead(pot_pin);
// Mapping the potentiometer value from the range 0-1023 to 0-255
// because analogWrite can only accept values from 0 (off) to 255 (full brightness)
led_value = map(output, 0, 1023, 0, 255);
// Set the LED brightness using PWM (Pulse Width Modulation)
analogWrite(led_pin, led_value);
// A small delay to avoid overloading the processor
delay(1);
}
0 Comments