Check the 34 other Arduino tutorials

#4 · Control blinking speed with potentiometer


Introduction

In this lesson we are going to work with a new component: the potentiometer. We will use this to control the blinking speed of an LED. Curious about what a potentiometer can do? You will know that after following this Arduino tutorial! 🤓

Course material

At the bottom of this page you'll find the course material button. This button allows you to download the code, circuit diagram and other files relevant to this Arduino tutorial.

Potentiometer

In the previous tutorials we used a resistor with a fixed value. With the potentiometer this is different. You can actually see a potentiometer as a variable resistor. The resistance changes by turning, just like the volume knob on an amplifier. In this lesson we will use a 10K Ohm potentiometer. This means that the resistance value ranges from 0 to 10K Ohms.

Potentiometer / potmeter
Potentiometer

A normal resistor has 2 ends, the potentiometer has 3. Left and right are the + and the -. Which one you use for what doesn't matter since a resistor works both ways.

The middle pin returns the current with the resistance of the potentiometer applied. Suppose you connect the + to 5 Volt, and connect the - to the GND. When we turn the potentiometer for 50% open the middle pin will be 2.5 Volts.

You can use this to make a light shine brighter or softer, or to turn the volume of an amplifier up or down. We are going to use it to control the blinking speed of an LED.

🎓 If I set the potentiometer to 20%, how many volts is there on the middle pin?

The circuit

We use the breadboard to connect the potentiometer to the Arduino. Insert the potentiometer as shown in the image below.

Arduino with potentiometer
The circuit with the potentiometer plugged into the breadboard

Then connect the following jumper wires:

  • The 5V on the Arduino -> left pin of the potentiometer
  • The analog pin A2 on the Arduino -> middle pin of the potentiometer
  • The GND on the Arduino -> right pin of the potentiometer

Arduino Code

We start by defining 3 variables:

1 int potPin = A2;              // Potmeter pin
2 int ledPin = LED_BUILTIN;     // Builtin LED pin
3 int potVal = 0;               // Potmeter's value (0 by default)

By using LED_BUILTIN the Arduino IDE can determine itself to which pin the built-in LED is connected. When reading out, we have to store the value of the potentiometer somewhere. To do this, we define the potVal variable.

loop()

1 // The loop() function runs infinitely
2 void loop() {
3   
4   potVal = analogRead(potPin);       // Read the analog value of the potmeter (0-1023)
5   Serial.println(potVal);            // Write the value to the serial monitor
6   
7   digitalWrite(LED_BUILTIN, HIGH);   // Turn the built-in LED on
8   delay(potVal);                     // Pause for the length of the potval value (0-1023) milliseconds
9   
10   digitalWrite(LED_BUILTIN, LOW);    // Turn the built-in LED on
11   delay(potVal);                     // Pause for the length of the potval value (0-1023) milliseconds
12   
13 }

The loop() function consists of 3 parts:

  • Reading the potentiometer
  • Turn on built-in LED
  • Turn off built-in LED

analogRead()

1 potVal = analogRead(potPin);

In the previous tutorial we used analogWrite where we wrote an analog value to the Arduino. The potentiometer returns an analog value, for this we use analogRead. analogRead has only one parameter, namely the pin to be read. Here we read the value of the potPin, which is a value between 0 and 1023.

Turn LED on/off

1   digitalWrite(LED_BUILTIN, HIGH);   // Turn the built-in LED on
2   delay(potVal);                     // Pause for the length of the potval value (0-1023) milliseconds
3   
4   digitalWrite(LED_BUILTIN, LOW);    // Turn the built-in LED on
5   delay(potVal);                     // Pause for the length of the potval value (0-1023) milliseconds

Now all we need to do is turn the LED on and off. For this we use the digitalWrite() from the previous lessons. Earlier we used a fixed value for delay(). Now we use the variable potVal. This contains the read value of the potentiometer.

🎓 Can you think of the longest possible pause that we can set with the potentiometer?

Arduino IDE serial monitor

So far we have put the code on the Arduino and show via an LED that the code works. The Arduino can also talk back to the computer via the serial monitor.

Earlier in the code we saw the line:

1 Serial.begin(9600)

This indicates that the Arduino is allowed to send back symbols at a rate of 9600 symbols per second. This number is called the Baud rate.

Later in the code, we're going to actually write data to the serial monitor. We do this through:

1  Serial.println(potVal)

Here we write the value of the variable potVal to the serial monitor. This is the readout value of the potentiometer. println() stands for print line.

The serial monitor can be found in the menu of the Arduino IDE

Tools ▸ Serial monitor

Arduino IDE serial monitor
Arduino IDE serial monitor

Uploading code to the Arduino

You can now upload the code to the Arduino. After this you will see the read potentiometer values scroll by, they change as soon as you turn the potentiometer.

💡 At the bottom right you see the Baud rate. Note that this is the same as specified in Serial.begin(), in our case 9600.

#4 · Control blinking speed with potentiometer schakelschema

Bas van Dijk

About Bas on Tech


My name is Bas van Dijk, entrepreneur, software developer and maker. With Bas on Tech I share video tutorials with a wide variety of tech subjects i.e. Arduino and 3D printing.

Years ago, I bought my first Arduino with one goal: show text on an LCD as soon as possible. It took me many Google searches and digging through various resources, but I finally managed to make it work. I was over the moon by something as simple as an LCD with some text.

With Bas on Tech I want to share my knowledge so others can experience this happiness as well. I've chosen to make short, yet powerful YouTube videos with a the same structure and one subject per video. Each video is accompanied by the source code and a shopping list.