In this case we connect the potentiometer central point to pin a0 because we will be using a sketch that uses pin a0 as analog input.
Connect your FTDI breakout board to the Lilypad and upload the following sketch:
int softPot = 0; // this line selects the input pin a0 for the potentiometer cursor
int ledPin = 13; // this line selects the pin 13 for the LED
int tempPot = 0; // variable to store the value coming from the sensor
void setup() {
// this line declares the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the soft potentiometer
tempPot = analogRead(softPot);
// it turns the LED on
digitalWrite(ledPin, HIGH);
// stop the program for <tempPot> milliseconds:
delay(tempPot);
// turn the LED off:
digitalWrite(ledPin, LOW);
// stop the program for for <tempPot> milliseconds:
delay(tempPot);
}
You do not need to connect an LED to pin 13 to test this sketch because pin13 has a built-in LED.
By sliding up and down the ring on the tape you should see the LED blinking at a different rate.
Theory
Let's now see more in detail how it works. A microcontroller analog input is able to detect a variable voltage. Analog means that it is not just a mere HIGH or LOW value, 0 V or 5 V, like in digital inputs, but it can be anything between 0V or 5V. As the world around us is analog and not digital having the possibility of reading continuous values it is very handy.
Sometimes, instead of a measuring sensor, we want to be the ones that want to create an analog signal, maybe to control the volume of our headphones, or to adjust the light level of our room. How do we achieve this? With a potentiometer. A potentiometer is just a long resistor with a sliding cursor able to touch the resistor in one point only. This point can be moved, giving different readouts.
A potentiometer has three terminals (see right part of the picture), one (1) must be connected to i.e. 5V, one (2) to 0V and the terminal connected to the sliding point to the input of the microcontroller (3). This is where we will measure the variable voltage. Now when we slide the cursor along the resistor very close to the terminal connected to 5V we will get voltage values close to 5V, when we will slide the cursor close to the terminal connected to 0V on the third terminal we will get voltages near 0V.
In the picture we suppose to have a resistor with a total resistance of 15Kohm. According how we place the cursor we can obtain a situation where, for example, we have one resistor of 5Kohm and one of 10kohm.
We will now consider equal to zero the current flowing into the Lilypad through its input pin, and as a result we will have that both resistors are crossed by the same amount of current. Applying Ohm's Law we calculate the current flowing I = 5V / 15k Ohm = 0.0003 Amp. The voltage drop across R1 will then be V = R1 * I = 5000 * 0.0003 = 1.66 V. That means that our input pin will be at 5V - 1.66V = 3.34 V.