In this post, I will teach you how to control the brightness of several LEDs using three buttons and an Arduino. This project is an excellent way to learn about the use of PWM (Pulse Width Modulation) signals to vary the brightness of an LED.
Each button will have a specific function: one will allow you to switch between the LEDs, another will increase the brightness, and the third will decrease it. It is a practical exercise that combines basic concepts of electronics and programming, ideal for those looking to improve their skills with Arduino.
MATERIALS:
The materials we will use for this project are as follows:
- 1 Arduino Uno.
- 3 Buttons
- 3 Resistors of 1k
- 4 Resistors of 220 Ohms.
- 4 LED diodes (Any color).
WIRING DIAGRAM:
EXPLANATION OF THE FUNCTIONING:
In this project, it is essential that the LEDs are connected to pins that support PWM (Pulse Width Modulation). The buttons will be connected to pins A0, A1, and A2 respectively.
To control the brightness of the LEDs, we will adjust the PWM value. Instead of using specific functions, we will create a variable called pwm. This value will increase or decrease depending on the button we press. If we press the button connected to pin A1, we will increase the value of pwm. On the other hand, if we press the button connected to pin A2, we will decrease it. The selection of the LED we want to control is done with the button connected to pin A0.
The value of pwm for each LED is stored independently using an array, allowing each LED to have its own brightness value. This approach also applies to managing the LEDs, ensuring organized and efficient management.
COMPLETE CODE:
/** Code written by: Esteban Carrillo for EDEPTEC.* Website: https://www.edeptec.com* Facebook: @edeptec* Youtube: https://youtube.com/c/EDEPTEC*//* We define the pins where the buttons are connected */#define pulCambio A0#define pulSuma A1#define pulResta A2byte ledPins[] = {3, 5, 6, 9}; // Pins where the LEDs are connectedbyte numeroDeLeds = sizeof(ledPins); // Number of LEDs in the arraybyte pwmLeds[sizeof(ledPins)]; // Array to store the PWM value of each LEDbool previousState = 0; // Variable to control the previous state of the buttonint pwm;// Variable to store the value of the pwmintx;// Variable to control the current ledvoid setup() {//--- We declare inputs and outputs ---//pinMode(pulCambio, INPUT);pinMode(pulSuma, INPUT);pinMode(pulResta, INPUT);/* We configure the led pins as outputs */for (byte x = 0; x < numeroDeLeds; x++) {pinMode(ledPins[x], OUTPUT);}}void loop() {bool statePulChange = digitalRead(pulChange); // Logical state of the buttonif (buttonState != previousState) { // Debounce implementationif (buttonState == HIGH) { // When the LED change button is pressedx++; // Change LEDif (x == numberOfLeds) x = 0; // If we reach the last LED, go back to the firstpwm = pwmLeds[x]; // Get the PWM value of the current LED and store it in the pwm variable}delay(10); // Pause of 10ms}previousState = statePulChange; // Debounce implementation(digitalRead(pulSum) == HIGH) && pwm++; // If we press the add button, we add the value of the pwm variable(digitalRead(pulResta) == HIGH) && pwm--; // If we press the subtract button, we subtract the value of the pwm variable// We limit the value of the pwm variableif (pwm > 255) pwm = 255;if (pwm < 0) pwm = 0;pwmLeds[x] = pwm; // We store the value of the pwm variable in the position of the array corresponding to the current ledanalogWrite(ledPins[x], pwmLeds[x]); // We send through the pin where the LED is connected, the value of the PWM that belongs to it}
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.