Analog Input and Output

The computer prefers things as yes or no but we inhabit a world with more nuance.  We need to make sure we don’t start conforming to the binary simplification of the world!  Analog in and analog out are going to make the variation be a little more variable.  Instead of just knowing “whether or not” something is happening in the world we will know “how much” it is happening.  Instead of just turning on and off an LED we can make it dimmer or brighter.  On a technical level analog reusese most of what you learned from digital input but gives you a bigger range of numbers than zero or one.

Analog Sensors

Variable resistors are the easiest type of analog sensors.  They vary in resistance with some other type of energy like heat, light, pressure etc.  Other types of analog sensors like accelerometers or microphones produce electricity instead of resisting it.  They are harder to work with so we will start with variable resistors .

FSR Force Sensitive Resistor
Flex Sensor

Stretch Sensor


Analog Input Circuit

For this circuit you have to use one of the pins on the Arduino labeled analog.  This circuit is just like the digital input circuit but your replace the switch with a variable resistor.  The resistance of the other resistor connected to ground depends on the variable resistor you use.  You want one that is pretty close to the maximum resistance of the variable resistor.  What you are making is called a “voltage divider circuit” where you want to give the electrons a difficult decision to make about which way to go.  Try swapping in different resistors for the non variable resistor until you get a good enough range of numbers.



Analog Read Functions

Now instead of digitalRead, you say analogRead.  Instead of it giving you a zero or one back it will give you a bigger range of numbers between 0 and 1024.  The particular range it sends you back will be idiosyncratic to each sensor.

void setup(){

void loop(){
  int input = analogRead(1);
  if(input < 800){ //this number depends on your sensors (see about println)
  } else{



This command sends information back down the USB cable from the Arduino back to your laptop.  Ultimately you will use this to talk to your screenbased software in Processing.  For now it is very useful for debugging by checking what is inside variables. This is particularly useful for seeing the range of values that your analog sensors give back to you.

  1. You have to first set up the Serial connection in setup.
  2. Then you can send back variables to look at by saying Serial.prinln(varName) in loop.
  3. Finally you have to remember to  click open the serial monitor using the magnifying glass icon on the top right in the Arduino software on your laptop.
void setup(){

void loop(){
  int input = analogRead(1);
  //don't forget to click open serial monitor



The Special Case of a Pot

A potentiometer is behind most “knobs” and is the archetypal analogy sensor.  In this class we are trying to extend interface to accommodate expression beyond the turning of knobs so in a way this is the least interesting of the usual suspects listed above.  What  potentiometers have going in their favor is that they give you back a perfect and full range of numbers from 0 to 1024.  Remember in the analog input circuit was a “voltage dividing” with two resistors?  Well the voltage dividing circuit is built into the mechanism of the potentiometer so that the center “wiper” divides the resistive material into two resistors which are in a perfectly  complementary balance to each other.

potentiometer1pot in


Analog Ouput

I am sure you already found analog input to more fun than just a swtich.  You can dim the usual LED instead of just turning it on and off.  You can hit different pitches on the buzzer.  Probably best of all you can send you servo motor to variable positions.  But you are limited to some degree by the fact that the 5V and 20milliamps coming out of the microcontroller pin is too little to be very bright, make much noise or move something big.  For that you would have add an intermediary  like a transistor to boost the power.  In the meantime there is plenty of fun you can have with these devices.


Your micronconroller is really only capable of on or off, so it fakes an analog circuit with with a technique called PWM or Pulse Width Modulation.  Basically it turns the pin on and off really fast.  When it leaves the pin on for longer than off, it has the feeling of being brighter.  Look at the two graphs below.  As you can imagine an LED attached to a pin with wider pulses, that is the pulses left on for longer will look brighter than the LED with the narrower pulses.  In truth the LED is really only on or off but your persistence of vision makes it look dimmer or brighter. Even the longer pulses are on the order of microseconds so we don’t see the flicker.

Analog Out LED Circuit

 or digout

You could also just use the old digital output circuit from last week and it would or okay but the one with capacitor is better (see below).  Make sure you connect to one of the pins marked for  PWM pins on your micro controller (look for a “~” next to the pins on a uno).  The advantage of these pins is the microcontroller will keep sending out that pattern of pulses in the background even when it moved on to another line of code that does something else.

Capacitors (optional read)

capsymcapelectrolytic capacitor detail

To help hide the flicker further this you will see a capacitor in the circuit.  This is the last of the major electronic components, resistors, diodes and capacitors.  A capacitor acts as a savings bank storing up energy when time are good and current is flowing.  When electricity stops flowing it discharges the stored energy filling in the blanks.  In this way it acts as a smoothing filter.     This If you’re dimming an LED, start with a 10µf capacitor and a 220-ohm resistor, and experiment with different values from there to see what works best.  Some capacitors are polarized (it matter which leg is which).  You should see a minus sign on the casing of capacitor to indicate the polarity.  y.

Analog Output of Tones

If you connect a piezo buzzer to your circuit you can make some noise (you might want to do make sure you are alone because it is not av very pleasing sound).  The circuit is very easy, just one pin to ground and the other connected to an Arduino pin.  You will hear a click on and off if you try digitalWrite.  AnalogWrite will create a tone because the quick pulses will start some vibration but varying it will not be very satisfying.  If  you look at those graphs above they might look roughly like sound waves but if you look carefully even though they have different widths, they all start at the same intervals.  The interval between the waves determines how frequently they come along thus the frequency or pitch of the sound.  If you want to change the pitch of the sound you have take either take finer control by turning  on and off the wave for precise periods of microseconds or better yet use the tone command.

void setup() {


void loop() {
  // turn off tone function for pin 9:
  // play a note on pin 9 for 200 ms:
  tone(9, 440, 200);

  // turn off tone function for pin 9:
  // play a note on pin 9 for 500 ms:
  tone(9, 494, 500);

  // turn off tone function for pin 9:
  // play a note on pin 9 for 500 ms:
  tone(9, 523, 300);


Analog Output with Servo Motors

For really big fun you should play with servo motors.  With most motors you give them power and they spin.  Servo motors are different.  You give the a pulse of a particular duration or “width” and they turn to a corresponding angle.  For instance you give them a short pulse and they turn to zero degrees and you give them the longest pulse and they turn to 180 degrees (they usually can’t go all the way around).

Visit Liz Arum’s Page on This

Servo Connection

Servos usually come with three wires, black for ground, red for 5 V, and then another color the “signal” wire that you send a pulse to.  The end of the wire of a servo is typically a header with three holes which is too much like the holes in your breadboard to fit together.  If you can get your hands on some header posts it works nicely, otherwise stick some jumper wires in there.

servocon2 servocon1

Servo Software

You can create the pulse manually by turning on and off a pin using digitalWrite with microsecond precision.  Otherwise you can use the the servo library to make it a little easier.  You will notice in this code that there is an include statement at the top the refers to a library.  A library is an add on piece of code that is not part of the core processing code.  Luckily for you this library comes with the standard download of the Arduino environment so you don’t have to do anything except mention at the top that you intend to use it by saying #include <Servo.h>  .

Hard Way

Easier Way Using Servo library

#include <Servo.h>      // include the servo library

Servo servoMotor;       // creates an instance of the servo object to control a servo

void setup() {
  Serial.begin(9600);       // initialize serial communications
  servoMotor.attach(2);  // attaches the servo on pin 2 to the servo object

void loop()
  int analogValue = analogRead(A0); // read the analog input
  //Serial.println(analogValue);      // print it

  // if your sensor's range is less than 0 to 1023, you'll need to
  // modify the map() function to use the values you discovered:
  int servoAngle = map(analogValue, 730, 980, 0, 179);

  // move the servo using the angle from the sensor:


You will also see a new function called map in this code.  Because the numbers we are getting in from some random analog sensor are unlikely to the exact numbers that some random servo want, you will have to do a like math to massage them.  Map does this for you.  You give it the number you want to convert in a variable, the range from the input, and the range for the output and it will return a number in the output range that is proportionate to the input number in the input range.

 You Don’t Really Need to Read Further

Analog Ouput Amplified with a Transitor

To power bigger motors or speakers or lights you will need and intermediary like a transistor.

Leave a Reply