Moving Past “Blink” with Android Things

When you speak with developers about their experiences with IoT and electronics, you’ll often hear that they managed to run through the Blink sample (the “Hello World” of electronics) with an Arduino or Raspberry Pi, and then stopped. The reasons for stopping vary, including not knowing what the next step should be, not having additional hardware, or psyching themselves up to think that electronics are too difficult. Either of these issues can lead to people losing interest before they even attempt a second project. At one point or another, all three have also applied to myself.

One of the ways over these hurtles is to know what software or hardware tools and components are available to create your next project or to solve a particular problem. Once you know what’s available, you can plan out projects that are relevant to you and your interests, can figure out what hardware to buy, and can find the proper documentation to get past the “this is probably difficult” hump by just building something.

This article will introduce you to a collection of hardware components that you can use for creating your next project, as well as some of the tools provided by Android Things to interact with that hardware and do something meaningful with the world around the device. Since we’re focusing on the Internet of Things, we will also consider some of the backend frameworks that are available to easily integrate with your Android Things devices.

Hardware Components

There are two categories of components that we will focus on: sensors and actuators. Sensors control the flow of electricity in a circuit based on inputs from the world around them, either through an active event, like the press of a button, or passive conditions, such as environmental temperature. Actuators use electrical energy passed to them to interact with the world around them, such as a screen displaying information, or a moving motor.

While this section will introduce you to various hardware components, it will not go into detail on how those components are used in a circuit. There are various sites and books that you can use to learn the intricacies of building a circuit by simply searching for the component you want to use or by checking out videos on YouTube. Links to useful resources can also be found at the end of this article.

Sensors

Conductive Cords: These rubber cords change their resistance based on how much they are stretched. When placed in a voltage divider circuit, you are able to measure how far the cord has been pulled by how high the voltage reads on the circuit. You can use these in robotics for measuring how far a robot has moved in a given direction, or in something like an electronic cat trees for triggering an event when a toy on the end of the cord has been pulled.

Potentiometers: Generally build with internal voltage dividers, these components have a variable resistance based on how far their dial is turned. They can be used to provide a temporarily fixed output voltage for tasks such as the volume on a speaker or the brightness of an LED display.

Flex Sensors: This sensor consists of small metal squares on the outside, and increases resistance as the sensor is bent. Originally patented by Spectra Symbol, they claim that these were used in the original Nintendo Power Glove for measuring the bend of a hand within the glove.

Force Sensors: Perfect for detecting pressure on a surface, these flat sensors can change their voltage when acted upon by an outside force. Typically they aren’t the most accurate, so you may not want to use them for a scale, but they are great for detecting when a pressure has been applied.

Load Sensors: While the force sensor isn’t accurate enough for a scale, a load sensor is. One thing to note about load sensors is that you will often need an amplifier component to get a better reading for your project.

Motion Sensor: As the name suggests, this sensor detects motion. Many standard motion detectors will allow you to set the sensitivity and wait time between events by adjusting built in potentiometers.

Proximity Sensor: Often used in robotics to determine if an object (such as a wall) exist in front of the device. One variant of these sensors works by emitting an ultrasonic sound and listening for the echo, much like echolocation in dolphins and bats.

Switches and Buttons: Switches directly control the flow of electricity on a circuit by creating or breaking pathways between the source and ground. While a standard switch can maintain its position without constant interaction, buttons are typically momentary switches that are only in their activated position while being acted on.

Passive Environmental Sensors: Some of the more common sensors that you’ll find in tutorials and projects, environmental sensors do what you’d expect: they measure different properties of the environment around them. Some sensors in this family that you can find include photoresistors, which measure the amount of light shining on the sensor, soil moisture, ultraviolet light, air quality, humidity, and even the presence of flames.

Sensors from left to right: temperature/pressure, air quality, humidity, UV, soil moisture

Microphones: Microphones have been gaining prevalence in IoT devices as voice recognition technology has continued to improve. Technologies such as Google Assistant and Amazon Alexa allow developers to easily add voice actions to their IoT devices by simply including a microphone for listening to user input and sending data to the proper API.

AIY voice hat microphone and USB microphone

Cameras: Some of the more complex sensors you may use, cameras allow your device to see the world in the same way as your users. They can be used to scan bar codes, detect and recognize faces, analyze colors or simply take a picture and forward it to a user.

Raspberry Pi camera module, and an Arduino CamShield module

Actuators

Relays: These are mechanical switches that are controlled by a small voltage in order to allow a large amounts of power to pass along a circuit. They can be useful for creating devices that require a larger power flow than you would normally get from a battery, such as something that must be plugged into a wall outlet. The way most common relays work is that there are two metal plates that are separated from each other by a thin gap, and behind one of the plates is a metal coil. When a small amount of energy is applied to the coil, an electromagnetic force is created that pulls the second plate closer to the first in order to close the gap and make a connection for the circuit, allowing electricity to flow into the device that requires more power.

LEDs: You’ll probably recognize these from your “Blink” project :) Light Emitting Diodes (LEDs) are exactly what they sound like — components that emit light. They can come in a variety of colors and sizes, and some even support ranges of colors by combining smaller diodes together.

Motors: Consisting of a set of gears within a container, motors move along a pivot in order to interact with the world around them. Continuous motors can be used for robotics or vehicles, whereas servo motors move to a given position and maintain their state.

Speakers: Most people are familiar with speakers from standard electronic devices that are used every day. They can output music, voices, or even just buzzing sounds. They can be used to provide audio feedback to a user based on the conditions of your device.

Speaker that can be connected to an I2S connection, and a simple buzzer

Displays: About as common as speakers, displays appear on television sets, computers, phones, electric marquees and LED boards. They provide visual feedback to users and come in varying shapes, sizes and type. Displays may be an LED matrix, liquid crystal (LCDs), segmented displays, plasma screens or organic LED (OLED) to name a few.

Integrated Circuits

Rather than having to build complex circuits completely from scratch, some common circuits have been packaged into small ‘chips’ known as integrated circuits (ICs). While there’s a huge number of ICs available, I will only discuss a couple of useful chips that you will most likely use while working with Android Things.

555 timer and an MCP3008 ADC

Analog to Digital Converters (ADCs): ADCs convert analog signals from sensors to a digital signal that can be read by your IoT device. Since Android Things only supports digital input, an ADC is needed to read sensors that produce analog readings by interacting with the natural world.

Microcontroller Units (MCUs): These are the chips that you often see in an Arduino board. MCUs are important because they allow you to support sensors and actuators that may not be natively supportable by Android Things. One example of this is the DHT11 humidity sensor that requires signals in the nanosecond range, where Android only supports time in milliseconds. Since most complex components also have libraries already written for Arduino devices, MCUs can be used to quickly add those components to a prototype project for testing.

Shift Registers: Sometimes the prototyping board you’re working with simply doesn’t have enough pins for what you want to do. Shift registers allow you to take a small number of pins and interact with a higher number of connections, allowing you to add more components to your circuit than your board would have originally allowed.

Android Things

Once you’ve figured out which hardware components you would want to use for a project, you need to think about the software side of it. While Arduino is great for starting out and learning the basics, you’ll quickly find tasks that could be a lot easier. This is where Android Things comes in. Android Things is a modified version of the Android operating system designed specifically for IoT development. It is also a platform that be easily used for production of consumer devices. Although this section will not go into the details of creating an IoT device with Android Things, it will introduce you to various capabilities supported by the operating system that may be useful as you build projects. As many of these components exist on standard Android phones, you can find tutorials and guides for using them online, giving you the support of the vast and already established Android community.

Android Components

Camera: Even though cameras are complex sensors, there’s no reason for them to be difficult to use as a developer. Android already supports the cameras on mobile devices, and allows developers to access and use images taken by them. With Android Things, camera inputs can be automatically routed to your app and accessed through the Camera 2 API, allowing you to quickly develop apps using that peripheral.

Text to Speech: Android supports text to speech natively within the operating system, which means your IoT devices can also speak to users over a USB, auxiliary, or I²S speaker.

Data Storage: IoT devices often generate or need to receive a lot of data, as well as needing to save confirmation information. Using Android, you can store this data as preferences, in a SQLite database (may also be worth checking into the new Room architecture component), or in any other framework supported by the Android OS.

Communication: You can’t have IoT devices without the Internet. Rather than requiring a network or bluetooth peripheral attached to your device, Android Things uses devices that support a System on Module (SoM) architecture. These SoMs include (among other things) bluetooth and WiFi built in to the module, and are accessible through the Android communication frameworks for WiFi and bluetooth.

Device Control: Android Things provides a DeviceManager object that allows you to reboot or factory reset devices, which is useful for users of commercially available devices.

User-Space Drivers

Android has multiple frameworks in place for organizing data received by a device, such as user input, location, audio and sensors. With Android Things, if your IoT device includes a GPS, buttons, or sensors, you can easily incorporate the data from that component into the Android framework through the use of drivers. This will allow you to interact with framework components, such as the LocationManager or SensorManager, as you normally would with Android while also getting your new data.

Backend Services and Additional Libraries

In addition to the Android operating system, Google provides various services and libraries to help you create complex applications. In this section we will explore some of the services that can be easily to use with Android Things. These frameworks include Google Play Services and Firebase, which are a collection of classes that interface with Google’s backend services, and various machine learning tools, such as the Google Assistant and TensorFlow, and the Google Cloud Platform, which includes additional machine learning and data management tools. Each of these services can do a lot, and there are multiple tutorials and articles dedicated to small portions of all of them. This article will simply introduce a few key features that may be useful for your IoT projects.

Google Play Services

Mobile Vision: This API allows you to take information from a camera and detect faces, read bar codes, or recognize text. Using this API, you can analyze the detected faces to determine the likelihood of people smiling, or take the recognized text and have it translated for your users.

Faces detected from an Android phone using the face detection API

Awareness: Ensuring that your devices are contextually aware is just as important on IoT devices as it is on a mobile phone. Using the Awareness API, you can make devices start up or turn off automatically when specific conditions are met, such as the user returning home or within time windows.

Android Things fan turning on automatically when using the Awareness API

Google Fit and Activity Recognition: If you’re making a health and fitness related device, the Google Fit and Activity Recognition APIs will allow you to determine if your user is running, walking, sitting, etc. and then sync their exercise data with the Google Fit cloud store, making it accessible from any other device that they use with Google Fit.

Nearby: Not all devices need to operate alone. Using the Nearby API, you can easily communicate between different nearby devices. This is especially useful when you want to configure an offline IoT device to get it onto a wireless network through the use of a mobile app.

Firebase

Database: Using Firebase Database, you can sync the data related to an IoT device across multiple other devices, such as a web or mobile app that can be used for controlling it. Firebase Database supports data change listeners, so as soon as the state of a device on the backend is changed, your IoT device can immediately react to the change.

File Storage: If you are using a camera on your IoT device, you may want to store any captured images somewhere for your user to access. Using Firebase Storage, you can upload images and send the link to your user’s mobile device, allowing them to see whatever the device has detected.

Analytics and Crash Reporting: Understanding how users are interacting with your devices allows you to improve and update to fit their needs. Likewise, understanding where crashes happen allows you to fix bugs that could be frustrating your users. Both can be easily added to your devices using Firebase.

Machine Learning

TensorFlow: Image classification is a tool that provides a different way to solve old problems. Using TensorFlow, you can train a model to classify new images taken by your IoT device without needing to be online. TensorFlow has been used for numerous for numerous applications, including skin cancer analysis and animal detection.

TensorFlow on an Android Things device classifying wildlife with a 98% confidence

Assistant API: The Assistant API allows you to include the Google Assistant on your new IoT device, allowing users to ask questions, set timers and alarms, and a whole host of other tasks that may be useful on a smart home device.

Google Assistant built into an Android Things device

DialogFlow: Formally known as API.AI, this API handles natural speech processing and conversations. Sentences that fit patterns, as specified by you, can have actions taken to fulfill the user’s requests. DialogFlow can be used on IoT devices with the Assistant API, or on other devices using the API, such as Google Home.

Communicating with an IoT device using a Google Home

Additional Resources

Official Android Things SDK documentation

Hackster.io: Projects built by the community. Great for ideas, documentation on how people build their projects

SparkFun Tutorials: Great electronics tutorials on concepts as well as implementation

Android Things Drivers: Google has put together a set of classes that allow you to easily add common hardware components to your apps. These include buttons, servo motors, LEDs, GPS modules and the AIY voice hat for Raspberry Pi

Adafruit Learning Center: tutorials about general hardware sold by Adafruit.

The Art of Electronics: If you’re more interested in a formal book for learning electronics, The Art of Electronics is one of the best you can get. As you go along, you can figure out how to integrate the circuits that you learn from the book with Android Things projects.