To follow this tutorial, you will need:
- 220 ohm resistor
GPIO numbering on ESPRESSObin GPIO headers begins on the first pin on the right side (these can be also seen marked on your ESPRESSObin board):
Obviously, pin below 1 is 3, below 3 is 5; pin below 2 is 4, below that is 6 and so on. Relative to the board image above, pin layout for ESPRESSObin looks like this:
Here we can see which pins can be used as GPIO pins, which will be needed later.
Connecting LED to 3v3 pin
We will begin by plugging the LED on the breadboard. We have, for example, plugged the anode (long leg, shown here with a "bend") of the LED into row F, column 20, and the cathode (short leg) into column 21 in the same row. This can be seen on the image below:
An important thing to note is that you must ALWAYS use resistors to connect to your ESPRESSObin board. When connected, LEDs will want to draw more and more current, and resistors will ensure that only small current will flow and that your ESPRESSObin board will not burn out.
We will put the resistor on row J so that one end shares the same column as the LED anode side (so that the resistor bridges with column 20) whereas we will plug the other end of the resistor to column 16 on the same row). Depending on where you have placed your LED you can also connect the resistor on the cathode's side. Which way round you connect the resistor is irrelevant. This is how the breadboard looks at this point:
To repeat once again, our resistor is connected to row J, on one end to column 16 and on the other end it bridges with our LED anode on column 20. The anode bridges the LED cathode (column 21) where we will later connect ground.
To start playing with GPIO pins on ESPRESSObin we will first connect our LED to a 3v3 pin on the ESPRESSObin. On the pin mapping scheme from the beginning of the tutorial we can see that the first two pins on every GPIO header are ground pins. The third and fourth pins on the left header are 3v3 pins. So, firstly we will plug one jumper (we have used a black jumper) to pin 2 (ground or GND). Then we will plug another jumper (blue) to a 3v3 pin, namely pin 4 which is right below pin 2:
Next we connect our "positive" (blue) jumper to column 16 on the breadboard (in the same row where we connected our LED):
and lastly we connect the ground jumper (black) to our cathode (column 21, row J, next to resistor) which should light your LED:
Going back to the pin mapping layout, to follow the instructions in this section you can use any ground and 3v3 pin available on ESPRESSObin.
Enabling GPIO pins
To enable GPIO pin functionality, go into kernel and issue:
espressobin@buildserver:~/kernel/4.4.8$ export ARCH=arm64 espressobin@buildserver:~/kernel/4.4.8$ export CROSS_COMPILE=aarch64-linux-gnu- espressobin@buildserver:~/kernel/4.4.8$ make mvebu_v8_lsp_defconfig
Now enter make menuconfig and there enable GPIO_SYSFS:
espressobin@buildserver:~/kernel/4.4.8$ make menuconfig # once in make menuconfig search for and select Symbol: GPIO_SYSFS [=y] Type : boolean Prompt: /sys/class/gpio/... (sysfs interface) Location: -> Device Drivers (1) -> GPIO Support (GPIOLIB [=y]) Defined at drivers/gpio/Kconfig:73 Depends on: GPIOLIB [=y] && SYSFS [=y]
after that recompile kernel images, transfer them to your microSD card and boot ESPRESSObin from it.
Driving a LED with GPIO
The GPIOs on ESPRESSObin, when set to HIGH, output 1v8. In other words, GPIO outputs on ESPRESSObin can be either on or off, HIGH or LOW, 1v8 or 0v. This in turn means that an LED for 3v3 or higher will result in a dimmer light (or will not light up at all) if you use a GPIO pin as the current source (pulling GPIO pin HIGH). You can always use the 3v3 pin as the current source and pull the GPIO pin LOW (thus making it act as GND) to turn on your LED. We will cover both cases below.
Pulling GPIO pin HIGH
Once in ESPRESSObin console, head to /sys/class/gpio/ directory. This directory contains three kind of entries:
- Control interfaces for userspace control over GPIOs
- GPIO controllers
If we list the contents of our /sys/class/gpio directory:
root@localhost:/sys/class/gpio# ls export gpiochip0 gpiochip36 unexport
we can see that we have two GPIO controllers (gpiochip0 and gpiochip36) and two write-only control interfaces (export and unexport). By writing a certain GPIO number to export file we ask the kernel to export control over that GPIO. On the other hand, by writing a certain GPIO number into unexport file we undo the process and unexport a previously exported GPIO number.
For example, we will use pin GPIO1_8 located on the left GPIO header (15th pin, meaning 8th pin on the right):
To keep things simple, we will be using the same breadboard arrangement as in the last section. Using a jumper (blue here) connect GPIO1_8 pin to column 16 (LED anode) on the breadboard. Then using another jumper (black) connect a GND pin (we will use pin 2 on the left GPIO header) to your LED cathode (column 21, row J, next to resistor):
Head back to ESPRESSObin console. To create a "gpio8" node — that is, to enable access — for GPIO #8 we will issue the following command:
root@localhost:/sys/class/gpio# echo 8 > export
You should now get a gpio8 directory containing the following read/write attributes:
- direction -> either "in" or "out" meaning a certain GPIO is either set as "input" or "output".
- value -> either "0" (low) or "1" (high). Value can be written only if the GPIO is configured as an output by changing its direction. Furthermore, any non-zero value is treated as "high".
- edge -> either "none", "rising", "falling" or "both". This can only be configured if the pin can be used as an interrupt-generating input pin, which is out of scope of this tutorial.
- active_low -> either "0" (false) or "1" (true). Used to invert the *value* attribute both for reading and writing. This attribute will also not be used in this tutorial.
So, now that we have exported our GPIO pin, we can check its current direction:
root@localhost:/sys/class/gpio/gpio8# cat direction in
To turn this GPIO pin into a current source, we need to set it as an output pin. To do that, we issue:
root@localhost:/sys/class/gpio/gpio8# echo out > direction root@localhost:/sys/class/gpio/gpio8# cat direction out
Next, we must set the value for our GPIO — meaning, set it to HIGH — which we do with:
#first we check the current value root@localhost:/sys/class/gpio/gpio8# cat value 0 #and then change it to 1 (HIGH) root@localhost:/sys/class/gpio/gpio8# echo 1 > value
and our LED should light up, although as noted before we are using 3v3 LED so the light is slightly dim. Of course, by echoing 0 then 1 and so on you can turn your LED on and off.
Pulling GPIO pin LOW
We can also pull the GPIO pin LOW thereby using it as ground (GND, 0v) and use the 3v3 pin as the current source. Using one jumper (red) connect 3v3 pin (we will use pin 4 on the left GPIO header) to row F, column 16 (LED anode) on the breadboard. Then use another jumper (black) to connect GPIO1_8 to LED cathode (column 21, row J, next to resistor):
When we pull a GPIO pin LOW, it basically acts as if it was GND. To do that, we issue:
#of course, export GPIO1_8 and set it as an output pin if you have not already root@localhost:/sys/class/gpio# echo 8 > export root@localhost:/sys/class/gpio/gpio8# echo out > direction #pull GPIO1_8 pin LOW root@localhost:/sys/class/gpio/gpio8# echo 0 > value
and our LED should light up (since we're using 3v3 as current source, the light is brighter than in previous example). That is it, you have successfully powered a LED using GPIO on ESPRESSObin.