Reader small image

You're reading from  Android Things Quick Start Guide

Product typeBook
Published inAug 2018
PublisherPackt
ISBN-139781789341799
Edition1st Edition
Right arrow
Author (1)
Raul Portales
Raul Portales
author image
Raul Portales

Raul Portales is a software engineer who has had a love for computers, electronics, and gadgets in general for as long as he remembers. He jumped into Android as soon as it was released. Raul has worked on social networks, education, healthcare, and even founded a gaming studio and a consultancy company. Specializing in mobile and UX, he speaks frequently at conferences and meetups. Raul's love for electronics reignited when Google announced Android Things. He started tinkering with it with the first Developer Preview, which lead to adding the IoT category on his Google Developer expert profile.
Read more about Raul Portales

Right arrow

I2C - Communicating with Other Circuits

The Inter-Integrated Circuit bus (also expressed as IIC, I2C, or I2C) is used to connect to simple peripheral devices with small data payloads, such as sensors, actuators, and simple displays. We will start with some general concepts about the protocol to then learn how to access I2C components from the Rainbow HAT directly. We will look at some extension components, displays, and other sensors that work with I2C. We will cover the following topics:

  • Overview of I2C
  • Revisiting Rainbow HAT components
  • Extension components
  • Small displays
  • Other I2C sensors

As with PWM, the Rainbow HAT exposes the pins, so we can use the hardware on the HAT to interact with the new extra components we plug into the I2C bus.

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/2N75Sbg.

Overview of I2C

I2C is a synchronous serial interface, which means it uses a shared clock signal to synchronize data transfer between devices. The device that controls the clock signal is known as the master; in our case, the developer kit. All other connected peripherals are known as slaves. Each device is connected to the same set of data signals to form a bus.

I2C structure diagram from developers.google.com

Being a serial bus, the components need to wait for their turn to talk. That is also done by the master, which issues commands to the bus and some of those commands may require a response (for example, requests to read values). This is known as half-duplex communication.

I2C devices connect using a three-wire interface consisting of the following pins:

  • Shared clock signal (SCL): The clock used to synchronize the data transfer
  • Shared data line (SDA): The wire used to actually...

Revisiting Rainbow HAT components

Following the same approach as for GPIO and PWM, we are going to start by removing the Rainbow HAT meta driver from our dependencies and using the specific drivers of the components directly. This time the components are the alphanumeric display and the temperature and pressure sensor.

Alphanumeric display (Ht16k33)

The driver for the alphanumeric display is called driver-ht16k33, so we just need to add it to our build.gradle:

dependencies {
[...]
implementation 'com.google.android.things.contrib:driver-ht16k33:+'
}

The only difference with the code we used before is that we now have to construct an object of type AlphanumericDisplay passing the I2C bus name, which we can initialize...

Extension components

In this section we will talk about some components that extend the functionalities of our developer kits, allowing us to do more and overcoming some of the deficits with Arduino, such as the lack of analog inputs and the very limited amount of PWM pins.

Analog to digital converter (ADC) – PCF8591

There are many sensors that just provide an analog value: moisture, smoke, raindrop, light, temperature, and so on, and as we mentioned several times, the Android Things developer kits do not have any analog inputs. To be able to use these sensors as input, we need a special component called an Analog to Digital Converter (ADC).

An ADC reads an analog value and converts it to a digital value. The resolution...

Small displays

Another typical component for IoT projects are simple displays. We have already used two different LED segment-based displays, but there are other models that offer more functionality and versatility while still being cheap, such as LCD displays and OLED screens.

Both the LCD screen and the OLED screen drivers work by giving us access to the pixels on the screen, including some utility methods to draw text or even bitmaps depending on the case. The main difference is that on the LCD screen, the pixels are grouped into separated characters (5 by 8 pixels each), arranged in rows and columns (typically, two rows of 16 characters), while the OLED is a continuous screen, typically monochrome.

To use an LCD screen, we need to add the following driver by Gautier Mechling:

dependencies {
[...]
implementation 'com.nilhcem.androidthings:driver-lcd-pcf8574:+&apos...

Other I2C sensors

There are many variations of magnetometers, accelerometers, gyroscopes, and so on. In some cases, the component has a single function, such as the module HMC5883L, which is just a magnetometer, or ADXL345 and MMA7660FC (part of contrib-drivers), which are accelerometers. In other cases, they are more complete, even proper 9 DoF (Degrees of Freedom) sensors like the ones on the phones, which are also known as IMUs (Inertial Measurement Unit).

Check whether a driver exists before deciding on a component, or be ready to write your own.

In most cases there are drivers already available, even if most of them are just ports from Arduino. The structure of the driver varies a bit but it is usually along the lines of what we saw in this chapter. In the case that there is no driver, you can always read the spec sheet of the component and write it yourself. It is not as...

Summary

In this chapter we have covered the I2C protocol, which allows us to communicate with more complex peripherals. We have looked at the pin structure and how it works internally to address multiple components into the same bus.

Then we looked at the components of the Rainbow HAT that use I2C (alphanumeric display and temperature sensor) to realize that the meta driver layer is really thin. It is just hiding one parameter and the constructor. The complexity of handling these type of circuits is all about learning how each driver works.

We also looked at a few expansion boards: Analog to Digital Converter (ADC), PWM, and GPIO expanders. With all those, we can overcome the lack of analog inputs and outputs of the developer kits of Android Things.

Finally, we checked other type of sensors, including magnetometers, accelerometers, and gyroscopes, which are common in phones nowadays...

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

Raul Portales is a software engineer who has had a love for computers, electronics, and gadgets in general for as long as he remembers. He jumped into Android as soon as it was released. Raul has worked on social networks, education, healthcare, and even founded a gaming studio and a consultancy company. Specializing in mobile and UX, he speaks frequently at conferences and meetups. Raul's love for electronics reignited when Google announced Android Things. He started tinkering with it with the first Developer Preview, which lead to adding the IoT category on his Google Developer expert profile.
Read more about Raul Portales