Skip navigation

For those of you who have been following along with my BeagleBone Black and Drupal series,  you could see this post coming. In my first installment I showed you how to build a website on the BeagleBone Black using the Drupal CMS, and followed it up with Controlling the BeagleBone Black's GPIO Pins with Drupal. This tutorial is going to take things a step further and show you how to take that knowledge, and use it to trigger some relays to turn mains powered electronics on and off. While the method I am going to use would not be ideal for conrtolling an entire home, it works quite well for controlling things like props in a haunted house and other low-amperage things like small fans, lights, and such. Just to make things easier, I am going to recap a good bit of my previous tutorial here. You will still need to go through the Drupal setup steps in the first tutorial though.




Prerequisites: What you need to know to be able to complete this project.

  • Understand how Drupal-based websites work.
  • Understand how PHP can be used to parse commands to the web server's terminal.
  • Understand the basics of how Transistors work, and why they should be used for applications like this.
  • Understand how the BeagleBone Black accesses its GPIO Pins.


Hardware Required: What hardware is needed to complete this project.

  • BeagleBone Black (Rev C recommended) BeagleBone Black (Rev C recommended) running the latest Debian image.
  • 5mm Green LED
  • 1x 460Ω 1/4w Resistor
  • 3x 3.3kΩ 1/4w Resistors
  • 3x 100Ω 1/4w Resistors
  • 3x 2n3904 General Purpose NPN Transistors
  • Female to Male Jumper Wires
  • Male to Male Jumper Wires
  • 5V Power Source for BeagleBone Black



Getting started: Building the discrete circuit and connecting to the Beaglebone Black.


Just like last time, we need to set up a transistor that will be used to supply the trigger voltage to the relay coils. The transistor circuit is needed because the BeagleBone Black is only able to supply about 8mAh to each of its GPIO pins and connecting something that draws more than 8mAh can damage the board's ARM chip. We can turn on a general-use PNP transistor with just a few mAh which will let us power the LED externally, and prevent any damage to the board. This is also handy for controlling relays and other components that can be triggered via the GPIO pins. Follow the diagram below to build the simple circuit. Instead of setting up the circuit to illuminate a LED, this time we will configure it to trigger the relay. We will be using a cheap 8-channel relay board to connecto to our circuit, so we will need a common ground, as well as two signal lines to the first two relays.




With the breadboard populated with our transistor circuits and connected to our BeagleBone Black, lets move onto the software side of things. We will come back and connect the relay board later in the tutorial.



Setting up the GPIO Pins in Debian: Using the terminal to configure specific GPIO pins in Debian.


To test the circuit we need to access our BeagleBone Black via SSH. I covered how to do this in the BeagleBone Black RSS News Reader tutorial, but will post it here as well.


To access the BeagleBone Black via SSH I will be using a terminal for Windows called Putty, as well as an add-on called MTPutty, which allows me to run multiple putty instances from within a single tabbed interface. Both Putty and MTPutty are available for free, and take just a few moments to download and install. Chances are, if you are reading this, you already know how to use Putty. If you are using a Mac you can simply use the built-in Terminal to access your Black Via SSH. To connect to your Black, you need to enter the following credentials.


With these settings configured, click ok and then double click the BeagleBone Black name in the server list. This will open up a new terminal tab and you will be prompted to enter a username. Unless you have modified the root user, you should enter root as the user name.




BeagleBone has made it easy here as there is no password and after entering the username, the connection will finish, and you will be placed in your "home" directory.



Now that you are SSHed into the BeagleBone Black we will need to prepare GPIO_67 (P8:08), GPIO_68 (P8:10), and GPIO_44 (P8:12) . The pinout diagram can be seen below.




Before we get started with testing, lets take a moment and talk about how the BeagleBone Black's GPIO pins are addressed by the Linux-based Debian operating system. The BeagleBone Black addresses its GPIO Pins by reading the contents of files which are located in the sys/class/gpio directory in the board's operating system. The most basic functionality is to write the pin high or low, and is what we will be doing in this tutorial.


To use a GPIO Pin you first need to tell Debian that you are going to use a particular pin in a specific GPIO header. The following code uses GPIO 8.8 as an example, but any GPIO Pin can be accessed by creating these files. Enter the following commands into the BeagleBone Black's terminal.


The first thing we need to do is create a few new directories in the /sys/class/gpio/sys/class/gpio directory.

echo 67 > /sys/class/gpio/export
echo 68 > /sys/class/gpio/export
echo 44 > /sys/class/gpio/export


At this point you will need to let the BeagleBone Black know whether or not the specified GPIO pins are input or output.

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


With this command, the BeagleBone Black no longer supplies 2.3V to the header and it is considered to be turned off. In order for us to set the GPIO Pins high or low we need to set their value to  either 1 or 0. We can do that with the following commands where the gpio# variable is set to the corresponding pin #. An example for gpio_67 is listed below.


Set the GPIO Pin High

echo 1 > /sys/class/gpio/gpio67/value


Set the GPIO Pin Low

echo 0 > /sys/class/gpio/gpio67/value


Remove control of the GPIO Pin when finished.

echo 67 > /sys/class/gpio/unexport


You will also need to chown and chmod the directory for each GPIO pin used. This can be done by entering the following commands.

chown -R www-data:www:data /sys/class/gpio/gpio67/value
chown -R www-data:www:data /sys/class/gpio/gpio68/value
chown -R www-data:www:data /sys/class/gpio/gpio44/value
chmod 777 /sys/class/gpio/gpio67/value
chmod 777 /sys/class/gpio/gpio68/value
chmod 777 /sys/class/gpio/gpio44/value


Testing The Circuit: Using the terminal to trigger the GPIO Pin.


With those basics out of our way, we can test out setup by turning the LED connected to GPIO_67 on and off. Connect a 9-volt battery to our breadboard and head back to the terminal window and follow the steps below to turn the LED on and off via the command line.




With everything connected, we should be able to use the command line to set GPIO Pin 67 high, which should in turn activate the transistor and allow the LED to light up.

Set the GPIO Pin High

echo 1 > /sys/class/gpio/gpio67/value


If everything was followed correctly, you will see the LED light up




Now set the GPIO Pin to low to turn off the circuit.

Set the GPIO Pin Low

echo 0 > /sys/class/gpio/gpio67/value



Setting up the Relay Board: Connecting the Relay Board to the BeagleBone Black.


If those commands worked, then everything is set up as expected. We can now connect the 8-channel relay board and test to see if it is working correctly. I'm using a cheap relay board I bought on Ebay a while back, but Newark and MCM Electronics both have several you can purchase. My board is quite simple and only requires a VCC input of 5V and a GND connection. Each relay has a trigger pin broken out, and these are where you will attach the GPIO pins via the transistor circuits we built earlier. Since my board only requires 5V to trigger the relay coils, I am going to power it from the BeagleBone Black. Generally this is frowned upon, but since I am only using two relays at once I am letting it slide. If you try triggering more than 2 relays at once, they could try to pull more current from the BeagleBone Black than it can supply and the relays will not trigger and the board could be damaged. If you plan on triggering all 8-relays I would suggest powering it with an external 5V 2amp power source. You could also use the 9-volt battery and a L7805 +5V Linear Voltage Regulator to trim 9-12V down to 5V. Follow the instructions below. I could not find my resistor assortment, and had to risk connecting the relays directly to the BeagleBone Black since they would only be used for the duration of this tutorial.




  • For this tutorial,  connect the two relays to the BeagleBone Black by connecting the VDD_5V (P9:05) pin to the relay boards VCC connection header.
  • Connect the BeagleBone Black's Ground DGND (P9:01) to the Relay Boards GND pin via the transistor circuit.


As I mentioned above, I could not find my resistors to complete the transistor circuits, so follow the directions below to connect the relays to the transistor circuits.


  • Connect the transistor that is connected to the  BeagleBone Black GPIO_68 (P8:10) pin to the relay 1 pin on the relay board.
  • Connect the transistor that is connected to the  BeagleBone Black GPIO_44 (P8:12) pin to the relay 2 pin on the relay board.


Now with those relays connected, its time to test that they work. Click back into the terminal and enter the following commands. If all is right, both relays should click on and off. Note that some relay boards are set as normally closed, and will appear to function the opposite you think they should. You will have to reverse the on and off commands for the relays to function correctly. This is as easy as swapping the 0 and 1 around in the code.


Turn Relay 1 on:

echo 0 > /sys/class/gpio/gpio67/value


Turn Relay 1 off:

echo 1 > /sys/class/gpio/gpio67/value


Turn Relay 2 on:

echo 0 > /sys/class/gpio/gpio67/value


Turn Relay 2 off:

echo 1 > /sys/class/gpio/gpio67/value


If they clicked on and off, then proceed to the next step. If they did not then repeat the process to see if you missed something.



Configuring the Drupal website: Adding the additional control buttons to the website.

With all of the hardware working its now time to configure the website that will let us control each relay as well as the LED remotely. Instead of writing 2000 more words and making this tutorial impossibly long, I have recorded another video to guide you through the configuration process. Check it out below and remember to subscribe to the channel if you like what I am trying to do!



When you get to the page creation phase, you can find the needed code examples pasted below.


LED Code:


Relay 1 Code:


Relay 2 Code:


With the website now configured, you should have a working remote automation system. You can use this to control a number of things such as lamps, irrigation systems, and even props in things like haunted houses, or film sets. I first designed this system to control a simple fog machine for my home on Halloween via my smartphone, but sadly the fog machine burnt out a week before the project was complete. So as you can see in the video below, I am using it to control a light bulb on my office desk. I plan on streamlining the system before next Halloween, and writing a few tutorials here on automation with the BeagleBone Black using triggers such as an Android app, motion sensors, and even pressure plates. If there is enough demand, I would consider turning it into a monthly series so leave me a comment if this is something you might like to see.


I have been working on an idea for my project. I am using my BBB to get the weather forecast and to let me know if I need to take an umbrella when I go out. (To be honest I don't use an umbrella but this is is for a "proof of concept" idea).


I am using python here to collect the forecast from Weather Underground, the api page is very helpful, and when I have the forecast as a json document I can extract the data I am looking for and do stuff with it. Currently I am displaying the weather forecast form my local area for the day that the program is being run and the next day. I have coded the place that I want the weather for into the string that makes up the URL, as my project is going to be setup per user I think that this is OK. See code below:


# program to grab the weather forecast from weather underground
# author Mark Venn
# based on code from "Getting Started with Raspberry Pi"
# currently collects the forecast for current day and next day. Lights an LED if the chances of rain are over 60%

# imports
import requests
import Adafruit_BBIO.GPIO as GPIO
import time

# configure GPIO pins


# wunderground api key

key = 'xxxxxxxxxxxxxxxx'

# build string using hard coded web address & api key variable
ApiUrl = '' + key + '/geolookup/forecast/q/UK/Bembridge.json'

while True:

    # get the weather forecast from wunderground and store in variable
    r = requests.get(ApiUrl)

    # take response and parse it into python dictionary object
    forecast = r.json

    # extract relevant data.
    # use variable to collect day of the week
    day = forecast['forecast']['txt_forecast']['forecastday'][0]['title']
    # display next day
    nextDay = forecast['forecast']['txt_forecast']['forecastday'][2]['title']

    # chance of rain
    rain = forecast['forecast']['txt_forecast']['forecastday'][0]['pop']
    rain = int(rain)

    # display current day
    print day

    #directly display the required info
    print forecast['forecast']['txt_forecast']['forecastday'][0]['fcttext']

    print nextDay
    # print rain

    #directly display the required info
    print forecast['forecast']['txt_forecast']['forecastday'][2]['fcttext']

    if rain >= 60:


# prints entire json document
#print forecast

The data I need for the forecast is held in fcttext in 2 key / value pairs and this is displayed.

root@beaglebone:~# python


Mostly cloudy. Lows overnight in the low 40s.


Cloudy and becoming windy. Periods of light rain early. Turning colder. High 46F. Winds N at 20 to 30 mph. Chance of rain 40%.


Mostly cloudy. Lows overnight in the low 40s.


Cloudy and becoming windy. Periods of light rain early. Turning colder. High 46F. Winds N at 20 to 30 mph. Chance of rain 40%.


Cloudy. Lows overnight in the low 40s.


Light rain early. Then becoming windy in the afternoon. Turning colder. High 47F. Winds N at 20 to 30 mph. Chance of rain 40%.


I am displaying this in the console at the moment, I might use some kind of GUI at a later date; my son odion is often telling me that I should put stuff into GUIs but A console is good enough for me for now

The code to get the forecast could be run on any machine that can run python 2.7 but now we come to the reason for running this on the BBB. There is a single LED connected to P8-8 and when the value of the precipitation chance in percentage is over  60% this LED lights up. The whole check is done every 3 minutes at the moment but I will be playing with the timings later. It is likely that I can get away with every 6 hours or so but I will see.


I have taken the idea for this from the "Getting Started with Raspberry Pi" book and made a few changes as well as converting it to the BBB; I will probably set this up on the Pi next and see if there are any differences in the way it works.

The Weather Underground api looks very wide ranging and I will see what else I can get out of it, I think that as we in the UK head into winter something that pulls the expected maximum and minimum temperatures will be useful. I have found where this info is held in the json document:


"high": {




"low": {




Will have to play around with this info and see how I can make use of it.


I will be using my BB_VIEW display to see how I can display the info, not sure what or how yet, this will be in another blog. I am going to be writing about my experiences with my new Pi B+ so check out my blogs on there.

Bye for now, off to check if my LED is alight yet!


Beaglebone Black

BB View 4_3 LCD Display