Digital Outputs

Controlling a digital output with CircuitPython is easy to do with a few lines of code. In fact you can connect to a board’s REPL and directly turn digital outputs on and off with a few simple commands.

An easy way to demonstrate digital outputs is with a simple single-color LED. You can connect a LED to a digital output of your board and turn it on or off by setting the output to a high or low voltage level. Remember digital outputs are only on or off and never in-between so you can’t control the brightness of the LED! To wire a LED to your board you’ll need these components:

  • A single color LED. You want a simple single color LED and not a fancier multi-color LED or NeoPixel. Look for a LED that has two legs, a short one and long one. Check out the Adafruit LED guide for more details on LEDs.
  • A resistor in the range of 300-1,000 ohms. You must use a resistor when wiring up a LED to your board or else you might damage the digital output on the board. The resistor limits the amount of current to the LED and prevents damage to the board or LED. The exact value of the resistor isn’t super important for this demonstration–pick any resistor in the 300-1,000 ohm range.
  • A breadboard and wires to connect the components and board together.

Connect the components to your board as follows:

 

  • The short leg (cathode) of the LED connects to one end of the resistor.
  • The other end of the resistor connects to the ground or GND pin of the board.
  • The long leg (anode) of the LED connects to a digital output on your board.

Now connect to your board’s REPL and you can use the digitalio module to control the digital output connected to the LED. Run the following code to first import the necessary modules and create a digitalio.DigitalInOut object for the pin (pin A1 in this example but you can use any digital output from your board):

Download: file
>>> import board
>>> import digitalio
>>> led = digitalio.DigitalInOut(board.A1)

The digitalio.DigitalInOut class is your gateway for controlling both digital inputs and outputs. By default when you create an instance of one it starts as a digital input, however you can set the digitalio.DigitalInOut.direction property to make it an output:

Download: file
>>> led.direction = digitalio.Direction.OUTPUT

Once a digital output is created and initialized you simply change the value of its digitalio.DigitalInOut.value property to turn the output on or off. For example to turn the LED on set value to true:

Download: file
>>> led.value = True

And to turn the LED off set value to false:

Download: file
>>> led.value = False

Remember with digital signals you can only set them to on or off states, i.e. true or false values. There is no in-between or half on and half off!

Finally you can blink the LED by simply changing the value in a loop with a small delay:

Download: file
>>> import time
>>> while True:
...    led.value = True
...    time.sleep(0.5)
...    led.value = False
...    time.sleep(0.5)
>>>

Remember in the REPL you need to press delete to de-indent the while loop and then press enter for it to see you’re done typing code in the loop! Alternatively press enter three times an CircuitPython will automatically close the loop and run it. You can press Ctrl-C to stop the loop from running with a keyboard interrupt exception.

This guide was first published on Aug 28, 2017. It was last updated on Aug 28, 2017. This page (Digital Outputs) was last updated on Aug 21, 2019.