You can create a program that uses anolog output:
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.


potentiometer.jpg

Voltage Divider


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.
analogin-pot.jpg
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.

  1. The first (one of the outer pins of the potentiometer) goes to ground.
  2. The middle pin goes to an analog input pin.
  3. 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 1023. In between, analogRead() returns a number between 0 and 1023 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().
/* 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 II

Modify the code above so that you see values in the Serial Monitor.

  1. Create two variables. One to keep track of the old value of the pot and one to keep track of the new (analogValue).

  2. In the loop(), set the new value to the current value of the pot.

  3. Use a while loop to execute when the new value does not equal (!=) the old value.
    while(current!=old){
    
    }


  4. 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);
      
    • Set old value to new value
    • Turn the pin on and off.