Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Things Quick Start Guide

You're reading from  Android Things Quick Start Guide

Product type Book
Published in Aug 2018
Publisher Packt
ISBN-13 9781789341799
Pages 192 pages
Edition 1st Edition
Languages
Author (1):
Raul Portales Raul Portales
Profile icon Raul Portales

GPIO - Digital Input/Output

Now that we have seen all the hardware working with the Rainbow HAT in the previous chapter, we are going to learn about the first communication protocol: GPIO (General-Purpose Input/Output). We have used it already for LEDs as output and for buttons as input, and we will extend that to other outputs and inputs. Finally, we will look at a few components that use GPIO in a more generic way (DC motor controller, stepper motor, distance sensor, and LCD display).

GPIO is the simplest protocol to control devices; it just uses digital signals that can be on or off. In digital circuits, there are two possible values for what is considered a logic 1: 3.3v and 5v. The current Android Things developer kits use 3.3v. Note that the developer kits have some 5v Vcc pins, but that is used exclusively to power external circuits and has nothing to do with the value...

Technical requirements

You will be required to have Android Studio and Android Things installed on a developer kit. You also will require many hardware components to effectively perform the tasks given in this chapter. The components are very interesting to have, just to see them working, but the Rainbow HAT is particularly important. We go into details about the developer kits and how to pick the right one, as a part of Chapter 1, Introducing Android Things. Finally, to use the Git repository of this book, you need to install Git.

The code files of this chapter can be found on GitHub:
https://github.com/PacktPublishing/Android-Things-Quick-Start-Guide.

Check out the following video to see the code in action:

http://bit.ly/2PnjkoX.

Making the code work on any developer kit

As we mentioned before, the pin names of the Raspberry Pi and the iMX7D are different. You can take a look at the pinout diagrams in the Appendix, and I suggest that you print them out to have them at hand.

Have the pinout diagram of your developer kit at hand.

When we flipped the Rainbow HAT, we could see that the red LED was wired to BCM6, which is pin 31 of the board. While the pin position is the same, that pin on the iMX7D is called GPIO2_IO02.

However, if we look at the source code of the Rainbow HAT driver, which is in Java, we see that it is accessed using a function.

public static Gpio openLedRed() throws IOException {
return openLed(BOARD.getLedR());
}

This is a concept that is used frequently on examples when you want them to work on both devices. At the very minimum you should use a constant for the pin, so if someone has to...

Using GPIO for output

Digital output is used to control devices. LEDs are just an easy and visual way to check that things work, and we will start by revisiting them to set the concepts and then look at relays, which are one of the most versatile components we can use.

LEDs

In this section we will see how to handle LEDs directly, learn a bit more about PeripheralManager (the class you use to access peripherals), and understand how GPIO works for output.

So, let's begin by replacing the utility method from the Rainbow HAT meta driver with some code that accesses the pins directly.

Given that we are already using a Gpio object to handle the LEDs, the only part that we need to change is how to open it. We already looked...

Using GPIO for input

Now that we have seen how to use GPIO for output, let's move on to input. In this section we will look at accessing Button and ButtonDriver without using the meta driver of the Rainbow HAT. Both drivers are a layer of abstraction over GPIO, so our next step will be to learn how to work with GPIO directly. Finally, we will look at a few sensors that generate a GPIO signal and how to use them.

Buttons

Buttons are quite simple to deal with when using the driver or the input driver, but they can also be used to learn how to use GPIO output at a low level.

Let's get into the button driver.

Button driver...

Other usages of GPIO

In many cases, circuits are required to use several digital signals as input or output. In these cases we need to know what they are expecting and handle it appropriately. This usually takes the form of a driver.

In this section we will be exploring briefly some of those cases, such as a DC motor controller that allows us to select the direction of two motors and a stepper motor controller.

We will also take a look at other non standard uses of GPIO that require special handling, such as an ultrasonic distance sensor and an LCD numeric display.

With all these, you will get a general idea of the type of peripherals that can be controlled using GPIO and the different ways they work.

DC motor controller (L298N)

...

Summary

We have covered quite some ground in this chapter. First, we looked at how to address the pins from code in a way that it works on both developer boards. Then we learned about GPIO as output, how to access LEDs directly, and how to use relays. We also explored GPIO as input, taking off several abstraction layers from Button reaching direct access to GPIO, which we then used for other sensors. Finally, we learned about more general ways in which GPIO is used by other components, such as DC motor controllers, stepper motors, distance sensors, and numeric displays.

It is time to move on to another protocol that is a bit more complex but also allows us to do more: PWM.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Android Things Quick Start Guide
Published in: Aug 2018 Publisher: Packt ISBN-13: 9781789341799
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.
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}