Analog Outputs

Analog Outputs (AO) are one of the four contexts or types of circuits that are core to tangible.

If you have not yet looked at the tangible matrix Building Block, you should check it out now. It will introduce you to all four contexts.

Context :: Analog Output

Analog refers to signals, circuits, or logical systems that are variable or graded. They can be the top or bottom of the waterfall and every shade in between.

Input and Output are defined from the perspective of our Arduino.

Electrical signals generated by the Arduino and sent out to circuits are OUTPUTS.

So, Analog Outputs are variable signals (V) that move OUT of the Arduino.

PWM — How Digital Becomes Analog

The Arduino Uno is a strictly digital device. Given this, how is it going to make an analog signal?

The Arduino approximates analog output signals using a process called pulse width modulation or PWM. PWM is achieved by turning a selected pin HIGH and LOW very, very quickly. By altering ON (HIGH, 1) time and OFF (LOW, 0) time one can alter average current delivered to a circuit. We call the ratio of ON:OFF time the duty cycle.

Duty Cycle: A deeper look at PWM

We can use a concept called duty cycle to imagine how this works. A deep look at duty cycle is beyond what we need at this level — but I will cover it in general here. Follow links below for details.

First imagine an LED blinking on a digital pin. The duration of time the pin is HIGH (1) is called ‘ON’ time. And conversely, the amount of time the pin spends ‘LOW” (0) is known as ‘OFF’ time. The blink can be asymmetrical, it can be ON for more or less time than it is ‘OFF”.

We will call one cycle the amount of time it takes to go from the start of the HIGH, through the LOW and just back to the start of the next HIGH.

The percentage of time spent in the HIGH state during one cycle is called the duty cycle.

So a 50% duty cycle would be HIGH (on) for half the time and OFF for the other half. A 75% duty cycle would be on 3/4 of the time and off for 1/4.

A 100% duty cycle is the same as setting a pin HIGH while 0% is equivalent to settinga pin LOW.

Image source

Read more about duty cycle and PWM here.

PWM: Controlling LEDs and Motors

The pulses of a PWM cycle are incredibly fast (brief) — the individual blinks are too fast to see with the naked eye. But they are very real.

When an LED is powered by a pin using PWM, reductions in duty cycle reduce LED brightness. The outcome is a an ON/OFF cycle that dims an LED. This effect is grounded in the same principle that makes video and film look like moving images — not stuttering stills.

We will also use PWM to control speed of motors and the positions of servos.

Card

Your kit contains a reference card for analog outputs.

Each card indicates the context, command, circuit and offers a code sample.

Video :: Analog Outputs

Get the Slides

Classic Example

The classic example of a analog output is a fading LED.

Circuit

The classic circuit for exploring an analog output is a simple LED and current limiting resistor. Yup — it is the same circuit we used for digital outputs.

Command :: analogWrite()

The code that creates an Analog Output is:

    analogWrite(pin, state);

where:
pin = 3,5,6, 9,10,11
state = 0 – 255

Note 1: these pins are marked with a tilde ~

The line of code above means:

Use PWM to set the duty cycle of ~pinX to state = 0 to 255. Where 0 = 0% and 255= 100% duty cycle.

How Does it Work?

As indicated above, analogWrite quickly blinks ~pinX. The variable state defines the duty cycle.

The result is a staircase like change in brightness as state increases from 0 to 255.

Important in Arduino, we Write outputs.

Check the Arduino Docs for this topic

Code Sample

int ledPIN = 6;
int state = 0;

void setup() {
  pinMode( ledPIN, OUTPUT );
}
void loop() {
  analogWrite( ledPIN, state );
  state++;
  delay(50);
}

Once you have this basic code running, try also cycling through the range of possible states with a for loop instead of the counter.

Going Further

Read more about duty cycle and PWM from sparkfun.

Lots of in depth (too much?) info on PWM at wikipedia

Some follow up Arduino references:

pinMode

int — variable declarations

++ increment

Leave a Reply

Your email address will not be published. Required fields are marked *