Skip to main content

How to use a Potentiometer



Variable Resistor


Introduction 

Picture


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.

How to use a potentiometer

Picture
All potentiometers have three pins. The outer pins are used for connecting power source (Vref and gnd). The middle pin (output) give us the variable of resistance value.
Let's see it in practice, you will need:
  • potentiometer
  • led
  • battery AAA 1.5 (or another but no more than 5V)
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:
  • Arduino uno
  • Breadboard
  • Potentiometer (e.g. 4.7K)
  • LED
  • 220 Ohm resistor
If you would like you can also use breadboard shield for arduino uno.
Picture

The Circuit

Picture
The connections are pretty easy, see the image above with breadboard circuit schematic.

The code

Here's the 'Fade an LED with potentiometer' 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:

  • 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);

****************************************************************************************'

/* Learn how to use a potentiometer to fade an LED with Arduino - Tutorial  */
   

//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
  
}
****************************************************************************************



Well done!

You have successfully completed one more "How to" tutorial and you learned so far how to use:
  • LEDs
  • potentiometers
  • pinMode(), delay(), map(), digitalWrite(), analogWrite() and analogRead() functions
  • variables and constants
  • if statement
I hope you liked this, let me know in the comments.
Congratulations you have become an Arduino developer!

Comments

Popular posts from this blog

Arduino ​Obstacle Avoiding Robot Ardunio Computer

Build Your Own Arduino Weather Station

Complete Guide for Nokia 5110 LCD with Arduino