Reader small image

You're reading from  Banana Pi Cookbook

Product typeBook
Published inJun 2015
Publisher
ISBN-139781783552443
Edition1st Edition
Right arrow
Author (1)
Ryad El-Dajani
Ryad El-Dajani
author image
Ryad El-Dajani

Ryad El-Dajani is a software engineer and passionate technology enthusiast. He developed an interest in computers when he was 10 years old. Soon thereafter, he began to learn his first programming languages. After he had completed his training as an IT specialist, he worked on several e-commerce Internet projects. Currently, he is studying business computing and works for a big IT company in Germany, realizing various IT projects based on Java or .NET technologies. Besides the classic application development, he has professional experience in realizing projects based on web frameworks such as Spring, Play, Symfony, eZ Publish, and Magento. Moreover, he has been excited about all kinds of Unix-like embedded systems since the revolutionary SheevaPlug.
Read more about Ryad El-Dajani

Right arrow

Chapter 5. Using the GPIO Pins

In this chapter, we will cover the following recipes:

  • Lighting up an LED using the gpio command

  • Programming the LED

  • Using the GPIO input with a pushbutton

Introduction


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.

Lighting up an LED using the gpio command


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.

Getting ready

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.

How to do it…

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.

  1. Connect the first jumper wire to the GPIO 1 pin. That...

Programming the LED


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.

Getting ready

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.

How to do it…

Once we have the construction, we can proceed with the steps given in the following sections.

Programming the LED with C

We start by programming the LED in the classic C programming language.

  1. Power on your Banana Pi.

  2. Open a shell.

  3. Create a source code directory and change location to it:

    $ mkdir ~/source
    $ cd ~/source
    
  4. Create a source file using nano:

    $ nano led_test.c
    
  5. The nano editor will open. Write the following code in C:

    #include <wiringPi.h>
    
    int main(void) {
            ...

Using the GPIO input with a pushbutton


We controlled the LED in the previous two recipes by the GPIO output mode. The next step is to use the GPIO pins as input pins. There is a basic electronic component to demonstrate the GPIO input mode easily—the pushbutton.

In this recipe, we are switching on an LED while a pushbutton is pressed. Once the pushbutton is released, the LED is switched off again.

Getting ready

The following ingredients are required for this recipe:

  • A Linux system on the Banana Pi

  • Access to the shell

  • A 5 mm LED (forward voltage 2.0 V)

  • A pushbutton

  • A 470 Ω resistor

  • A 10K Ω resistor

  • A breadboard

  • Four female to male jumper wires

  • One male to male jumper wire

How to do it…

We build the circuit and program the Banana Pi afterwards.

Preparing the circuit

The circuit in this recipe looks a bit more complex than the previous one.

Note

Before you connect or disconnect any electrical parts, power off your Banana Pi and also unplug the power supply.

Let's see how to prepare the circuit:

  1. Connect the jumpers...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Banana Pi Cookbook
Published in: Jun 2015Publisher: ISBN-13: 9781783552443
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ryad El-Dajani

Ryad El-Dajani is a software engineer and passionate technology enthusiast. He developed an interest in computers when he was 10 years old. Soon thereafter, he began to learn his first programming languages. After he had completed his training as an IT specialist, he worked on several e-commerce Internet projects. Currently, he is studying business computing and works for a big IT company in Germany, realizing various IT projects based on Java or .NET technologies. Besides the classic application development, he has professional experience in realizing projects based on web frameworks such as Spring, Play, Symfony, eZ Publish, and Magento. Moreover, he has been excited about all kinds of Unix-like embedded systems since the revolutionary SheevaPlug.
Read more about Ryad El-Dajani