Analog

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 .

Potentiometer
Potentiometer
fsr
FSR Force Sensitive Resistor
thermistor
Thermistor
photocell
Photocell
flex
Flex Sensor
stretch

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.

ain

IMG_20140204_092906

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.

 

Println

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 screen based 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.

 

serialMonitor

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.

PWM

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)

capsymcapcapacitor

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 (or a small 8 ohm speaker) to your circuit you can make some noise (you might want to do make sure you are alone because it is not a 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.

 

Analog Mapping

We can also try using the range of our analog input values to somehow correlate with our analog outputs.  For example we can use  our Force Sensing Resistor (FSR) to dim some LEDs so that the amount of pressure we apply to our sensor directly controls how bright or dim our led becomes.

analogMap2LED_bb analogMap2LED_schem

First declare our variables at the top of our sketch:

Then initialize our Serial communication and our outputs:

Then inside the loop read our analog input and store that value in our fsr variable. Use Serial.println and the serial monitor to see what values you get from your sensor.

Then map the range of our FSR to the LEDs:

The map function takes 5 parameters:

  1. input value – The variable that we want to be mapped.  in this case this is our analog reading
  2. input minimum – the minimum value of our input’s range
  3. input maximum – the minimum value of our input’s range
  4. mapped minimum – our desired minimum value (this correlates to the input minimum)
  5. mapped maximum – our desired maximum (this correlates to the input maximum)

Remember not all variable resistors will actually give full readings from 0-1023.  You can change the map functions input min and max, as well as the output min and max to reflect your actual readings and desired output.

Now imagine mapping our values to a tone, or motors…

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).

degrees
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

digitalWrite(motoPin,HIGH);
delayMicroseconds(1850);
digitalWrite(motoPin,LOW);
delayMicroseconds(1850);
Easier Way Using Servo library

 

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.  http://itp.nyu.edu/physcomp/Tutorials/HighCurrentLoads

Leave a Reply