You're reading from Banana Pi Cookbook
This chapter covers the basics of the hardware world of the Banana Pi. Just like the Raspberry Pi and the Arduino, the Banana Pi is able to interact with external electrical components. To achieve this, we are going to use the GPIO (General-Purpose input/output) pins that are introduced in this chapter.
We will switch on an LED using the shell and with a small application that we are programming. Furthermore, we will build a circuit, including a pull-down resistor and control an LED with a button. To achieve this, we will introduce the C programming language with the WiringPi
library and the Python scripting language with the RPi.GPIO
library.
This recipe is the "Hello World"equivalent of electrical experiments with a single-board computer. We will connect an LED to a GPIO pin of the Banana Pi and apply a logical high signal on that pin. This will enlighten the LED.
We require several components to switch on an LED on the Banana Pi:
A Linux system on the Banana Pi
Access to the shell
A 5mm LED (forward voltage 2.0 V)
A 470 Ω resistor
A breadboard
Two female to male jumper wires
You can conveniently purchase these components at retailers that specialize in electronics. Most of the components in this and the upcoming recipes are available for a low price.
The following steps will switch on an LED. You will find the complete Banana Pi and Banana Pro pin layout in the There's more... section of this recipe.
Tip
Before you connect or disconnect any electrical parts, power off your Banana Pi and also unplug the power supply.
Connect the first jumper wire to the GPIO 1 pin. That...
Switching on an LED by a command is one thing. The other is controlling the LED programmatically. In this recipe, we are going to write a real program in C and in Python to switch the LED on and off periodically.
We require the very same breadboard construction from the previous recipe:
A Linux system on the Banana Pi
Access to the shell
A 5 mm LED (forward voltage 2.0 V)
A 470 Ω resistor
A breadboard
Two female to male jumper wires
Construct the circuit as we have seen previously.
Once we have the construction, we can proceed with the steps given in the following sections.
We start by programming the LED in the classic C programming language.
Power on your Banana Pi.
Open a shell.
Create a source code directory and change location to it:
$ mkdir ~/source $ cd ~/source
Create a source file using nano:
$ nano led_test.c
The nano editor will open. Write the following code in C:
#include <wiringPi.h> int main(void) { ...