Hackernoon logoAndroid Things Basics: Obstacle avoidance sensor with LED feedback by@danieldallos

Android Things Basics: Obstacle avoidance sensor with LED feedback

Daniel Dallos Hacker Noon profile picture

@danieldallosDaniel Dallos

Android and IoT enthusiast | iOS developer | Technology lover

In this project we will use a LED as a notification when an obstacle is too close to our sensor.

We will learn how the standard I/O works with a GPIO in Android Things.


We use a Raspberry Pi 3 with pre-installed Android Things Developer Preview(version 1).

I have a cheap Obstacle Avoidance Sensor and a Dual-color Common Cathode LED for this project.
You can use any LED what you want, I had only this at home.


Google has a really good tutorial how to prepare your Raspberry Pi 3 for Android Things development, so I don’t want to describe it again.

Please follow the steps here:


So, let’s see our sensors.

The Obstacle Avoidance Sensor has 4 pins, we use only 3 of them. The 4th (Null), the top one is not in use.

As you see in the picture, the bottom one needs to be connected to a Ground (GND) pin on the Pi, the 5V obviously to a 5V power pin, and the Digital Output pin needs to be connected to a GPIO.

The Digital Output pin sends the signals to our development board if an obstacle is too close.

The LED module is a bit simpler.

It has only 3 pins, we will use 2 of them, but of course, you can connect all the 3 to your Pi for testing.

The GND connects to a Ground pin on your Pi, and the Yellow (or Green on some modules) and the Red input pins need to be connected to a GPIO (1 pin - 1 GPIO) on your board. (For this project one Input pin is enough to be connected.)

(If you have a simple LED, you have only 2 pins, 1 Ground and 1 Input pin.)

Here is how I connected the Obstacle Avoidance Sensor pins with the Raspberry Pi:

  • Null
  • Digital Output → GPIO 21 (BCM21, Pin 40)
  • 5V → 5V (Pin 4)
  • GND → GND (Pin 20)

And here is how the LED pins connect to the Pi:

  • GND → GND (Pin 34)
  • Yellow → GPIO 16 (BCM16, Pin 36)
  • Red → GPIO 20 (BCM20, Pin 38)

And this it how it looks together:
(On the left side you can see a pinout diagram which illustrates the locations of the available ports exposed by the breakout connectors of the Raspberry Pi 3 board)


Google also has a good tutorial how to configure your Android Things project in Android Studio.

Please follow the steps here:

Now you have a basic, compilable, working Android Things application, so we can start adding the necessary code.

In our main Activity we can setup our GPIO pins:

Then we initialize the pin (BCM21) for the Obstacle Avoidance Sensor as an input (we read from this pin):

Configure the LED pins (BCM16, BCM20) as an output (we write to those pins):

And our GpioCallback(), which does the magic: (blinks the led, when the obstacle is too close)

And don’t forget to close the resources if you don’t need them:

That’s it! And the end result:

I hope you enjoyed the Raspberry Pi hacking with Android.

The full project is available on Github:


Join Hacker Noon

Create your free account to unlock your custom reading experience.