Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Arduino 4 – Analog

Output from a Digital


Device
Mr. McBrien
TEJ2O
Quote for the Day
Yesterday

 Loops and Variables


Today

 Pulse Width Modulation


 More Use of the Keyboard
Analog vs. Digital
 Digital :
 1’s and 0’s
 On/off

 Yes/no

 Analog:
 “any value”
 “onish/offish”

 “maybe”
Reminder - Power
 What’s the definition of power?

 Energy per unit time – J/s = “Watts”

 P = VI

The output from an Arduino pin is 5V.


What does that tell us about the
power?
Modifying Power Output

 There are lots of applications for the ability to apply a


certain amount of power…
 Dimmer switches
 Motors
 Temperature controls
 More
 Many devices don’t support a linear response to voltage
 The Arduino doesn’t produce a range of voltages
How do we run a Motor at 1/3 Power?
How do we light an LED at 1/3
brightness?
P = IV

 If we can drop the current, we can lower power.

How do we lower the current???


Add a Resistor?
 Ohm’s Law: I = V/R
 This would waste energy!
 If devices behaved as a perfect resistor, we could change the
voltage to modify the power
 Many don’t:
 Motors
 LEDs
 Etc.

Instead of changing the voltage we apply, we take advantage


of a trick…
Analog Output from an Arduino

 Can a digital device produce analog output?

Image from Theory and Practice of Tangible User Interfaces at UC Berkley

Rather than changing the voltage, we exploit the time


aspect of power output…
Pulse Width Modulation

 Instead of changing the potential from 100% to some other


value, we briefly drop the voltage to zero
 We wait a period of time and then bring it back up to the
“on” state.
 The overall power output is the average over a larger time
period…

In PWM the switching is so fast, it’s undetectable in the


real world…
Pulse Width Modulation

• Can’t use digital pins to


directly supply say 2.5V, but
can pulse the output on and
off really fast to produce the
same effect

• The on-off pulsing happens so


quickly, the connected
output device “sees” the
result as a reduction in the
voltage

Image from Theory and Practice of Tangible User Interfaces at UC Berkley


PWM Duty Cycle
output voltage = (on_time / cycle_time) * 5V

Image credit: Tod Kurt

Fixed cycle length; constant number of


cycles/sec
PMW Pins
Core Skill!
• Command:
analogWrite(pin,value)

• value is duty cycle: between 0


and 255

• Examples:
analogWrite(9, 128)
for a 50% duty cycle

analogWrite(11, 64)
for a 25% duty cycle
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Activity 1: PWM

 Write a sketch to use PWM to change the brightness


of an LED at pin 9 (don’t forget the resistor)
 Experiment with different brightness settings
 Extension: Set up a loop to move the LED from
bright to dim repeatedly.
Activity 2: Serial and PWM

Modify your program from part 1 to control the


intensity of the LED attached to pin 9 based on
keyboard input.

Use the Serial.parseInt() method to read numeric


keyboard input as an integer.

An input of 9 should produce full intensity and an


input of 0 should turn the LED off.
…more about motors in a later
discussion…
Technically Speaking…
Geek Slide!
 …the analog output from an Arduino is not analog.
 Because there are a limited number of possibilities, it’s a
digital simulation of analog output…
Key Points
 Arduinos are not designed to allow for analog output
 Many devices are also not ideal for use with analog voltages
 We can still control the power sent to these devices using
fast switching between on and off – PWM
 This is done using the analogWrite(pin,value) command.
Homework

 Complete your PWM sketches.

You might also like