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

PWM - Buzzers, Servos, and Analog Output

Pulse Width Modulation (PWM) is a one-wire, one-way communication protocol. Both Android Things developer kits have two PWM outputs -this is one case where Arduino has the upper hand- but we will see how to overcome that limitation in the next chapter. The Rainbow HAT uses one of them for the piezo buzzer and exposes the other via the pins on the HAT. We will take advantage of this setup by using some of the hardware of the Rainbow HAT for our examples.

Arduino has a lot more PWM outputs than Android Things. We'll see how to work around that in the next chapter.

And as usual, since we will be using peripherals, we need to make sure that the USE_PERIPHERAL_IO permission is being declared in our manifest:

<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO"/>

We will start by learning...

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

PWM overview

PWM is an output digital signal that is commonly used to apply a proportional control signal to an external device using a single digital output pin.

PWM is a square wave (no intermediate values, only 0 and 1) that oscillates according to a given frequency and duty cycle. It encodes the information in the length of the pulse, hence its name. A Pulse Width Modulation signal has two components:

  • Frequency (expressed in Hz), which describes how often the output pulse repeats
  • Duty cycle (expressed as a percentage), which describes the amount of time the pulse is on

For example, a PWM signal set to 50% duty is active for half of each cycle:

You can adjust the duty cycle to increase or decrease the amount of time the signal is on, which is reflected in the average value of it (the analog equivalent). The following diagram shows how the signal looks like with 0%, 25%, and...

Piezo buzzers

Similar to what we did for GPIO, let's remove the Rainbow HAT driver from our dependencies and include only the drivers we require. For this first example, we just need to add the driver for the piezo buzzer, which is called driver-pwmspeaker:

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

And then, we simply need to instantiate a Speaker object attached to the correct PWM pin:

class PianoActivity : Activity() {

[...]

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
buzzer = Speaker(BoardDefaults.piezoPwm)
[...]
}

[...]
}

If we dig into the source code of the driver, we can see that it sets the duty cycle to 50% and then modifies the frequency when we invoke play. The buzzer uses the frequency from the PWM signal to vibrate and...

Servos

Besides relays, servo motors are one of the most interesting and versatile components that are frequently used on IoT projects. A servo motor can turn itself to a determined angle with high precision. They can be used to steer a robot car, orient a camera, as actuators on a robotic arm, and so on. They are -obviously- controlled using PWM.

Robotic arms and camera brackets rely on servos

Servos use PWM in a particular way. They do not read the duty cycle in the way you would expect; they do check the length of the pulse. That length is not related to a percentage of the period, but an absolute number. A servo expects this value to be sent about every 20 ms, so we will work at 50 Hz. Repeating the signal is important to make the servo return to that position if it was moved -i.e. by brute force- but without an external interaction, the servo will simply stay in the last...

PWM as analog output

In the introduction we talked about the average value of a PWM signal. That average can be considered an analog value for the signal as long as the frequency is high enough that the circuits do not notice it. Many circuits will have voltage regulators to compensate for the variations on the input and in that case it will be in fact an analog output. This analog value is directly proportional to the duty cycle.

Since we only have one PWM available we will use a single color LED to visually understand how analog outputs work. In the case of the LED, the intensity of the brightness will be proportional to the value of the duty cycle.

We will use a red LED for this example, connected as shown in the following diagram. We connect the LED to the PWM pin, add a 330 Ω resistor to take care of the extra voltage, and then we connect again to ground. A mini breadboard...

Summary

In this chapter we have learned how PWM works and how it encodes information in both the frequency and the duty cycle. We have seen how the frequency is key to working with a passive buzzer and learned to manipulate it directly. We also learned how servos use PWM in a special way and how to modify the configuration to match the specs of a target servo. Finally, we have learned how to use PWM as analog output with a simple circuit.

So far we have used single direction protocols. Let's step up a level and start working with bidirectional communication with I2C, which also supports multiple devices on the same bus. This will be the focus of the next chapter.

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