[TUT] How to use another power supply with your IoT device

When creating IoT systems, you’ll often want to use devices that run off another voltage of power supply than the normal 3.3v or 5v that your Raspberry Pi or other development board can give out. In this instance you need to use a relay. This post will walk through the use relay circuit boards and show a coded example with AndroidThings.

What we’ll do:

– Talk about different toltages
– Look into and describe a relay
– Relay circuit labelling
– Relays & GPIO
– Coded example

Let’s get started..

Raspberry Pi and other development boards like the iMX7D or Intel Edison run themselves on 5 volts, they also downscale the voltage for their GPIO pins to 3.3v. However if you want to use some other hardware with your board such as a 9V DC Motor, a 240V (UK) or 220V (US) Fan, an Air Conditioning Unit, a Mechanical Garage Door, a Blender etc etc. They will likely need a higher voltage to be powered.

If your peripheral device is externally powered but is a smart device with some type of API to control it. They you’ll likely not need a relay circuit. However if your device is “dumb” and to control it you need to control power to it, i.e. turning it off and on again, then this is where a relay board comes in.

what a relay is?

an electrical device, typically incorporating an electromagnet, which is activated by a current or signal in one circuit to open or close another circuit.

A relay circuit allows you to control a larger voltage with a smaller voltage. The two circuits are not physically connected, the electromagnetic is connected and controlled by the Raspberry Pi and the switch is connected to the higher voltage.

Two, two way relays on a relay board look like this:

When the electromagnet is off, once circuit is closed. When the electromagnet is turned on the closed circuit is opened and the opened circuit is closed. Each two way relay can control two circuits.

COM, in the middle is for the common connection – this is where you connect your usual power supply.
NO, is for Normally Open. Normally open means when this relay is “on” then this circuit is connected.
NC, is for Normally Closed. Normally Closed means when this relay is “off” then this circuit is connected.

At the other end, is where you connect your Raspberry Pi or other board.

In this example:
IN1, is the GPIO pin for the first two way relay.
IN2, is the GPIO pin for the second two way relay.

Changing the GPIO pin to HIGH will close the normally open circuit and changing the GPIO to LOW will close (or leave closed) the normally closed circuit.

I’ve been using this relay circuit from Amazon 5 V DC 2 Channel Low Level Trigger Power Relay Module JQC 3FF Adapter S Z 10 A 250VAC 15 A 125Vac as I wanted to control two peripherial devices at once, and this board has two GPIO inputs.

As an example with AndroidThings of connecting the NO circuit:

            Gpio gpio = new PeripheralManagerService().openGpio(gpioPin);
            gpio.setActiveType(Gpio.ACTIVE_HIGH);
            gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);

            gpio.setValue(true); // close Normally Open circuit

With AndroidThings I don’t feel `gpio.setValue(true)` really explains what is going on with the Relay. Therefore I’ve created this wrapper class to make working with relays a bit more readable:

public class Relay {

    public static Relay newInstance(String gpioPin) {
        PeripheralManagerService service = new PeripheralManagerService();
        return newInstance(service, gpioPin);
    }

    public static Relay newInstance(PeripheralManagerService service, String gpioPin) {
        try {
            Gpio gpio = service.openGpio(gpioPin);
            gpio.setActiveType(Gpio.ACTIVE_HIGH);
            gpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
            return new Relay(gpio);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    private final Gpio gpio;

    Relay(Gpio gpio) {
        this.gpio = gpio;
    }

    public void connectTo(Position position) {
        try {
            switch (position) {
                case NORMALLY_OPEN:
                    gpio.setValue(true);
                    break;
                case NORMALLY_CLOSED:
                    gpio.setValue(false);
                    break;
                default:
                    throw new IllegalStateException("Developer Error, unhandled enum " + position);
            }
        } catch (IOException e) {
            throw new IllegalStateException("Cannot switch to " + position, e);
        }
    }

    public void close() {
        try {
            gpio.close();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    public enum Position {
        NORMALLY_OPEN, NORMALLY_CLOSED
    }

}

Now you can control a relay like this:

Relay relay = Relay.newInstance(gpioPin);
relay.connectTo(Relay.Position.NORMALLY_CLOSED);

Thats it!

Once you know to use a relay. The relay board itself is straight forward, especially once you understand the synonyms normally open and normally closed. When doing IoT projects you’ll often want to use a different voltage for devices running off the mains/wall power supply and now you can!

The relay I’ve been using, off Amazon.

The AndroidThings Relay Driver Library is available here.