Let’s go now to comment some of the most interesting facets of our card: its possibility of communication with outside world. In order to do this, we will describe – in a simple way – how the Raspberry Pi uses its general purpose signal port , well known as GPIO.
Note: this entry is based on the Raspberry Pi hardware running Raspbian software (eg Wheezy)
We will discuss in this first article – in the most didactic way – the hardware associated with that port, as well the way how Linux handles the same, finally we will comment an I/O «educational» board (XUE-001), that allow us to control from a Raspberry one output and one input signals, or telling in anoter way: to make Raspbian control an external hardware via software.
Summary of what will discuss below is:
(1) A (very) basic description of GPIO port.
(2) Some theory about inputs and outputs.
(3) Hardware I/O for Rasperry Pi: XUE-001 board
(4) How Linux «see» the Raspberry Pi GPIO port.
(5) First (simple) test program: reading and writing via GPIO.
1 – A (very) basic description of GPIO port.
The Raspberry Pi board can communicate with external devices through buildt in GPIO connector. In this connector are integrated power pins (+5 and +3.3 V), ground, and inputs / outputs pins capable of implementing different protocols.
Since exist two different versions of Raspberry Pi hardware (rev. 1 and 2), port assignments also can vary. Without entering – at this the moment – in the details of protocols, the two possible versions of our Raspberry Pi drive us to two possible hardware scenarios. We can see here a comparative diagram pin of GPIO numbering.
We have to keep in mind that – at software level – we need to know which pin we want to use for communicate, and therefore, in this and subsequent entries we will choose common to both releases.
In addition to the pin for +5, +3.3 and ground, we have several general purpose pins where we can connect hardware devices. In this article, for now, we’ll explain how to use a pin as input (INPUT) and another as output (OUTPUT).
It is very important to note that any manipulation error, wrong wiring or static discharge at GPIO pins can permanently damage our board. For this reason we recommend to faithfully follow the recommendations given from now on.
2 – Some theory on inputs and outputs ..
When a computer, microprocessor or microcontroller drive any control device via I/O the software communicates with an external hardware device, and the way of doing are typically two:
OUTPUT: a logic output is generated (write) on a pin configured as output.
INPUT: a logic input is received (read) from a pin configured as input.
In this post we will take a couple of pins to perform the commented read/write sequences and this way control an external device, that’s means, enabled or disabled by the output pin, and receive a logic signal from the input pin.
In our example, we will use the XUE-001 board from Webtronika
and the pins used for this board are:
Pin 8 (GPIO14): digital output
Pin 24 (GPIO8): digital input
Since the «logic level» that Raspberry Pi handles is 3.3 V, the possibilities and relationships between software and hardware are:
For input (Pin 24):
> We will read a logic 1 (software) when we have 3.3 V in the pin (hardware)
> We will read a logic 0 (software) when we have 0 V in the pin (hardware)
For output (Pin 8):
> We will write a logic 1 (software) to obtain 3.3 V at pin (hardware)
> We will write a logic 0 (software) to get 0 V at pin (hardware)
In next entries we will describe settings such as protocols, pull-up resistors, etc.. but – for now – can continue without using these parameters.
4 – How Linux «see» the Raspberry Pi GPIO port.
Linux, as many people know, reference almost everything as a «file», and the GPIO port pins would not be an exception. We don’t need to be «experts» in Linux and know about all internal management, but what we should know is that the «core» (kernel) of Linux already knows of the existence of that port, but not the «outside» Linux system, this is the reason because we have to «inform» our Raspberry board (via software) about the mentioned GPIO port.
To do this task we will have to «export» the information regarding I/O from the Linux kernel before we can access the port itself, and we will do it as an standard file. To do this we must execute the commands for «create» access to hardware via files, and then we would access to them from the path /sys/class/gpio.
Note: The export operation has to be done as root (su)
If we look to mentioned path before «mapping» any port we can see that the content is:
Now we acquire root privileges to execute commands through
sudo-i
and we execute the first command to export the pin GPIO14 as output
echo «14» > /sys/class/gpio/export
we give full permissions to the file
chmod 777-R / sys/class/gpio/gpio14
we configure pin direction as «out»
echo «out»> / sys/class/gpio/gpio14/direction
If we take a look again to the contents of /sys/class/gpio/ we have:
and we find that gpio14 is «exported»
We repeat the a similar process with the pin that we will use as input (gpio8), thus:
echo «8»> / sys / class / gpio / export
chmod 777-R / sys/class/gpio/gpio8
echo «in»> / sys/class/gpio/gpio8/direction
and then we can check again the result in folder /sys/class/gpio/
At this point the pins in the card that we will use via XUE-001 are ready, so we have only to choose the programming language we like and proceed to control the XUE-001 board from the Raspberry Pi.
5 – First (simple) test program: reading and writing via GPIO.
Controlling XUE-001 board is not complex if we follow the instructions mentioned below. The easiest way to control GPIO outputs is by commands from the operating system. Its management by high-level languages will be discussed in subsequent posts.
The steps to follow are:
(A) – Connect the XUE-001 board to the Raspberry GPIO port through the ribbon (flat) cable that XUE-001 kit includes.
Note: we must connect this cable to the unpower Raspberry Pi board , and paying special attention to the correct connector insertion in both Raspberry and XUE-001 baoard.
(B) – Connect USB (power) cable to the XUE-001 board. This is a typical cable USB-USB-B (female) used normally to connect USB printers to PC.
Note: USB-B cable is not included with XUE-001 board kit.
(C) – We power-up the Rasspberry Pi card and let the system boot. If the process is successful the yellow LED should light as soon the Raspberry Pi is powered (Raspberry POWER signal) and green LED (OUT) as well.
Note: If we have «exported» GPIO ports previously green LED is off
Red LED (IN) must be off (unless you press the TEST micro-switch).
(D) – We export the Raspberry Pi pins (gpio14 as output and gpio8 as input ) as we discussed in the previous section.
Now we’re ready to read and write in our Raspberry Pi GPIO port Here we go …
– Controlling Raspberry Pi OUTPUT –
The connector [OUTPUT] in XUE-001 board incorporates 3 points (screws), as follow:
* NC – Normally closed contact
* Co – Common contact
* NO – Normally Open contact
These contacts come from a relay which, in origin, is managed by the signal that arrive to XUE-001 board from Raspberry Pi, and is optocoupled for maximum safety, adding a hardware isolation between our Raspberry pi and XUE-001 board.
If we made the «export» (before sending any command from the Raspberry Pi) only the yellow LED (POWER) should be active. At this time the relay keeps contacts «Co» and «NC» closed.
To change the output status – and thus the relay, green LED indicator and any device connected through it – run the command:
echo «1»> / sys/class/gpio/gpio14/value
that enables the output and force the relay to switch. At same moment the contacts «Co» and «NO» should be closed and the green LED should be activated.
in order to disable the output run the command
echo «0»> / sys/class/gpio/gpio14/value
Now the contacts «Co» and «NC» must close and green LED should turn off.
We can control an external device from our Raspberry using the relay contacts . Which one? … that depend on our imagination.
Note: power consumption of device controlled by relay must not exced 750 mA (max 1A).
– Controlling Raspberry Pi INPUT –
The XUE-001 board incorporates a micro-switch labeled «TEST«, which allows us to make a quick check of input signal status. This results very interesting because we can verify that Raspberry Pi board reads the input signal before connecting any signal or sensor to it.
we can observe that the value obtained depends on we press (1) or not (0) the micro-switch «TEST«. Accordingly we can create some piece of software that act upon the state of our entry …
XUE-001 board can use two INPUT modes, these are:
Mode A – (switch) mode => activated by closing the contacts (SW-SW)
Mode B – (signal) mode => activated by voltage level [ 5 to 12 V ] in contacts (+ -)