Setting a GPIO

The second part of this week’s 2-Bullet Tuesday! See the first part here.
You can subscribe to the newsletter on the 2-Bullet Tuesday page!

Omega Tip of the Week:

GPIO as Output

Last week we talked about reading GPIOs. This week we’ll show you how to write to GPIOs and use them as outputs to control external circuits!

Let’s try this out the same way as last time: grab an Omega, an Expansion Dock, and a male-to-male jumper wire. We will use GPIO1 to output a signal and GPIO2 to read it.

First, we need to set both the input and output GPIOs as inputs before connecting anything. This is good practice to avoid situations where an external circuit may be driving current to the GPIO before we are ready:

gpioctl dirin 1
gpioctl dirin 2

Connect the jumper wire from GPIO1 to GPIO2. Then set GPIO1 as an output:

gpioctl dirout 1

Now let’s try sending a LOW signal. Connect the wire from GPIO1 to GPIO2. Then run:

gpioctl dirout-low 1
gpioctl get 2

You should see the following output:

root@Omega-2757: /# gpioctl dirout 1
Using gpio pin 1.
root@Omega-2757: /# gpioctl dirin 2
Using gpio pin 2.
root@Omega-2757: /# gpioctl dirout-low 1
Using gpio pin 1.
root@Omega-2757: /# gpioctl get 2
Using gpio pin 2.
Pin 2 is LOW

Now let’s try writing a HIGH signal. Run these commands:

gpioctl dirout-high 1
gpioctl get 2

You should now see:

root@Omega-2757: /# gpioctl dirout-high 1
Using gpio pin 1.
root@Omega-2757: /# gpioctl get 2
Using gpio pin 2.
Pin 2 is HIGH

You can use this tool to drive LEDs, transistors, or other external circuits from the command line!

To use this in a program, try using the Onion GPIO Python module.

And that’s it!

Share

Lazar Demin