# Arduino Basics- Using Potentiometers - I Made It at TechShop

19,308

13

4

Posted

## Introduction: Arduino Basics- Using Potentiometers - I Made It at TechShop

Potentiometers have a lot of uses in electronics. They are essentially a variable resistor. Combine this with the fact that an Arduino is capable of reading analog voltages, this can turn a potentiometer into something like a continuous speed control or position control. This basic control is essential understand in making more complex systems. On top of that, I made this at TechShop. Maybe you can try this after taking the electronics course.

http://techshop.ws

## Step 1: Materials

Arduino Uno

Usb cable

LED

10k ohm Potentiometer

500ohm resistor

wire

## Step 2: Circuit

This is a fairly simple circuit. The Arduino can provide power for the potentiometer and a logical high from the digital pins is high enough to run a simple led.

Connect the LED to pin 3 with a current limiting resistor of 500 ohms. Connect the long end of the led to the resistor and the short side to ground. Connect the potentiometer to power and ground and the third leg to analog pin 5. This creates a variable voltage divider. Once the Arduino is connected to a computer, the board will have power.

## Step 3: Code

The following is the code for the blinking device.

-----------------------------------------------------------------

#define POT_PIN 5
#define LED_PIN 3

void setup(){

//set this to output
pinMode(LED_PIN, OUTPUT);

}

void loop(){

int val;

digitalWrite(LED_PIN, HIGH);

delay(val);

digitalWrite(LED_PIN, LOW);

delay(val);

}

-----------------------------------------------------------------

So what's going on here? First the arduino needs to set the led pin to an output. Then the program loops through and does a few things. First it sets aside a little memory for the value that will be read from the potentiometer. Then it reads the voltage value from analog pin 5. This is a value from 0 to 1023 which translates to 0-5v. Once that value is determined, it sets that value as the delay for the duration of the on and off cycles of the led.

And that's it, a potentiometer used to control a blinking light.

## Recommendations

• ### Arduino Class

68,117 Enrolled

• ### Spotless Contest

We have a be nice policy.

## Questions

another cool thing you could do is use the pot to control the brightness of the LED, replace your current loop with this:

void loop(){
int val;
analogWrite(LED_PIN,(val/4);
delay(100);
}

2 replies

sorry put an extra ) at the end of the analogWrite:

void loop(){
int val;
analogWrite(LED_PIN,(val/4));
delay(100);
}

Sure, turn that blinking light into a dimmer switch! But this is just a really really slow version of analogWrite. Turns out analogWrite doesn't change the voltage, but instead just blinks really fast. Also known as PWM'ing. Turn down the delay, the blinks start getting so fast that it just looks dim. It's a good intro to PWM though!

But if I were to make a dimmer, I would do this

void loop(){
int val;