Relays

Relays are very useful components; they allow us to automate pretty much any device:

A relay is essentially a digital switch. This means that it works like a switch, but instead of a physical button, it uses a digital signal.

Relays have two sets of pins. One is connected to our development board, including Vcc, Ground, and a signal pin -which is the GPIO-. The other part is the one we automate -the switch- which consists of an input (typically the middle pin) and two outputs, one that is connected when the signal is low (typically labelled NC) and one that is connected when the signal is high. Quite often only two are used, but the third one is handy in certain cases.

Relays allow us to automate pretty much anything.

Relays can work with AC as well as with DC, so you can use them to control something such as an electric popcorn maker (which uses AC) as well as a simple DC motor.

A very important concept for a relay is that it separates the signal from the actual current that powers the device. It enforces the fact that we should not use GPIO to power anything.

Make sure your relays are 3.3v, since that is the value of a GPIO on the Android Things developer kit. Note that most Arduino boards use 5v as GPIO value, so many relays you find online will not work with Android Things straight away. You will need to make a small circuit to make them work, but you don't want to need to do this.

Make sure your relays are activated at 3.3v.

The code to handle a relay module is the same as for lighting up the LED: just a simple GPIO configured for output. The wiring, however, is a bit different, as you can see in the following diagram:

To be able to reuse the same code as for blinking the red LED, you just need to take the Rainbow HAT off and connect the relay to the pin 31 of the board (which is BCM6 or GPIO2_IO02, depending on your developer kit).

Note again that the current is coming from a main plug and the GPIO value is simply used as a signal to control the switch.

And, with that, you can blink a real bulb. We'll see a more interesting example with relays in the next section, when we talk about input.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.51.117