In the previous labs, I used a switch (on or off) to make a digital circuit. In this lab, I am using two different kinds of variable resistors (potentiometer and force sensor resistor) to make analog inputs which reflect continuous changes in the circuit.

First, I took my microcontroller and breadboard and wired a potentiometer (pot) and a 10mm blue LED to the breadboard. The pot is a 10K variable resistor with three connections, where the center lead is wired to the microcontroller, and the other two leads are wired to power and ground. I also hooked up a fixed resistor with the LED to provide a path to ground in the circuit.

Potentiometer and LED

As I turned the pot dial back and forth, it varied the voltage being transmitted to the microcontroller. As this happened, the LED turned brighter (or dimmer) and the microcontroller’s input tracked the changes in voltage. These real-time changes were displayed for me in the Arduino debugging pane, so that I could read the changing values as I turned the pot.

Pot analog input values

The code for this example is here:

int potPin = 0; //Analog input pin "0" that the potentiometer is attached to
int potValue = 0; //value read from the pot
int led = 9; //PWM pin that the LED is attached to (note: PWM 0 is on digital pin 9)

void setup(){
  //initialize serial communications at 9600 bps:
  Serial.begin(9600);
  //declare the LED pin as an output:
  pinMode(led,OUTPUT);
}

void loop(){
  potValue = analogRead(potPin); //read the pot value
  analogWrite(led,potValue/4); //PWM the LED with the pot value
  Serial.println(potValue); //print the pot value back to the debugger pane
  delay(10); //wait 10 milliseconds before the next loop
}

Next, I removed the pot and replaced it with a force sensitive resistor (FSR) which detects changes in pressure / force applied on the surface of the sensor. I wanted to try analog input using a different kind of variable resistor.

Here is a diagram of how an FSR is built in three layers:

Diagram of FSR

Diagram of FSR

I hooked up the FSR with a 220-ohm resistor on the breadboard because, unlike the pot which balances the ratio of voltage between its two sides, the FSR’s resistance range can be anywhere between 0K, to 100Kohm (light force), to 200Kohm (max force). I also hooked up a 5mm red LED and 10kohm fixed resistor on the breadboard to provide a path to ground.

FSR and LED

When I squeezed the FSR, the LED turned brighter and the microcontroller’s input tracked the changes in voltage.

Squeezing FSR

Here are the real-time changes displayed for me in the Arduino debugging pane as I squeezed the FSR:

Changing inputs from FSR

The code for this example is here:

void setup(){
  //initialize serial communications at 9600 bps:
  Serial.begin(9600);
  //declare the LED pin as an output:
  pinMode(9,OUTPUT);
}

void loop(){
  int potValue = analogRead(0); //read the pot value
  analogWrite(9,potValue/4); //PWM the LED with the pot value (divided by 4 to fit in a byte)
  Serial.println(potValue); //print the pot value back to the debugger pane
  delay(10); //wait 10 milliseconds before the next loop
}

Note: Both of these circuits use Pulse Width Modulation, or PWM, to get analog values using digital means. PWM manipulates the intervals on and off (pulsewidth) in such a way that it fools the eye and makes it appear as though you are controlling the relative brightness of the LED.


No Responses to “Lab – Analog In with Arduino”  

  1. No Comments

Leave a Reply