Type Here to Get Search Results !

Complete Guidance of How to Control Brightness of an LED With Arduino UNO R3

We already saw that one challenge that we face when using a value from an analog input and sending it to an analog output, is that the analog read () function in our case will return a value in the range of 0 to 1023


While analogwrite() function works with values in the range of 0 to 255 as the input for the duty cycle to fix this, we will use again the map function. With the parameters 0 to 1023 as the original range and 0 to 255 as the new needed range. That’s all, let’s see that in action, meaning the new updated sketch step-2b and the actual result using the hardware.





Sketching Code For Led Brightness In Arduino Software


So in this step-2b, we would like to take the values that we got from the potentiometers. The speed and the brightness levels, and not just to print it in the serial monitor, we would like to apply that in our project, to actually play and set the leds according to that selection.

So i added few new lines in the code so i will scroll down to the relevant function that is called "checkwavebutton", that was the original two lines that was responsible for getting the potentiometers values, using the "analogread()" function, and i added two additional lines that are utilizing the "map()" function that help us to map the original speedlevel to the needed range.

So in that case, that was the original range that is coming from the analogread, and that's the target range, the same goes to the brightness, just different range, because we are going to use the brightness level as input to the analogwrite().

That will help us to emulate the brightness with pwm and that's the input we need to provide, so after mapping those two values, then we can use that so i will scroll down again, and looking on that code you will see that i put the digitalwrite() as a comment so it is not being used any more, and replaced that with a new code called "analogwrite" providing again the same pin number, from the array with the relevant index, and the brightness level as a parameter, and the next line, delay is now using not a constant value but the speed level, as input parameter, and i am swithing that to off using the digitalwrite(),

I could also use the analogwrite() while providing 0 as duty cycle. And that just the other side again i am using the analogwrite, the delay with the new parameter and then switch that off. Very simple overall and now let's see the result in our project.




Arduino Coding For Controlling Of Led Brightness By Potentiometer


We added new lines of code that will help us to actually change the speed and led's brightness, and let's actually test that in the project itself, so the first button is of course testing on/off, no change in that behavior,

However the second one, this one will run animation wave, you see that it is running fast, so if you remeber that's the speed potentiometer, let change it, ok so i will change that a little bit to the left, and I am expecting much slower animation wave, Now the second potentiometer is used for led's brightness,

Now it is set almost to zero, if i will turn that up to the maximum to the right and then press this button again, you see that the leds are displaying in much stonger light,

I hope that you can see that in the video, maybe i will turn that back and play that again now it is much dimmer from the last one so that's all. We have the two analog sensors one for speed and one for brighness we learned how to use that in our sketch, to map the relevant range, that is super important when we are working with sensors, and of course to use a method that is called pwm to be able to emulate analog and analog output, because we don't have analog output pin in our arduino.



Post a Comment

0 Comments