From Robert-Depot
Revision as of 08:55, 2 March 2010 by Rtwomey (talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Lab 8 - Analog Input/Output, PWM


  • Analog input to Arduino.
  • Analog output from Arduino.
  • PWM and motor speed control.
  • 8 and 10 bit variables.
  • Arduino programming:
    • analogWrite()
    • analogRead()


  • DC motor
  • TIP 120 Transistor
  • 1N4001 Diode
  • red LED
  • 220 Ohm resistor
  • hookup wire
  • 10K potentiometer
  • breadboard
  • fully assembled Freeduino


Analog Input

Open the example File->Sketchbook->Analog->AnalogInput. In this example, the value of the analog input changes the timing of the light: the lower the input voltage the more quickly the light blinks, the higher the input voltage the slow the light blinks.


First we will use a potentiometer as our input.

Pot in.png Pot photo.JPG

The simple potentiometer voltage divider can be connected to an of the Analog In pins (0-5) on your Arduino.

Freeduino seedstudio labelled.png

By default, the AnalogInput program expects the potentiometer to be connected on Analog In 0, the value of sensorPin variable at the top of the program.


  • Determining the voltage output of the potentiometer circuit. What are the upper and lower voltage values as you turn the knob, and where do they occur? (HINT: this is a voltage divider circuit. There are formulas for calculating the voltage in a voltage divider)
    • Highest voltage ____, position ____.
    • Lowest voltage ____, position ____.


Photo resistor.png Photoresistor.JPG

Use the photoresistor from your 147A kit (which has a resistance between 800 Ohms - 140 KOhms depending on your lighting conditions) and build the simple photoresistor voltage divider circuit above.


  • With the same R2 resistor (10k) from above, replacing R1 with the photoresistor, what would you expect the highest and lowest voltage to be output from the simple photoresistor circuit?
    • Highest ____V, Lowest ____V.
  • The important part of this sketch is the line where the microprocessor reads the analog input sensorValue = analogRead(sensorPin). Looking at the Arduino reference for that function (analogRead()), what do you think the value sensorVal will be for those highest and lowest voltages from the photoresistor circuit? Remember, analogRead() returns a number between 0-1023.
    • sensorVal: highest ____, lowest ____.
  • Verify your answers for the questions above. You can build the circuit and measure the voltage directly, but getting the sensorVal data is a little more complex. Try using concepts from next week's class (Examples->Communication) or just the basic Serial.println() we discussed in class.
  • How could you make this more sensitive? There are least three ways to try... think in terms of the physical enclosure around the sensor, the hardware setup (a voltage divider), and the software (look at Analog->Calibration..., specifically the map() function)
    • Does changing the code make this more sensitive? The voltage range of the input is staying the same, the number representing it does not...

Other Sensors

Familiarize yourselves with the specs for these sensors. If your TAs have any of them in class, try them out.

flex sensor

spectra symbol flex sensor [1]

home made strain gauge

A poor man's flex sensor. See page 32 in Forest Mims "Electronic Sensor Circuits & Projects" to make your own.

force / pressure sensor

Force Sensing Resistor (FSR)


datasheet purchase

Voltage returned is proportional to force applied to surface:


Application circuit ([2]):


Good for substantial physical interaction (this project:

more force sensors

FlexiForce Sensors:

IR rangefinders



"This sensor takes a continuous distance reading and returns a corresponding analog voltage with a range of 10cm (4") to 80cm (30"). The sensor package includes a JST 3-pin connector, three pre-crimped wires, and a booklet with detailed information and examples." [3]


One of many Sharp Rangers, with different effective ranges comparison guide

Ultrasonic rangefinders range of four meters

Can we sample sound?

  • It is an analog signal.
  • I said in class it takes 100 uS to do an analogRead(), which gives us a maximum sampling rate of 10 kHz. This is sufficient for old fashioned phone conversation.
  • We can do definitely do threshold detection: is a sound louder than a certain point.
  • ...try it out? What do people in the Arduino forums say?

Analog Output

Continually varying signals, rather than discrete "steps". How does a digital microprocessor create an analog output? Using Pulse Width Modulation (PWM), setting the duty cycle (and effective voltage) of an on-off signal. This happens 500 times per second (500 Hz).

LED Dimming

Open the example Examples->Analog->Fading. The crux of this sketch is the analogWrite function.

The function analogWrite() only works on some digital output pins. Look up the help for the function, it will tell you which pins work: in our case digital pins 3,5,6,9,10, and 11.

Freeduino seedstudio labelled PWM.png

<youtube v="62gWVWCyw_w" loop="true"/>


It looks like a MacBook napping...

Get this working.

Drive a Lightbulb

Same as the LED, but with a higher current load: your microprocessor can not source enough current to power the bulb. (Or maybe it can, but you probably don't want to test it!) We need to use a power transistor: TIP120. This allows you to control a larger current (the bulb) with a smaller current (digital out from the chip).

Lamp fader.png BULB.JPG

Drive a Motor / Speed Control

  • Basic motor speed control (for a DC motor).
  • Same circuit as above, swapping the motor for the lamp.


Analog Input driving Analog Output

Potentiometer driving Lightbulb

Combing your AnalogInput and Fading code to drive the fading/speed-control circuit with the potentiometer.

Use analogRead() to get the value from the sensor, like you did before, then use analogWrite() to output the value to the LED. NOTE: analogRead() will return values from 0-1023, and analogWrite() writes values from 0 to 255, so you will need to divide your read value by 4 to get the output value. Or you could use map().

Potentiometer driving Motor

Swap the motor for the light bulb in the transistor circuit. Now your knob should control the speed of the motor.

Photoresistor driving Lightbulb

Drive this circuit with the input from the photoresistor, using the transistor driver and lightbulb as output. Does yours turn on when it is light, or does it turn off? Switch this to the opposite behavior by rewriting the code running on the processor.

Photoresistor driving Motor

Swap the motor for the light bulb. Now you have a light controlled motor... hmmm.


Cadsoft EaglePCB - the program I have been using to make these schematics. This software is free. You can use it to design circuits, from schematic to layout to fabrication. (FYI It is also installed on the lab computers). You could draw your schematic for the final project with this.

Actuators, for future reference





Stepper Motor

Cellphone buzzer (I have one here)


AC appliances with a relay (!!!!CAUTION CAUTION CAUTION!!! get input from me before you try anything VERY DANGEROUS!!!!) (think dancing pixar light)

Anything that moves, blinks, has activity, and runs off electricity... ?