Digital Outputs

A digital output is a simple high (on) or low (off) signal from a board's pin that you can control with MicroPython.  Using a digital output you can turn something on or off, like a LED, a relay, a transistor, or more.

Let's walk through how to turn a LED on and off with a digital output from a MicroPython board.  There's an entire guide just on blinking a LED with MicroPython that you'll want to read and follow first.  In that guide you'll see how to wire a LED to a MicroPython board, for example like with the Feather Huzzah ESP8266, a red LED, and a 560 ohm resistor as below:

  • Digital GPIO 15 is connected to the anode, or longer leg, of the LED.  It's very important to use the correct leg of the LED otherwise it won't light up as expected!
  • The cathode, or shorter leg, of the LED is connected to one side of the resistor (unlike the LED it doesn't matter which way you orient the resistor).
  • The other side of the resistor is connected to the board's ground or GND pin.

Once the LED is wired to the board connect to the serial or other MicroPython REPL and create a digital output pin by running the following code:

Download: file
import machine
pin = machine.Pin(15, machine.Pin.OUT)

The import machine line will import the machine module which provides much of the hardware access API for MicroPython.  In particular the machine module has a Pin class that allows you to create pin objects for all of the digital I/O pins on a board.

In this case the second line will create an object called pin and set it as an instance of the machine module Pin class.  The initializer for the Pin class takes two important parameters:

  • The number or name of the board pin. Check your board's documentation for details on the pin values--some boards like the pyboard use a string identifier (like 'X1') while other boards like the ESP8266 use simple numbers (like 15, 14, etc.).
  • The type of digital I/O pin, in this case a digital output.  The machine.Pin.OUT value is a special constant value that tells MicroPython we intend to use this pin as an output we can control.

Now control the output level of the pin by calling the value function on the pin object in different ways:

Download: file
pin.value(0)
pin.value(1)
pin.value(True)
pin.value(False)

The value function takes a single parameter which indicates if the output should be high or low (on or off).  Notice when the pin value is set to 0 or False the LED turns off, and when set to 1 or True the LED turns on!  The LED turns on and off because the digital output connected to it changes from high to low voltage levels.

There's also a handy shortcut for setting a pin to a high or low level directly with the high and low functions:

Download: file
pin.high()
pin.low()

The high function will set the pin to a high level, and the low function will set the pin to a low level.  Notice again the LED turns on and off as the level from the pin changes.

That's all there is to using digital outputs with MicroPython!  Although they're simple digital outputs are very handy for talking to devices that expect an on or off signal like a LED, a relay or power tail, or a transistor controlling high power devices like a solenoid, laser, super bright LED and more.

This guide was first published on Sep 02, 2016. It was last updated on Sep 02, 2016. This page (Digital Outputs) was last updated on Sep 18, 2019.