There are many different ways to control GPIO pins of the raspberry pi. We will be discussing two methods using Linux and C programming. These methods will work on other single board computers with minor modification. Also You don’t have to install any extra libraries
The sysfs method is the easiest method to control GPIO’s of the raspberry pi and this works on any raspberry pi model and any single board computer including beaglebone and orange pi. This does not mean it is the most efficient way. In fact it is the least efficient way. Then why have I written a tutorial about it? Because our goal is to learn Linux and sysfs. Not ways to blink an LED. For the same reason we will not be discussing any libraries here too. Learning Embedded Linux will make your life easier and your projects come to life faster while still having low execution latency.
User written programs/application software work in user space of the memory and kernel/OS related programs(drivers) work in kernel space of the memory. Sysfs is a way of communication between user space and kernel space programs(device drivers). It is a virtual file system. meaning It does not actually exist on the hard drive. It is created when the system boots up and gone when system shuts down. Entries in it are created when a process requests one. It was mainly developed for control and configuration of hardware devices connected to the system. In other words this file system helps in transferring kernel process data to user and vice versa. Learn what’s kernel space and user space here. In our case we will be communicating with a driver on the kernel through files inside /sys/class/gpio to control the LED. All files inside this directory belongs to that GPIO driver.
To give you an example: You can turn on and turn off the caps lock, num lock and other LED’s of your PC by using sysfs. Be sure to try that out once we’re finished .
- Raspberry Pi 2.
- LED connected to GPIO17(BCM2835 notation).
- Pull up GPIO4 for input.
LED connected to GPIO17(BCM2835 notation), pull up GPIO4 for input.
sysfs is inside the root directory: /sys
You can edit the files in sysfs from the terminal. Log into your raspberry pi. open terminal(Ctrl+Alt+T) and go to /sys/class/gpio directory. It contains some files that can be used for gpio control.
You can use ‘echo’ command to write data into files.
First activate GPIO17 in sysfs using
echo 17 > /sys/class/gpio/export
After executing this command a new directory named ‘gpio17’ will be created in the same directory. This directory contains different files that control the functionality of that gpio pin. We consider ‘direction’ and ‘value’ only.
Set as output
To set gpio pin 17 as output write ‘out’ to the file named ‘direction’:
echo out > /sys/class/gpio/gpio17/direction
To make the pin digital high write ‘1’ to the file named ‘value’. This will turn on the LED:
echo 1 > /sys/class/gpio/gpio17/value
Turn LED off:
echo 0 > /sys/class/gpio/gpio17/value
Set as input
As we learned earlier we can write and read information from sysfs. Reading the gpio pin is simple too. First make the pin an input:
echo in > /sys/class/gpio/gpio17/direction
Reading the file ‘value’ will read the input value on the pin. If you have connected a 3.3V pin to GPIO17(switch not pressed) using a resistor and read the input value you will be able to see ‘1’ displayed on the screen. If switch is pressed(pin low) while executing below command ‘0’ will be displayed on screen.
you can use ‘cat’ command to spit out contents of a file on the terminal.
We can write a shell script to do all this automatically without typing each command one by one all the time or write a C program that opens above mentioned files and writes same text as earlier. Or read the next method which is more efficient.
I will be writting a program related to ASCII character table next.