In this post, we will see how to control (vary) the brightness of an LED using an Arduino Uno as a microcontroller, a potentiometer, and an LED.
MATERIALS:
The materials we will use for this project are as follows:
- 1 Arduino Uno.
- 1 Potentiometer (Any value).
- 1 220 Ohm resistor.
- 1 LED (Any color).
WIRING DIAGRAM:
EXPLANATION OF THE FUNCTIONING:
For this project, the LED must be connected to a PWM pin and the potentiometer to an analog pin.
To control the brightness of the LED, we modify the PWM value. We modify the PWM value with the values given by the potentiometer.
Initially, the potentiometer connected to an analog pin gives us values ranging from 0 to 1024; in this case, we need values from 0 to 255. To obtain these values, we use the map() function.
map() function:
This function is widely used in Arduino projects, as it allows us to "make equivalences" between different ranges.
The structure of this function is as follows:
pwm = map(value I receive, inmin, inmax, outmin, outmax);
- pwm:Variable that stores the values returned by the map function.
- value I receive: Value we read from the analog pin.
- inmin:Minimum value delivered by the analog pin.
- inmax:Maximum value delivered by the analog pin.
- outmin:Minimum value that the variable will takepwm.
- outmax:Maximum value that the variable will takepwm.
CODE:
#define led 3 // Pin where the led is connected
#define pot A0 // Pin where the potentiometer is connected
void setup() {
pinMode(led,OUTPUT); // We declare inputs and outputs
pinMode(pot,INPUT);
}
void loop() {
int pwm; // We declare a variable, in this variable we store the values that the map function gives us
pwm = map(analogRead(pot), 0, 1024, 0, 255);
analogWrite(led,pwm); // We turn on the led with the pwm value that the map function sends us
delay(100); // we add a small pause
}
Post a Comment
Hello! We're so glad you've made it this far and are reading this article on Edeptec.
This form is an open space for you: you can leave a comment with your questions, suggestions, experiences, or simply your opinion on the topic discussed.
» Did you find the information helpful?
» Do you have any personal experiences you'd like to share?
» Do you have any topics you'd like to see covered in future articles?
Remember that this space is for learning and sharing, so we encourage you to participate respectfully and constructively. Your comments can help other readers who are on the same path, whether in electronics, programming, sports, or technology.
Thank you for being part of this learning community! Your participation is what makes this project grow.