This brightness variable is an integer number between 0 and 255. So now we have computed the brightness to apply to the LED. Apply brightness setting to LED analogWrite(LED_PIN, brightness) Using map() is great whenever you need to do operations like this, and it works for any integer range. With this line and the 5 arguments in the function, what we’re saying is: take the potentiometerValue from the range 0-1023, to the range 0-255. To do the same thing, you could also use the Arduino map() function which can put a number into a different range. ![]() Compute LED brightness with map (option 2) int brightness = map(potentiometerValue, 0, 1023, 0, 255) Nothing fancy here, it’s very simple and it works. So, if we want to put a value from the range 0-1023 to the range 0-255, we can just divide by 4. And as I really like simplicity, you can see that, roughly, 1024 is 255 multiplied by 4 (not exactly this value but this is a good enough approximate for what we need to do). So, before we use this function, we need to make sure the value is in the correct range. This analogWrite() function takes a byte value, or in other words, a number between 0 and 255. In order to control the LED brightness, we are going to use the analogWrite() function on the LED pin (later on). Compute LED brightness (option 1) int brightness = potentiometerValue / 4 For 0V you get 0, and for 5V you get 1023. The higher the voltage, the higher this number. ![]() This number corresponds to the voltage we read on the analog pin. In other words, this is a 10 bit number: 2^10 = 1024. We store the value inside an integer variable. To do that we use the analogRead() function, which takes one parameter: the pin number to read from. We enter the void loop(), and the first thing we do is to read the potentiometer value. Now that everything is setup, we can start to control the brightness of the LED with what we read from the potentiometer. However, for analog pins, no need to use pinMode() as those pins are already in INPUT mode! The potentiometer is a component we read from, so its mode is INPUT.The LED is a component we control, so we use pinMode() with OUTPUT.In the void setup(), we need to initialize the mode for the pins we want to use: This way, we can modify the circuit at any moment, all we’ll need to do in the code is to modify those 2 lines. One for the LED pin, one for the potentiometer. We start by a best practice, which is to create some defines (you could also create some const int variables) for the pins we are going to use in the program. Int potentiometerValue = analogRead(POTENTIOMETER_PIN) And the maximum position will correspond to the maximum brightness – LED with full intensity, same as if you used digitalWrite() with HIGH. The minimum knob position will correspond to the minimum brightness – LED turned off. In this application, what we want to do is simple: when we turn the knob up (for example turning clockwise), we want the LED brightness to increase. Add a wire between the middle pin and an analog pin.Īrduino code to control LED brightness with the potentiometer.Connect the other extreme leg to 5V on the Arduino.Connect the extreme left (or right) leg to GND.Plug the 3 legs of the potentiometer to 3 different lines on the breadboard. ![]() Here on Arduino Uno you can choose between pins 3, 5, 6, 9, 10, and 11 – you can recognize PWM compatibility with the “~” next to the pin number. Add a wire between the other side of the resistor and a PWM-compatible digital pin (so we can control the brightness).From this leg, add a 220 Ohm resistor to yet another line.Plug the other (longer) leg of the LED to an independent line on the breadboard.You can directly plug this leg into the “minus” line of the breadboard, or add a small black wire. Connect the shorter leg of the LED to the ground.From this “minus” line, then we will be able to connect all other grounds, which will make things easier to manage. To do this we will first plug a black wire (black is convention for GND) between a GND pin of the Arduino and the “minus” line on the breadboard. It’s very important to make a common ground for all components. As a best practice we’ll start with the ground (GND).You are learning how to use Arduino to build your own projects?Ĭheck out Arduino For Beginners and learn step by step. For this tutorial I will use an Arduino Uno board. Conclusion – Control LED brightness with potentiometerĪrduino circuit with LED and potentiometer.Compute LED brightness with map (option 2).Arduino code to control LED brightness with the potentiometer.Arduino circuit with LED and potentiometer.
0 Comments
Leave a Reply. |