Month: November 2014

Beagle Bone Black, Relays and Bonescript.

One of the common things to do with an embedded system is to control a high voltage device with the low voltage signal from a microprocessor. The easiest way to do this is with a relay either of the electronic or mechanical type.

In my application I want to switch a 12 volt vacuum pump on and off and open a 12 volt bleed valve to cycle pressure on and off. When the system boots or there is no 5 volt power I want the pump to be off and the bleed valve to be closed. This means I will need to use the normally open connections of the relay to control the pump and  valve. This poses a couple problems for us, which I will get in to later.

First, lets hook the relay board (Sain Smart Relay Specification) to the BBB. I chose to use P8_11 and P8_12, with 12 controlling relay 1 and 11 controlling relay 2. So make these connections:

P8_11 –> IN2

P8_12 –> IN1

P9_01 –> Ground

P9_07 –> VCC

If we wire the relays directly to the BBB we have a problem.
If we wire the relays directly to the BBB we have a problem.

When we power up the system the first problem shows up. We hear the relays chatter and notice the indictor leds are dimly lit. The issue is that at power on the pin mode is not set and the P8_11 and P8_12 are floating at a value that is neither TTL high or low. This is a problem since we are controlling a pump we don’t want the relays to momentarily energize and send power to energize it.

With the pins on the BBB floating the relays are neither on or off[
With the pins on the BBB floating the relays are neither on or off.
We can exercise the relay with the following script. Just paste this in cloud9 and step through it.

var b = require('bonescript');

var relay1 = "P8_12";
var relay2 = "P8_11";
var c = 0;
//Set pinMode causes output to go to 0, which activates our relay!
//We really need the relay off when low.
b.pinMode(relay1 , b.OUTPUT);
b.pinMode(relay2 , b.OUTPUT);

//Just alternate the relays on/off every second.
setInterval(function(){
b.digitalWrite(relay1 , c % 2 == 0 ? b.HIGH : b.LOW);
b.digitalWrite(relay2 , c % 2 == 0 ? b.HIGH : b.LOW);
c++;
}, 1000);

When we set the pin mode we see the relay activating again. Pin mode sets the pin to TTL low initially, which in this case activates our relay. It is clear we need to add a small circuit between the BBB and the relay board to get this to work the way we want it to. We will add a 74LS04  hex inverter and  a pair of 500 ohm pull down resistors. The circuit is shown here (GND on pin 7, VCC pin 14):

We will add pull down resistors and a 74LS04
We will add pull down resistors and a 74LS04

So go ahead and shutdown the BBB and wire in the 74LS04.  Unused inputs on the 74LS04 should be grounded as per the manufacturers recommendation.

With the inverter and pull downs installed the relays behave as they should.
With the inverter and pull downs installed the relays behave as they should.

When we power up the BBB we will see the problem of the floating TTL is solved via the pull down resistors, and the inverter is translating that low to a high for the relay input. This keeps the relay off. Now we can run the test script above again. If you step through it you will see that setting the pin mode no longer causes the relay to activate. Writing a low to P8_11 and P8_12 will cause the relays to deactivate, a high to these pins will activate the relays. This meets our design goal.

So we see with minimal circuitry we can easily interface the BBB to a relays. The resistors and 74LS04 can be found in any electronics surplus store for pennies, or purchased form Mouser, Digi-Key or your favorite supplier.

Next time I will discuss how to control this relay setup via a web ui with bonescript.