/ / Variable Resistance
A Voltage Divider
-
There is a source of voltage
-
There is a source of variable resistance
- There is another source of variable resistance inside the chip

The variable resistor and the fixed resistor divide the voltage into two parts. The variable resistor
feeds a varying voltage to the microcontroller pin. The fixed resistor provides a path to ground.
The voltage where the resistors meet (the pin) will vary with the ratio of the resistors's values.
When the resistors are equal, the voltage on the pin will be half the source voltage.
The circuit above works for any variable resistor.
It's a voltage divider.
In the image above, there are two voltage dividers,
one on analog-in 0 and one on analog-in 1.
The fixed resistor in each circuit should have the same order
of magnitude as the variable resistor's range.
For example, if you're using a flex sensor with a range of 50 - 100 kilohms,
you might use a 47Kohm or a 100Kohm fixed resistor.
If you're using a force sensing resistor that goes from inifinity ohms to 10 ohms,
but most of its range is between 10Kohms and 10 ohms, you might use a 10Kohm fixed resistor.
A
potentiometer is a simple device that provides a variable resistance,
which you can read into the Arduino board.
When the pot is up all the way, the chip gets all 5 volts.
When the pot is down all the way, the chip gets 0 volts.
Along with the digital pins the Arduino has, it also has 6 analog input pins. These inputs take a voltage between 0 and 5 volts and convert it to a number between 0 and 1024.
To use a potentiometer, connect three wires to the Arduino board.
- The first (one of the outer pins of the potentiometer) goes to ground.
-
The middle pin goes to an analog input pin.
-
The third (the other outer pin of the potentiometer) goes to 5 volts .
If you get weird readings use a pull-down resistor.
The pull-down resistor avoids a short circuit when the variable resistance is set to zero.
By turning the shaft of the potentiometer, you change the amount of resistence on either side of the wiper which is connected to the center pin of the potentiometer. This changes the relative
closeness of that pin to 5 volts and ground, giving you a different analog input. When the shaft is turned all the way in one direction, there are 0 volts going to the pin, which is read as 0. When the shaft is turned all the way in the other direction, there are 5 volts going to the pin and the chip reads 1024. In between,
analogRead() returns a number between 0 and 1024 that is proportional to the amount of voltage being applied to the pin.
Exercise I
Turn on and off a LED. The amount of time the LED will be on and off
must depend on the value obtained by
analogRead().
- Connect one of the outer pins of the potentiometer to ground.
-
Connect the middle pin of the potentiometer to an analog input pin.
-
Connect the third pin of the potentiometer to 5 volts.
- Connect an LED/resistor to a digital pin
- Complete the code and upload the program to your microcontroller
/* Analog Read to Digital Write with LED*/
// select the analog input pin for the potentiometer
int pot = __;
// select the output pin for the LED
int led = __;
// variable to store the analog value coming from the sensor
int val = __;
void setup() {
//make the led pin an output
_____________________
}
void loop() {
// read the value from the sensor
val = analogRead(___);
// turn the ledPin on
digitalWrite(____, ____);
// stop the program for some time
delay(val);
// turn the ledPin off
________________
// stop the program for some time
___________________
}
Exercise 3
Microcontrollers cannot produce varrying voltages. They produce a high (5V) and a low (0V).
But you can "fake" analog output by using pulsewidth modulation. Just like animation creates the illusion of movement, pulsewidth modulation, manipulates the
intervals bewtween on (the pulsewidth) and off in such a way that it fools the eye and appears to be between on and off.
Where can I find me some PWM?
- Dimmers
- Speed control
- Noise making
The easiest way to see PWM in action is to attach an LED to one of the PWM pins. You may be able to see the flashing rather than a dimming effect. After you have wired and programmed, add a
low-pass filter circuit.
Using a Potentiometer to dim a LED
Create a circuit and program it so that you can turn on two LEDs, one based
digitally (on or off) and one based on the analog values of the potentiometer.
To turn on the analog LED on and off use this syntax:
analogWrite(ledPin, analogValue/4);
Put the fading led on pin 9. .
- Connect an LED/resistor to a digital pin 9. This is the PWM pin
- Connect another LED/resistor to another digital pin
- Create two variables. One to keep track of the old value of the pot and one to keep track of
the new (analogValue).
-
In the loop(), set the new value to the current value of the pot.
- Use a while loop to execute when
the new value does not equal (!=) the old value.
- If the condition of the loop is met:
- Print the
value of pot to the Serial Monitor
// Read analog input, divide by 4 to make the range 0-255:
Serial.println(analogValue / 4, DEC);
- Call analogWrite() pass it the pin and a value from the pot that is between 0 and 255
- Set old value to new value
- Turn the other LED on and off using digitalWrite()
Low Pass Filter Circuit
These circuits use a combination of a capacitor and a resistor to average out the pulses so the time when the LED
is off between pulses is not so pronounced. The capacitor stores up a charge during a pulse and releases in between pulses.
The circuit doesn't smooth all the pulses. It allows pulses below a certain threshold to pass
through without smoothing them, and smoothes out those above the threshold frequency into an even pseudo-analog voltage.
The range of frequencies filtered out is determined by the ratio of the resistor's value to that of
the capacitor.
Exercise 2
Sometimes you will want to switch an output when a value exceeds a certain threshold. To do this with a potentiometer change the loop code to:
int threshold=512;
//test if your analog value is greater than your threshold
_________________________
//if the condition is met then turn on the led
______________________
//otherwise turn it off
_______________________