analogWrite(9, 128); delay(2); analogWrite(9,0) delay(8); analogWrite(9,255) delay(2);
Flipping fast between a discrete on and off will give the illusion of smooth analog output. This method is called Pulse Width Modulation.
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) wll vary with the ratio of the resistors' values. When the resistors are equal, the voltage on the pin will be half the source voltage.
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.

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 .
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 1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to the amount of voltage being applied to the pin.