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.