- 1 Lab 8 - Analog Input/Output, PWM
- 1.1 Topics
- 1.2 Materials
- 1.3 Activities
- 1.3.1 Analog Input
- 1.3.2 Analog Output
- 1.3.3 Analog Input driving Analog Output
- 1.4 BONUS - CAD SOFTWARE
- 1.5 Actuators, for future reference
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:
- DC motor
- TIP 120 Transistor
- 1N4001 Diode
- red LED
- 220 Ohm resistor
- hookup wire
- 10K potentiometer
- fully assembled Freeduino
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.
The simple potentiometer voltage divider can be connected to an of the Analog In pins (0-5) on your Arduino.
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 ____.
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
sensorValwill 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
sensorValdata 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
- Does changing the code make this more sensitive? The voltage range of the input is staying the same, the number representing it does not...
Familiarize yourselves with the specs for these sensors. If your TAs have any of them in class, try them out.
spectra symbol flex sensor 
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)
Voltage returned is proportional to force applied to surface:
Application circuit ():
Good for substantial physical interaction (this project: http://roberttwomey.com/hitme/)
FlexiForce Sensors: http://www.tekscan.com/flexiforce/flexiforce.html
Sharp GP2D12 INFRA-RED RANGER
"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." 
One of many Sharp Rangers, with different effective ranges comparison guide
http://www.acroname.com/robotics/parts/R271-SRF05.html 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?
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).
Open the example Examples->Analog->Fading. The crux of this sketch is the
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.
<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).
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.
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
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.
BONUS - CAD SOFTWARE
Cadsoft EaglePCB - the program I have been using to make these schematics. http://www.cadsoft.de/download.htm 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
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... ?