Lab 4 - Potentiometer Lab Details

Objective:  To make an LED blink faster or slower when we twist the potentiometer.  Part 2 : to make an LED fade when we turn the potentiometer.


Parts List

Part Qty Notes
Potentiometer 1 10k Ohms
330 Ohm Resistor 1 orange-orange-brown
LED (any color) 1  
Wire (red) 1  
Wire (black) 1  
Wire (any color) 4 It helps if they are different colors


The potentiometer has 3 pins coming out the bottom and a dial with an arrow on it on the top.

The MIDDLE pin is connected to your Arduino and the outer pins are connected to the 5V and GND columns of your breadboard.

Remember that the potentiometer serves as a 10k resistor so it's ok to hook it straight up to 5V and GND unlike an LED that needs a resistor.

The potentiometer works even when flipped because the middle pin is the most important.



Parts Set Up

Instructional Parts Set-Up

Part Connection 1 Connection 2 Notes
Potentiometer     Connect 3 pins to B6 - B7 B8

Orientation does not mater as long as all 3 pins are in and the middle pin is on B7
Wire (any color)         E6 GND or -ve column       This wire is connected the -ve side to the potentiometer through the breadboard 
Wire (any color) E7 Arduino Analog Pin 0 This is connecting the potentiometer middle wire to Analog Pin 0 so we can monitor it
Wire (any color) E8 5V or +ve column  
LED Anode/Longer pin to H19 H20  
Wire (any color) J19 Arduino Pin 13 In Part 2 we will connect this one to Pin 9
330Ohm Resistor  J20 GND or -ve column  
 Wire (red) +ve column Arduino 5V  
 Wire (black) -ve column Arduino GND  

Visual Parts Set-Up

This circuit graphic was taken from CIRC-08 from the original ARDX Arduino Experimentation Guide which most labs on this website are based on.

In the first experiment, you will hook up the LED to pin 13.  In the second experiment you will hook it up to pin 9!

Lab 4 Code

Without Comments


With Comments

Line Code Comments
1  int sensorPin = A0;                                                                  This is assigning a value of "A0" to the integer sensorPin where A0 will mean Analog Pin 0
2 int ledPin = 13; Sets up the LED to Digital Pin 13
3 int sensorValue = 0; Setting up a variable to store the value for the sensor later
4 void setup() Declares our setup() function
5  { Bracket to show the start of the setup() function
6 pinMode (ledPin , OUTPUT); Declares the output pin for out LED.  ledPin or Digital Pin 13 is made to be an OUTPUT
7  } Close bracket to end the setup() function
8 void loop() Declares the main program to loop/repeat
9 { Open bracket or start of the loop() function
10 sensorValue = analogRead(sensorPin); This takes an analog reading of sensorPin (or A0 which is Analog Pin 0) and stores it in the variable called sensorValue
11 digitalWrite(ledPin , HIGH); This just turns on the LED by putting maximum power to ledPin (Pin 13)
12 delay(sensorValue); This makes the program wait according to the value of the twist potentiometer sensor.  
13 digitalWrite(ledPin, LOW); This turns the LED off
14 delay(sensorValue); This delays the program before it loops back to the top.  It delays it equal to the value of the potentiometer reading
15 } This close bracket ends off the loop() function

PART 2 - Fading

Type in this program or modify your current one to match it.  There are changes to ledPin and void loop(), plus the use of Pin 9 for an analogWrite command.

On the digital pin side, the following pins can also be used for analogWrite:  3, 5, 6, 9, 10, and 11.

Without Comments

With Comments

Line Code Comments
1 int sensorPin = A0;                                                                                                                                                                                                                     We want to use an analog pin to read a ranged value
2 int ledPin = 9;

We switched from digital pin 13 to pin 9 because pin 9 can be used as an AnalogWrite pin.  Pins 3, 5, 7, 9, 10, and 11 can do this.  Reference: analogWrite()

3 int senorValue = 0; Setting up a senor value to write to
4 void setup() Start of the setup() function
5 {  
6 pinMode(ledPin, OUTPUT); Setting up ledPin for our LED output.
7 }  
8 void loop() Start of the loop() function that will repeat
9 {  
10 int value = analogRead(sensorPin) / 4; The analogRead takes the value that comes from the potentiometer then divides it by 4. The max value out of the potentiometer is 1000.  1000/4 = 250 which is under the max value for analogWrite.
11 analogWrite (ledPin, value); This analogWrite to ledPin (digital pin 9 which can output an analog signal), gives a value between 0 and 255 which results in fading or anything between HIGH and LOW (completely ON and OFF).
12 }  

Lessons Learned: 



Arduino Analog and Digital Pins