How to control relay module with BeagleBone Black

I’ve finally received my 5V relay module and started messing up with my BegleBone Black tiny Arm board won from Touk.pl at Confitura conference.

Idea is to build a flowers watering system calculating water amount from current temperature and weather forecast with watering statistics.

Here’s how it works:

So. I’ve first connected my relay module to power(P8_7) and ground(P8_1) connectors on BB and input to some GPIO pins (ex: P9_8, P9_10). My relay module has only 2 relays, but you can connect as many as you like, just use more GPIO pins.
Here you can get nice pinout image to check what pin is where:
http://letsmakerobots.com/files/userpics/u19048/B_3PinOut3.png

Next we have to enable our GPIOs so we can control them:

if [ ! -d /sys/class/gpio/gpio67 ]; then echo 67 > /sys/class/gpio/export; fi

You have to do this as root to have access. Remember that GPIO numbers are different from pin numbers. Check pinout if needed.

Next we can power up our GPIO, it should enable relay channel:

echo out > /sys/class/gpio/gpio68/direction

to disable:

echo in > /sys/class/gpio/gpio68/direction

That’s all. Now you can use your BeagleBone to power on/off almost any device you like. Here’s a full script used in the demo if you like to check it out:

#!/bin/bash
gpio1=67
gpio2=68

if [ ! -d /sys/class/gpio/$gpio1 ]; then echo $gpio1 > /sys/class/gpio/export; fi
if [ ! -d /sys/class/gpio/$gpio2 ]; then echo $gpio2 > /sys/class/gpio/export; fi

click() {
    state="`cat /sys/class/gpio/gpio$1/direction`"
    if [ $state = "in" ] ; then
        state='out'
    else
        state='in'
    fi

    echo $state > /sys/class/gpio/gpio$1/direction
}

while : ;do
    click $gpio1
    sleep .2
    click $gpio2
    sleep .4
done
About these ads

9 Responses to How to control relay module with BeagleBone Black

  1. Robert says:

    Hi, thanks for the post it helped me with some gpio work I’m doing with a relay. Just curious if you if there is a way to detect if something is actually connected to a relay port or not.

    • Marek Piechut says:

      Sorry, no idea. I’m just starting to play with this little one and have no previous hardware hacking experience. Probably you need some communication protocol with device that’s connected to port. Not sure if you can do this with relays.

  2. Frank says:

    Hi, there is a mistake in your beaglebone picture B_3PinOut3.png. The Portnames P8 and P9 should be changed …

  3. Marek Piechut says:

    Hmm, looks that you’re right. It’s named the other way in official documentation. Thanks.

  4. Bob Igo says:

    This is a confusing result. According to the BBB specifications, you can only provide 6-8mA per GPIO pin (different references say different things), and the type of relay you’re using allegedly needs between 15-20mA at 5V to trigger (not 6-8mA at 3.3V). Either the specs are wrong, or you’re dangerously over-driving your GPIO pins. Have you blown up any GPIO pins since you’ve been doing it this way?

    • Marek Piechut says:

      Hi. Nope. I had no problems, but played with it only few times.

      • Bob Igo says:

        I got a similar result with an 8-relay board, and so far I have no issues.

        If your board is “reverse logic” like mine, you trigger the relays by sending a 0V, not 3.3V-5V. If so, then the 15-20mA that the relays need to act probably comes from the board’s VCC input. I might be wrong about that, but it seems to fit the result I’m seeing.

  5. Peter Ly says:

    Is it possible to know where you purchase the relay?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: