Related Terminologies

Table of Contents

Controlling of a LED using potentiometer

Twitter
LinkedIn
Print

Table of Contents

I’ll show you how to use a potentiometer to control an LED, specifically how to control the brightness, in this Arduino tutorial.

When you turn the potentiometer knob, you’ll learn how to make the LED fade in and out in a dynamic manner.

Check out this Arduino LED tutorial and this Arduino Potentiometer tutorial first if you are not yet familiar with the LED or potentiometer.

Table of Contents

Arduino circuit with LED and potentiometer

Let’s first build the circuit. You will need those components:

  • Arduino board – any will do. For this tutorial I will use an Arduino Uno board.
  • Breadboard.
  • LED – any color.
  • 220 Ohm resistor.
  • Potentiometer.
  • A bunch of wires (male to female).

Here is the circuit.

Steps to build the circuit:

We’ll start with the ground (GND) as a best practice. Establishing a common ground for all components is crucial. To do this we will initially plug a dark wire (dark is show for GND) between a GND pin of the Arduino and the “short” line on the breadboard. We will then be able to connect all other grounds using this “minus” line, making management easier.

Add the LED:

The LED’s shorter leg should be connected to the ground. Either insert a small black wire or directly plug this leg into the breadboard’s “minus” line.
Connect the LED’s other, longer leg to a separate line on the breadboard.
Add a 220 Ohm resistor to yet another line from this leg.
Connect a wire to a digital pin that is compatible with PWM on the other side of the resistor so that we can control the brightness. You can select between pins 3, 5, 6, 9, 10, and 11 on the Arduino Uno; the “” next to each pin number indicates whether or not it is compatible with PWM.

Add the Potentiometer:

Connect the potentiometer’s three legs to three different breadboard lines.
Connect GND to the extremity of the left (or right) leg.
Connect the Arduino’s other extreme leg to 5V.
Connect an analog pin and the middle pin with a wire.

Arduino code to control LED brightness with the potentiometer

Our goal in this application is straightforward: We want the LED brightness to rise when we raise the knob, for example by turning it clockwise.

When the LED is turned off, the position of the minimum knob will correspond to the minimum brightness. Additionally, just as if you used digitalWrite() with HIGH, the maximum position will correspond to the maximum brightness—LED at full intensity.

Here is the code to do that.

Code explained step by step

Let’s break down the code line by line.

Setup

Create some defines (you could also create some const int variables) for the pins we will use in the program, which is a best practice.

one for the potentiometer and one for the LED pin. This way, we can change the circuit at any time by just changing those two lines in the code.

In the void setup(), we need to initialize the mode for the pins we want to use:

Since we control the LED, we use pinMode() with OUTPUT.
Since we read from the potentiometer, its mode is INPUT. However, since analog pins are already in INPUT mode, there is no need to use pinMode() for them!
Now that everything is arrangement, we can begin to control the brilliance of the Drove with what we read from the potentiometer.

Read potentiometer value

The first thing we do when we enter the void loop() function is to read the potentiometer value.

We use the analogRead() function, which takes one parameter, to accomplish this: the pin number from which to read. The value is stored in an integer variable. The range for this value will be 0 to 1023. To put it another way, this is a ten-bit number: 2^10 = 1024.

The voltage that we observed on the analog pin is reflected in this number. This number is higher the higher the voltage. 5V yields 1023, while 0V yields 0.

Compute LED brightness (option 1)

The analogWrite() function on the LED pin will be used later on to control the brightness of the LED. A byte value, also known as a number in the range 0 to 255, is what this analogWrite() function takes as its input.

Therefore, we must ensure that the value is within the appropriate range before using this function. And because I really value simplicity, you can see that 1024 is roughly equal to 255 times 4 (not exactly the same number, but close enough for what we need to do). Therefore, we can simply divide by 4 if we want to assign a value between 0 and 255.

Compute LED brightness with map (option 2)

You could also use the Arduino map() function, which can place a number in a different range, to accomplish the same thing.

We are stating the following with this line and the five arguments in the function: The potentiometerValue should be taken anywhere between 0 and 255.

When you need to perform operations similar to this one, map() is a great option because it can be used with any integer range.

Apply brightness setting to LED

As a result, we now know how bright the LED should be. The value of this brightness variable is an integer from 0 to 255. Precisely what we want for the analogWrite() capability.

analogWrite() accepts two arguments: the pin that will receive the voltage, followed by a byte number between 0 and 255.

Note: This will only function with PWM-capable pins. Therefore, if the LED is only fully powered off and on when this program is run, the digital pin you selected is probably not compatible with PWM.

What will take place then? A voltage will be generated from the range 0-255. 0V for 0 and 255, respectively, with proportional values in between.

The void loop() is called again after this instruction, so the new potentiometer value is applied directly to the LED brightness. Since the update frequency is extremely high here, you will immediately notice a change in the LED brightness when you turn the potentiometer knob.

Conclusion – Control LED brightness with potentiometer

You have learned how to use an Arduino to control the brightness of an LED using a potentiometer in this tutorial.

In conclusion, the following is a summary of the code’s requirements:

Set up the pins.
With analogRead(), get the value of the potentiometer.
Convert this worth from the reach 0-1023 to the reach 0-255.
With analogWrite(), apply this value to the LED.
For more information, check out these tutorials:

A potentiometer can be used to control multiple LEDs.
With Arduino, control the LED, push button, and potentiometer.

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Whatsapp

Leave a comment

Over 1.2 Million+ learners impacted worldwide

Learners from 170+ countries have grown in their career through our programs

Explore Programs  

Global Presence

Get in touch to learn more about how you can make the best of your talent

Spend less time worrying about job availability, and more time growing your knowledge. Join DIYguru Program today.

If you’re a current student, please get in touch through the DIYguru dashboard to ask about more details of this Program.

Please note, eligibility for this course is reserved to students who have done related projects and have relevant profiles matching with the pre-requisite of this course.

The DIYguru team hold the right to cancel your admisssion into the program without any explanation via email if found unsuitable and unfit.

Our 7-day money-back guarantee starts from the moment of signup and runs through the free week. Cancellations between days 7 and 30 will get a prorated refund.

Fees for the program is charged only when the admission is approved.

Flag
PARTNERSHIP

Colleges and Institutions

We're growing rapidly across the country, don’t miss out.

Partner with Us

Fog
UPSKILL

Corporates and Industries

Have workforce requirement or employee upskilling!

Get in Touch

Folder-solid
OPPORTUNITY

Career@DIYguru

Work with a team that’s transforming future mobility.

DIY with Us

Book Your Free Session Now

Avail free Guidebook and Expert Mentorship in EV domain curated by Industry experts.

Register to continue..