GPIO and LEDs in Linux and lua

Category

Blog IoT
24 December, 2014 0

In our previous posts, we wrote about my development in Java, but now we’ll talk about another area we’re working on: robotics. This time, I’ll talk about some basics, managing General Purpose Input Output (GPIO) and LEDs in Linux using the terminal, and using lua, a lightweight and portable language.

Basics

When working with embedded robotics or electronics interfacing with computers, it is (often) a good idea to work with linux for several reasons, just to mention a few:

  • Open source: It’s easy to customize the distro so that you can leave out parts of the OS you won’t use, making it more lightweight to run in an embedded system.
  • Portability: Linux kernel is portable to a vast amount of architectures, making it very attractive for use in embedded robotics.
  • Community: There is a big community out there using linux to develop embedded systems, developing new features to linux, testing, discussing, and so on.

GPIO and LED filesystem in Linux

When working in linux there, another great advantage is that you can control de LEDs and GPIO just by accessing the file system, without the need of any drivers. For example, if you want to control the built-in LEDs you would use the /sys/class/leds/ portion of the file system. For GPIOs you’ll use /sys/class/gpio/

When using the file system, it’s possible to control these hardware components from any linux terminal and easily create a program that uses them. In the next sections, We’ll explain how to use them from both alternatives.

Managing built-in LEDs with the terminal

It’s very easy to control the built-in LEDs, We’ll show you how in this section.

Open a terminal and go to superuser (superuser permissions are needed in order to work with GPIOs and LEDs by default, although it is possible to change this with some OS rules):

sudo su

when listing the directory /sys/class/leds on my laptop I see these items:

root@rafael-vaio:/sys/class/leds# ls
ath9k-phy0 mmc0:: mmc1::

We’ll show you how to control the LED mmc1:: (on my laptop it’s the one near the memory card slot). First, when listing the contents of the mmc1:: folder I see these items:

root@rafael-vaio:/sys/class/leds# ls /sys/class/leds/mmc1\:\:/
brightness device max_brightness power subsystem trigger uevent

The important files here are brightness and trigger. brightness, as you’ll guess, it’s the file that stores the status of the led. trigger is which trigger will change the status of the led, you can see this using:

root@rafael-vaio:/sys/class/leds# cat /sys/class/leds/mmc1\:\:/trigger
none ADP1-online BAT0-charging-or-full BAT0-charging BAT0-full BAT0-charging-blink-full-solid mmc0 [mmc1]
rfkill0 rfkill1 rfkill2 rfkill3 phy0rx phy0tx phy0assoc phy0radio phy0tpt

The important files here are brightness and trigger. brightness, as you’ll guess, it’s the file that stores the status of the led. trigger is which trigger will change the status of the led, you can see this using:

root@rafael-vaio:/sys/class/leds# cat /sys/class/leds/mmc1\:\:/trigger
none ADP1-online BAT0-charging-or-full BAT0-charging BAT0-full BAT0-charging-blink-full-solid mmc0 [mmc1] rfkill0 rfkill1 rfkill2 rfkill3 phy0rx phy0tx phy0assoc phy0radio phy0tpt

The value between the square brackets is the current trigger for this led (in this case, mmc1). To change this (remember the previous value since you should change it back when you finish), just run:

root@rafael-vaio:/sys/class/leds# echo none > /sys/class/leds/mmc1\:\:/trigger

From now on, there is no system trigger that’ll change this led, so it’s going to be only you changing the value.

To change the value and read it:

root@rafael-vaio:/sys/class/leds# echo 1 > /sys/class/leds/mmc1\:\:/brightness
root@rafael-vaio:/sys/class/leds# cat /sys/class/leds/mmc1\:\:/brightness
1
root@rafael-vaio:/sys/class/leds# echo 0 > /sys/class/leds/mmc1\:\:/brightness
root@rafael-vaio:/sys/class/leds# cat /sys/class/leds/mmc1\:\:/brightness
0

And voila, as simple as that you can start playing with the leds ?

Managing GPIO using the terminal

The GPIOs are pins that are input/output pins connected to the processor, and in Linux it is very straightforward to configure and use them.

This is a bit more complex to test from your pc since you need to access the GPIOs of the processor physically. In our case, We’ll be using these from a Single Board Computer with linux to read the value of sensors and send commands to actuators on my robotics projects. 

If you already own one of these, you probably already know how to control them, but if not, there are several options to start with. You can look at the List of single-board computers in wikipedia. In our experiments, we used a BeagleBoard rev C4 with several accessible GPIOs on the expansion bus.

So, shall we?

The steps to control a GPIO in linux are these:

  1. Export the GPIO
  2. Configure the GPIO as input or output
  3. Set the value / read the value

The first step is to export the GPIO, this will tell linux that we’ll be handling the GPIO from the file system. We’ll work in the /sys/class/gpio portion of the fs.

When listing the contents of /sys/class/gpio/ we’ll see only two files: export and unexport. In order to initialize the GPIO with id 56 run:

echo 56 > /sys/class/gpio/export

This will create a folder gpio56 with the following content:

ls /sys/class/gpio/gpio56direction edge power subsystem uevent value

To set the direction (in/out), echo the value ‘in’ or ‘out’ to the direction file:

echo out /sys/class/gpio/gpio56/direction

To set the value, just change the value file, similar as we did with the leds (values 1 or 0):

echo 1 /sys/class/gpio/gpio56/value

That’s it. If you want to use the pin as an input, you should echo ‘in’ to the direction file and read the value file with ‘cat’

LEDs and GPIO using lua

Lua is a high-level language with a very small footprint and runs as fast as a C program. This is desirable when working with embedded systems since we need to save every last bit of memory.

It is very simple to write a program that uses the led or the gpio filesystem, you just need to execute the commands seen previously, and that’s it.

You can check out the code of the simple GPIO lua library we uploaded to Github.

That’s it for now! We’ll be posting more on embedded robotics soon. Let’s talk in the comments!

bkadmin
Wednesday December 24, 2014 - 22:12 Blog, IoT
Recent posts
Reclaim your disk space from Xcode

Do you have problems with the space on your disk? You are in the right place! This post is for […]

Read More…

Continue reading
25 June, 2020
RoR – Get your exceptions notified via Slack

Have you ever wanted to be one step ahead, and be notified of any unwanted exception before your users have […]

Read More…

Continue reading
18 October, 2018
Easy creation of a new Node + React project

Starting a new project is something everybody loves to do. However, some steps are always the same for every project. […]

Read More…

Continue reading
3 October, 2018
Learning SCRUM with LEGOS

In Kreitech, we know about Scrum, and we apply it in many of our projects, which gives us very positive […]

Read More…

Continue reading
19 September, 2018