Introduction
For those beginning to learn about robotics, particularly in the area of building circuits, you may have come across the question of how to change the brightness of a LED, without having to keep switching parts. Quite simply, the solution to this issue is a potentiometer.
Potentiometers are variable resistors and they function to alter their resistance via a knob or dial. You have probably used one before by adjusting the volume on your stereo or using a light dimmer.
Potentiometers have a range of resistance. They can be attuned from zero ohms to whatever maximum resistance that is specific to it. For example, a potentiometer of 10 kΩ can be adjusted from 0 Ω to its maximum of 10 kΩ.
In this tutorial you will learn how to use a potentiometer with and without Arduino board to fade an LED.
You will also learn how to use analogRead() and map() functions.
Potentiometers are variable resistors and they function to alter their resistance via a knob or dial. You have probably used one before by adjusting the volume on your stereo or using a light dimmer.
Potentiometers have a range of resistance. They can be attuned from zero ohms to whatever maximum resistance that is specific to it. For example, a potentiometer of 10 kΩ can be adjusted from 0 Ω to its maximum of 10 kΩ.
In this tutorial you will learn how to use a potentiometer with and without Arduino board to fade an LED.
You will also learn how to use analogRead() and map() functions.
How to use a potentiometer
Connect battery to outer pins of potentiometer and the positive end of led (larger pin) to middle pin. Now turn the knob (or dial) left and right. It changes the brightness of the led! Now let's see how we can connect the potentiometer with the arduino uno
What you will need - Hardware
For this tutorial you will need:If you would like you can also use breadboard shield for arduino uno.
|
The Circuit
The connections are pretty easy, see the image above with breadboard circuit schematic.
The code
Here's the code, by turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative "closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.
Ηow it works:
Ηow it works:
- Read analog value from potentiometer middle pin
-> value=analogRead(potPin) - Map analog values 0-1024 to pwm values 0-255
-> value = map(value, 0, 1023, 0, 255); - Send pwm value to led
-> analogWrite(ledPin, value);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /* Learn how to use a potentiometer to fade an LED with Arduino - Tutorial More info and circuit schematic: http://www.ardumotive.com/arduino-tutorials/arduino-fade-led Dev: Michalis Vasilakis / Date: 25/10/2014 */ //Constants: const int ledPin = 9; //pin 9 has PWM funtion const int potPin = A0; //pin A0 to read analog input //Variables: int value; //save analog value void setup(){ //Input or output? pinMode(ledPin, OUTPUT); pinMode(potPin, INPUT); //Optional } void loop(){ value = analogRead(potPin); //Read and save analog value from potentiometer value = map(value, 0, 1023, 0, 255); //Map value 0-1023 to 0-255 (PWM) analogWrite(ledPin, value); //Send PWM value to led delay(100); //Small delay } |
Download the code from here and open it with Arduino IDE.
|
|
Well done!
You have successfully completed one more "How to" tutorial and you learned so far how to use:
Congratulations you have become an Arduino developer!
- LEDs
- potentiometers
- pinMode(), delay(), map(), digitalWrite(), analogWrite() and analogRead() functions
- variables and constants
- if statement
Congratulations you have become an Arduino developer!