Skip to main content

Section 3.2 Output pins

The pins of most microcontrollers can be configured to function as output pins, meaning that the voltage on the pin can be controlled by software. If we connect the pin to other circuitry, we can control those circuits using software.

Subsection 3.2.1 Output pins

The circuit below is a simplified model of the circuit which drives an output pin. Each output pin has a copy of this circuit which can be independently controlled.

There are two electrically-controlled switches connected to the output. One is connected to Vdd (power), and the other is connected to ground. By opening and closing the switches, it is possible to control the voltage at the pin, and therefore to drive external circuitry. Play around with the circuit for a bit until you get a feel for how it works.

Figure 3.2.1. Output pin circuit model

This circuit can be in one of four possible states:

  1. Top switch closed, bottom switch open: the top switch makes a direct connection between Vdd and the pin, so the pin voltage will be Vdd (relative to ground). The bottom switch is open, so it does not affect the output. This is called a "high" output, or a "logic 1".

  2. Top switch open, bottom switch closed: the bottom switch makes a direct connection between the pin and ground, so the pin voltage will be zero (relative to ground). This is called a "low" output, or a "logic 0".

  3. Both switches open: The pin is not connected to anything, so current will not flow in or out. This is called "floating".

  4. Both switches closed: This creates a direct connection between Vdd and ground, causing a large current to flow directly from the power supply to ground. This is not only a waste of power; it is also likely to cause the chip to melt. As a result, the microcontroller is generally designed so that this isn't possible. (If you try this in the simulator above, you'll see that it halts the simulation.)

Subsection 3.2.2 Controlling output pins with MicroPython

Now that you know what an output pin can do, let's look at how to control one with MicroPython. Here's an example to get started:

from machine import Pin
led = Pin(10, Pin.OUT)

Let's break this down one line at a time. The first line, from machine import Pin is like any other import statement we've seen so far. It simply loads a library so that we can use its functions and objects.

Next, the code calls the Pin() constructor function and saves the result in the variable led. You could call led anything you'd like. But as with any other variable, you should give it a descriptive name that makes it easy to understand what your code does. The Pin() constructor takes two parameters: the pin number on your development board, and the mode to use for the pin. Most pins can be inputs or outputs (more on that later), but in this case we configure it as an output by specifying Pin.OUT.

Finally, we can set the pin to be high or low using the on() and off() functions. In the example, led.on() sets the output pin high (scenario 1 above). Writing would set the output pin low (scenario 2).

Warning 3.2.2.

Note that "off" means "pin connected to ground", and not "pin disconnected". This is unfortunate terminology, because an external circuit might require a low voltage (logic 0) to turn it on (i.e., operate as active-low).

If you're working with such a circuit, you might find it less confusing to use the Pin.value() function. In the example above, led.value(0) would set the pin low (logic 0) and pin.value(1) would set the pin high.