Arduino Potentiometer LED: A beginners project on controlling and fading an LED using an
potentiometer as an input control (by using the ADC to read it)
Introduction
In this Arduino potentiometer LED beginners tutorial. We'll build a
simple dimmable light circuit and learn some basic Arduino programming
along the way. You will learn how to use a potentiometer to control the
brightness of an LED connected to an Arduino.
A potentiometer, commonly referred to as a "pot", is a simple
3-terminal resistor component that allows you to adjust or select a
varying level of resistance.
It works by having a movable wiper contact that slides along a resistive
track connecting the outer terminals. As the wiper slides, it taps into
different points along this track to give a range of resistances
between the minimum and maximum. So if you have a 10k Ohm pot, and set
the wiper to the middle position, you have 5k Ohms resistance from the
middle to one end, and 5k Ohms from the middle to the other end.
The most common usage of the potentiometer is to divide voltage. When
the wiper is in the middle and you apply 5V to one end and 0V to the
other you will get a voltage of 2.5V at the middle wiper. If you changed
to 1k and 9k - moving the shaft so that the wiper moves nearer to one
end you would get 0.5V output at the wiper. You can vary the voltage to
any value between 0V and 5V.
Physically, a potentiometer looks like a dial or knob you can turn.
Turning the shaft moves the wiper and changes the resistance. They
typically come in common values like 10k Ohms, which is what we'll be
using here.
TIP:
Potentiometers come in two flavors log and linear. The log types are
used in audio circuits as our ears react logarithmically so for this
project you need a linear pot.
By reading the potentiometer value with the Arduino, we can map it to PWM (Pulse Width Modulation) to fade the LED from off to full brightness.
Required Components
- Arduino Uno.
- 10k ohm potentiometer.
- LED (any color).
- 220-330 ohm resistor (choose one value) - you can use 1k.
- Jumper wires.
Arduino potentiometer LED: Circuit Diagram
The layout diagram shows how to place components on the breadboard.
Ground (0V) and supply voltage (5V) are connected from pins in the lower
pin header of the Arduino Uno, while analogue input pin A1 connects to
the middle wiper of the pot.
The LED is connected from pin 5 via the 1k resistor.
Note that you have to place the LED the right way round -the flat side
of the LED (cathode - or negative side) connects to ground (GND or 0V).
Diagram using fritzing
Diagram using fritzing
Arduino potentiometer LED: Using Arduino analogRead
The Arduino function analogRead is a function that sets up your
Analog capable pin (choose from A0 to A5) as an analog input. Note that
these pins also have digital capability, so you can use them as
digital-only Input or Output i.e. using function digitalWrite and
digitalRead. However, in a single project you need to choose whether the
pin will be used as input, output or analog input - here you need
analog input.
The analogRead function does all the hard work in the background and starts the internal ADC getting the reading and waits for that reading to complete ~100us.
Arduino potentiometer LED: Example Sketch
You can copy and paste the code below into the Arduino IDE (in a new
sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.