Technology

Off-The-Shelf Hacker: Let’s Talk Digital Outputs

23 Jul 2016 6:20am, by

Physical computing reads inputs, does some calculations, then sets outputs. While sensing and calculating are fun n’ all, for me, the real action happens at the outputs.

Outputs cause changes in the physical world. I thrive on tangible, real-world results. Output devices might include electric motors, relays, lights, and so on. They get their orders from the microcontroller, taking data in from one or more inputs. Outputs are the business end of physical computing.

Outputs are controlled by a couple of different methods, digital and pulse width modulation (PWM). This week, we’ll examine digital outputs and see how they work with an average microcontroller. Over the next couple of columns of the Off-The-Shelf Hacker, we’ll expand the discussion into PWM outputs, followed by a run-down on actual output devices.

Hey…What about Analog Output?

You may have noticed there aren’t any analog outputs on microcontrollers. You know, where you increase or decrease voltage to a motor, to speed it up or slow it down. For one thing, microcontrollers struggle to provide enough current directly to drive even an itty-bitty electric motor. A microcontroller deals in micro or milliamps, whereas motors, relays, electric lamps and such work with amps. Many times those devices require current in the one to multi-amp range. The other thing is that analog outputs just aren’t really even needed with microcontrollers.

Digital and pulse width modulation output methods generate all the control behaviors needed for projects when coupled with appropriate output circuits, which coincidentally usually take care of the higher current handling issues, mentioned earlier.

It’s either On or Off

Digital outputs are easy. They are either on or off. Zero or one. Yes or no. +3.3 volts or zero volts. A great example is lighting up a LED. Turn the output pin on and the LED lights. Turn the pin off and the LED goes out.

The pin can also turn on or off as quickly or as slowly as you’d like. You could write code that simply turns the LED on then off. The following code is derived from the BLINK example bundled with the Arduino IDE.

void setup() {
   pinMode(13, OUTPUT);
  digitalWrite(13, HIGH);   // turn LED on - voltage high
  digitalWrite(13, LOW);    // turn LED off - voltage low
}

Every time you apply power to the microcontroller, it will flash the LED, attached to digital pin 13 on an Arduino, once. Cycle the power and the LED will again flash one time. That’s digital, on and off.

Maybe you want the LED to blink once every half second ever-so-briefly. That’s digital too. Just turn the LED on then off, inside a loop. Power up the microcontroller and the code will start in the loop. Each time the program cycles through the loop, the LED will turn on then off. Add a half second delay at the end of the loop and the LED will blink every half second, as long as there’s power going to the microcontroller.

void setup() {
// initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn LED on - voltage high
  digitalWrite(13, LOW);    // turn LED off - voltage low
  delay(500);               // wait for 500 milliseconds
}

Suppose you also want to keep the LED on for a half second?

Put a half second delay after turning on the pin and the LED will stay on for that amount of time before it hits the off statement in your code that turns the LED off. Combine that with the half second delay at the end of the loop and the program will turn the LED on for half second, then off for half second. This behavior will execute over and over as long as there’s power going to the microcontroller.

If you think of it graphically, you’re looking at a standard square-wave pattern. The pin turns on, has +3.3 volts for a half second, then turns off and has zero volts for a half second. Here’s the code:

void setup() {
// initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn LED on - voltage high
  delay(500);               // wait for 500 milliseconds
  digitalWrite(13, LOW);    // turn LED off - voltage low
  delay(500);               // wait for 500 milliseconds
}

You could also have one second on followed by half second off, followed by two seconds on with a quarter second off, and so on. Just write the appropriate on/off code into the loop. The only limit is your imagination and amount of microcontroller program space for the code.

What’s Next?

Next week we’ll expand the discussion of digital outputs to cover the pulse width modulation technique. PWM is a natural progression of the digital model and lets us do lots of interesting things with output devices. We’ll also look at some electrical/electronic concepts you’ll need to know to keep your microcontroller happy and your output devices working consistently.

Feature image via Pixabay.


A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.