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

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...

lock icon The rest of the chapter is locked
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 €14.99/month. Cancel anytime}