Home Automation with Raspberry Pi and Arduino

We’ve talked about the relative merits of Arduino and Raspberry Pi before – they each have their strengths. They needn’t be an either or choice though – combine them to get the best of both worlds. Home automation is the perfect candidate for this. The home automation market is flooded with expensive consumer systems, incompatible with one another and costly to install. If you have a Raspberry Pi and an Arduino, you can basically achieve the same thing at a fraction of the price, assuming you’re willing to put in the time and the effort.

Heimcontrol.js by Willi Thiel is a Node.js app built to run on Raspberry Pi. Combined with an Arduino and some off-the-shelf remote control sockets, it makes controlling AC appliances easy. You can add temperature sensors, and even control your TV – but we’ll be keeping things basic today and extending the project in a later tutorial.

Here’s a breakdown of the project:

  • The Raspberry Pi will act as the brains and the gateway of operations – it will run a Node app, tied to a Mongo database, and serve the front-end interface to any web browser.
  • An Arduino, powered from the Pi, will interface between the electronics – radio control power switches, for now.

To do this, you will need:

  • Arduino and a Raspberry Pi
  • Some remote controlled sockets and controller (I used these)
  • Powered USB hub

RC sockets   Home Automation with Raspberry Pi and Arduino

Before we begin, here’s a demo video from the project creator himself.

Start Afresh

We’re going to use Raspian for this project, and I’d strongly suggest starting from a fresh install if you’ve previously performed other hacks and such. Download the latest Raspian image, copy it to your SD card, and be sure to expand the filesystem and enable SSH. The rest of this guide will assume you’ve done so, and are connecting over SSH using the default user.

If you haven’t done this before, this video explains the process of preparing your SD card in OS X:

And this one for Windows users:


The installation process is quite laborious, and derived from the instructions here. Unfortunately, these were outdated or not designed for Raspian, so I’ve adjusted them heavily below. The codes below can mostly be pasted in blocks – you needn’t paste one by one. Since we’re compiling a few things on the Pi itself, be warned that this entire process will take a long time. I would say go make yourself a cup of tea – but when I say a “long time”, I mean the best part of a day – so 178 cups would be more appropriate.

All commands should be typed into the Terminal, and you may need to press Enter at some points. These first few commands will update the system and install pre-requisites:

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install git-core git scons build-essential scons libpcre++-dev xulrunner-dev libboost-dev libboost-program-options-dev libboost-thread-dev libboost-filesystem-dev

Next we need to install Node:

sudo mkdir /opt/node
wget http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-arm-pi.tar.gz
tar xvzf node-v0.10.2-linux-arm-pi.tar.gz
sudo cp -r node-v0.10.2-linux-arm-pi/* /opt/node
sudo ln -s /opt/node/bin/node /usr/local/bin/node
sudo ln -s /opt/node/bin/npm /usr/local/bin/npm

Add a PATH variable to your profile so that the OS knows where Node is located. Use the nano text editor as follows:

sudo nano /etc/profile

Locate the line that says export PATH and replace it with:

export PATH

Hit Ctrl-X to exit, and Y to save.

You may need to log out and in again for the path changes to take effect, but you can test with the command:

which node

If you don’t get any output pointing to the Node binary, something went wrong.


The next job is to install Mongo. Mongo is a document-based No-SQL database increasingly used by web apps. Unfortunately, this will take forever to install as we have to compile it. While running the following commands you’ll get a lot of errors like:

{standard input}:13085: Warning: swp{b} use is deprecated for this architecture

Don’t worry about these. So, run these commands to install Mongo:

git clone git://github.com/RickP/mongopi.git
cd mongopi
sudo scons --prefix=/opt/mongo install 
scons -c

When that’s finished, we need a little more setup first to fix permission issues and make sure it’s running on startup.

sudo useradd mongodb
sudo mkdir /data/dbb
sudo chown $USER /data/db
cd /etc/init.d
sudo wget -O mongodb https://gist.github.com/ni-c/fd4df404bda6e87fb718/raw/36d45897cd943fbd6d071c096eb4b71b37d0fcbb/mongodb.sh
sudo chmod +x mongodb
sudo update-rc.d mongodb defaults

This last command will launch the Mongo server, and you’ll need to open a new Terminal to continue with the other commands. I’m not entirely confident of this step, so if anyone can correct this in the comments on how to have mongod launching automatically on start up, it would be much appreciated. For now, it works, just not elegantly.

sudo shutdown -r now

Finally, it’s time to install the Heimcontrol.js Node application.

cd ~pi
git clone git://github.com/ni-c/heimcontrol.js.git
cd heimcontrol.js
npm install

You can start running the app by typing

node heimcontrol.js

At this point, you should be able to access the control interface with http://localhost:8080 from the Pi, or replace localhost with the IP address if you’re accessing it from a different computer (and you could also set up port forwarding to access it from anywhere in the world), so I’d encourage you to poke around and check all is working with the database before attaching the Arduino.


Eventually I’d like a hardwired relay, but for now I’ll be using the safer option of radio-controlled switches.

I’ve used some reasonably cheap £20 sets from Maplin which come with 3 sockets, and opened up the remote control so I could interface directly with the 433 MHz chip inside. I found the instructions for this here.

rc controller hack   Home Automation with Raspberry Pi and Arduino

You can also purchase individual 433 MHz transmitters ready for use on eBay or from hobby electronics suppliers. All you need to is connect the VCC to 5 V on the Arduino, the GNDs, and a single control pin – remember which one you used. (Schematic by Willi Thiel)

arduino rcswitch 419x590   Home Automation with Raspberry Pi and Arduino

The plugin works by sending “tristate codes”, but these will vary by manufacturer. Refer to the RCSwitch documentation to find your exact codes. This wiki guide may help also.

Communication with the Arduino is done using a Node library called duino. Stop the Heimcontrol app if it’s running and install the Arduino bridge using the following command.

npm install duino

The Arduino must have this code uploaded – I suggest you copy and paste to install this from a different computer. It’s essentially a listener program that responds to serial commands from the Pi, but there’s nothing to stop you extending it with your own features.

rc settings   Home Automation with Raspberry Pi and Arduino

With the web app launched, go to the Settings menu -> Arduino.

heimcontrol menu settings   Home Automation with Raspberry Pi and Arduino

From there you can add a new item, choosing RCSwitch method, the pin of your transmitter, and the tristate address code. Remember to save, then head back to the main screen screen for your new button.

rcswitch setting   Home Automation with Raspberry Pi and Arduino


After many hours of debugging the code, I found single digit pin numbers weren’t working – make sure your transmitter is placed on pin 10 to be sure.

I also found that the Arduino plugin was hardcoded with incorrect final bits for the tristate codes my receivers needed. A little explanation first: tristate codes consist of 3 bytes of information. The first determines us the network number (1-4), and the second provides the transceiver address (again, 1-4, producing a maximum of 16 addressable sockets). The final byte consists of two bits of padding, plus 2 bits for on/off. Unfortunately, the final byte is hardcoded in to the Arduino plugin – and in my case, the on/off code was incorrect.

I had to manually edit the plugins/arduino/index.js to use the correct codes. If you’re using the same remote control sockets as I am, change lines 80 onwards to:

// Send RC code
if (item.value) {
  return that.pins[item.pin].triState(item.code + "FFFF");//change from FF0F
} else {
  return that.pins[item.pin].triState(item.code + "FFF0");//change from FF00

Here’s a demo video of everything working:

I’m going to leave it here at this point, but sensor readings and IR remotes are also supported. I’ll probably revisit these at a later date with some more enhancements. If this has all been a little too complex for you, perhaps check out my Arduino internet control project.

Image Credits: Door Bell Via Flickr

The comments were closed because the article is more than 180 days old.

If you have any questions related to what's mentioned in the article or need help with any computer issue, ask it on MakeUseOf Answers—We and our community will be more than happy to help.


Will Anderson

I made something quite similar to this but using purely an arduino (controlled from an iPhone). If you are looking at ways to further the system, I’ve also used mine to turn on and off the main lights in my house. Check it out if you fancy:


You can install Ubuntu’s MongoDB 2.4.6 armhf packages on a current Debian Sid installation and skip compiling:




You mentioned “I had to manually edit the plugins/arduino/index.js to use the correct codes”

How do i edit this ?



James B

in the project directory, type:

nano plugins/arduino/index.js

and edit the lines mentioned.

Press “CTRL-X , y” , to save.


Many Thanks

All working now



Thanks for sharing the great project.

I have the remote wired to my arduino as you have in your picture. I am managing to get the led to light when turning the pin on but it is not turning on the socket. Could you please guide me as to the sketch I need to use? Do I need to find the tristate code if I am using the remote wired?

Sorry for being such a Noob!


James B

Hi Ash – are you using the same plugs and remote? If not, it’s a bit hard to help with your exact case. If you are, check the addresses on the plugs themselves are inline with the codes you’re sending; also, try writing a basic test case for the plugs without any additional logic in there or Raspberry Pi stuff.

The remote needs to be wired to the arduino, yes – but check using the basic on/off code first without delving into tri-state stuff thats needed for the pi.

Also, did you make the minor edit needed to the arduino plugin JS file? It’s a small change, but critical to making it work with these particular sockets.


Hi James

Thanks for the quick reply! I have the exact same plugs and remote and have set both the remote and receiver to setting one. I have wired three wires as per tutorial: 1 to +, 1 to -, one to DOUT.

I am running arduino on PC first before moving to use on Raspberry Pi.
I am running arduino from PC USB port and have tried running the following sketch:

Example for different sending methods


Need help? http://forum.ardumote.com


RCSwitch mySwitch = RCSwitch();

void setup() {


// Transmitter is connected to Arduino Pin #10

// Optional set pulse length.
// mySwitch.setPulseLength(320);

// Optional set protocol (default is 1, will work for most outlets)
// mySwitch.setProtocol(2);

// Optional set number of transmission repetitions.
// mySwitch.setRepeatTransmit(15);


void loop() {

/* See Example: TypeA_WithDIPSwitches */
mySwitch.switchOn(“11111″, “00010”);
mySwitch.switchOn(“11111″, “00010”);

/* Same switch as above, but using decimal code */
mySwitch.send(5393, 24);
mySwitch.send(5396, 24);

/* Same switch as above, but using binary code */

/* Same switch as above, but tri-state code */


This lights up the LED on and off as per the script but unfortunately no change to receiver.
I tried to look for the arduino plugin js file but couldn’t see it in my arduino folders.

Could you guide me further please on where this file is, what exactly I need to alter in the file and if the above script needs any changes?

Many thanks once again, I really appreciate it.

James B

Sorry for the late reply Ash, didnt get a notification of this.

First off, try removing the + into the remote. I don’t know why this works, but it seems the signal level is strong enough without it. That did it for me.

The example code is quite different to what I test with – try this instead. The code assumes pin 10 is transmit, and your plugs are set on group 3:


RCSwitch mySwitch = RCSwitch();

void setup() {
mySwitch.enableTransmit(10); // Using Pin #10


void loop() {
mySwitch.switchOn(3, 1);
mySwitch.switchOff(3, 1); // Switch 1st socket from 3 group off


(yup, thats is)


Hello there and thanx a lot for sharing all this information. I’m currently working on the same home automation idea but software will be my implementation on java using a raspberry pi/arduino. I wanted to ask you if you believe for us owners of pi if it’s worth to use only a pi and not an arduino. I think there are some modules you can find for pi but i don’t have a clear image anyway. Also if you can recommend a module to wire it up on switch directly cause rf sockets are fun but i want a more advanced solution.Thanks in advance & keep up the good work!

Son Hoang

my friends;

You don’t need to waste money on arduino ( you have to add more power supply, jumper cables). I just finished my project: one simple RF transmitter module (3 pins) add to Raspberry PI and control over 1000 channels/devices in your home. Don’t need any additional hardware. You can search on google for rfCommandMe. I’m going to make a youtube video to show user how to implement HVAC control, light, fan, garage, sprinkler, Power Strip, or anything to do with electric current . Hope this will help your HOME AUTOMATION PROJECT.

James B

“no additional hardware” – except for all those 1000s of RF controlled plugs, presumably?


Been racking my brain trying to figure out why heimcontrol will not start.

$ node heimcontrol.js

info – socket.io started
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0

heimcontrol.js listening on port 8080

events.js:2817: Uncaught Error: Cannot open /dev/
events.js:2817: Uncaught Error: Cannot open /dev/

throw er; // Unhandled ‘error’ event
Error: Serialport not open.
at SerialPort.write (/home/pi/heimcontrol.js/node_modules/duino/node_modules/serialport/serialport.js:257:15)
at Board.sendClearingBytes (/home/pi/heimcontrol.js/node_modules/duino/lib/board.js:112:15)
at null._onTimeout (/home/pi/heimcontrol.js/node_modules/duino/lib/board.js:37:14)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Brian Easley


You need to have the usb cable plugged into your arduino when starting heimcontrol.


Love the project and been really studying it, however curious to know hw one would go about adding more transmitters if one had these over a larger emplacement and need to have say 4 transmitters to ensure coverage?


James B

Difficult as it is – one way would be to edit the Arduino side of things to send the same signal out over different pins. I don’t know enough about electronics to know if you could just split one signal to different chips with some kind of amplifier though.

Martin @ Hayling Islane

Been messing about with the plugs from maplin and just an arduino just using delay(delay time) to control plugs


delay(n *60 * 1000) // Hours delay from midnight
mySwitch.switchOn(4, 2) // Switch Desk lamp on
mySwitch.switchOn(4, 4) // Switch Floor lamps on (6)
delay(on time)
mySwitch.switchOff(4, 2)
mySwitch.switchOff(4, 4)
delay(off time)
mySwitch.switchOn(4,4) // on all night till midnight
delay(on time till midnight)
mySwitch.switchOff(4, 4)

Loop ..

Basic timer just has to be started at midnight …

Ravi Thapliyal


i was wondering that can we run a raspberry-pi or arduino for 24 hours X 365 days?
is it that durable?
can we do the home automation stuff with PLC system?

James B

I don’t know what PLC is, but there should be no issues with running Pi/Arduino on 24/7. Unless you’re mining bit coins on the Pi or something, it won’t burn out.


Hello. I was wonderring if this works without an Arduino. I saw that there is something like rcswitch-pi to send via the 433 Mhz transmitter directly. Why do I need the Arduino?

Thank you for the great guid! :)

James B

Interesting; but if you want to use this particular Node.js app, you will need the Arduino until it integrates with the other library you mentioned. You could write your own of course, and only use the rcswitch-pi thing.


Hi, I’m really interested in doing this, is this all I need?

Arduino and a Raspberry Pi
Some remote controlled sockets and controller (I used these)
Powered USB hub

Do I need a Soldering Iron? A breadboard? Jumper wires?


James B

You will need a soldering iron, yes, as well as jumper wires and a breadboard for testing. Sorry, I assumed those would be in your inventory if you’re taking on this kind of project.


Thanks for replying :) I already have a raspberry pi which I’ve used for other projects. I don’t have a soldering iron because I’ve never had a need for one.

This project is what I need, I have no central heating, so electric heaters are all I have. Living in Norway, you want your house to be warm when you get home without leaving the heating on full all day. If I can get this all working, I will defiantly add temp sensors later!

James B

Technically speaking, yes, but it looks like it’s designed to fit on a Raspberry Pi rather than the Arduino, in which case the RPi software part of this project isn’t compatible. There’s a similar RC-Switch library for RPi, I believe, it’s just that it would be up to you to integrate it with the other software components.


OK, thanks for the reply. I’ll do some digging and see what I find .



think this a good project, going through the steps now and hit a problem with ” fix permission issues and make sure it’s running on startup”

I get the following error

pi@raspberrypi /etc/init.d $ sudo update-rc.d mongodb defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script ‘mathkernel’ missing LSB tags and overrides

any ideas?

James B

Sounds very random – did you start from a clean install?


I got the same error, first i used the old instructions, then i found this page and restarted from a clean install. But with no luck,…

pi@raspberrypi /etc/init.d $ sudo chmod +x mongodb
pi@raspberrypi /etc/init.d $ sudo update-rc.d mongodb defaults
update-rc.d: using dependency based boot sequencing
insserv: warning: script ‘mathkernel’ missing LSB tags and overrides
pi@raspberrypi /etc/init.d $ mongod
-bash: mongod: command not found

Both times i was using 2014-01-07-wheezy-raspbian, maybe I should use another build?


I’m also getting this error. Any help?

James B

Check out the comment from George, below.


Does anyone have a prebuilt image of this hacking working
That’s all I get is error after error also can you confirm what the layout of codes to control the plugs are

I have been reading that the first four digits are which band the plugs are on
i.e FFF0
The next four are which plug the pi is taking to
i.e FFFF
The next two are not used
i.e FF
The next two are the on or off bits
i.e F0 or FF
The last bit is not used

So the complete code would be as below


James B

Did you follow through the complete article? I detail a bug concerning tristate codes right at the end, which might be relevant in your case.

Otherwise, confirming tristate codes is easier when you’re just dealing with the Arduino – just use the sample RCSwitch code to experiment with different tri-states.

Also, you’ve mentioned you’re getting errors: post them so we can see. Not sure if it was a typo, but you wouldn’t put “S” in a tri-state code at all, since it isn’t a hex value.


I had the same issue, it seems to be a fairly common issue:. http://www.raspberrypi.org/forum/viewtopic.php?f=94&t=62118

just add following to mathkernel:

# start/stop mathematica daemon.

# Provides: mathematica
# Required-Start: $network $local_fs
# Required-Stop: $network $local_fs
# Default-Start: S 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Mathematica remote services
# Description: Allows remote use of Mathematica.


yes, clean install but my linux skills are alittle noob but I going over it again, might have fluffed the path part :)

also might be good to add that when editing in nano, save by pressing ctrl-x, y for yes and Enter to accept name ( told you i was noob)

will keep you posted but again thanks for you help and this is a very cool project

James B

I wonder – are you using the RPi “noobs” boot image? The one where you get to choose which OS to start from? Might be worth trying to re-image the SD card as plain Raspian without the added noobs tool.


Please help how to i run this heimcontrol.js on startup having to time in the command every time i reboot is not fun


Hey could you take a look at this. I have tried fresh installs of this project 7 times. I think I’m really close but I keep getting this error and can not figure out how I am to resolve it. Any suggestions would be appreciated.

I got my mongodb up and running and
This is the error I get after i run node heimcontrol.js:

info – socket.io started
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
heimcontrol.js listening on port 8080

throw er; // Unhandled ‘error’ event
Error: Cannot open /dev/



Great project!

However, I am just in the process of installing and setting up everything.

I had to add sudo command to the following commands, did I miss something from your instructions or is your instructions missing this information?


wget http://nodejs.org/dist/v0.10.2/node-v0.10.2-linux-arm-pi.tar.gz
tar xvzf node-v0.10.2-linux-arm-pi.tar.gz

david gardner

back again and still no luck, not using NOOB tool, im using 2014-01-07-wheezy-raspbian which i run update on. mongo is failing to auto run, status says not started and if i try and start it i get…..

pi@raspberrypi /etc/init.d $ sudo ./mongodb start
[FAIL] Starting database: mongodb failed!

anyone have a prebuilt image of their SD card or can you get in added to the repository seeing as its a popular DB app? i’ve run over this about 6 times now and its getting a little tiresome and i hear this pi is going to end up in the microwave :P



is /data/db directory in the root


Nice project, I am thinking of doing similar myself.
I don’t see why you should use 2 differtent computers for this.
IMO Raspberry PI should be more than powerfull enough to do the task, and it also has I/O ports, so what is the rationale behind this ?


@david gardner..Try deleting lock-file from mongodb?

Rui S

Hi @Bene,
I’m having the same problem as @david gardner.

Could you explain how to delete the lock-file from mongodb?
I’ve followed every tutorial on the web to fix that error:
pi@raspberrypi /etc/init.d $ sudo ./mongodb start
[FAIL] Starting database: mongodb failed!

and I can’t find an answer.
Please Do you have time to post the commands?
I would really appreciate.


Hi, I solved it with:
• Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock
• reboot ^^

Rui S

thanks for the quick reply!
I’ve tried that command before but It always says “No such file or directory”.
Here’s a print showing:
Can I find that file in another place?
Do you know another way to solve this problem?


P.S. Sorry for the re-post of this question…

Rui S

thanks for the quick reply!
I’ve tried that command before but It always says “No such file or directory”.
Here’s a print showing:

Can I find that file in another place?
Do you know another way to solve this problem?

Thanks again


Im also having problems and really keen to get this sorted. I am no expert infact a complete noob but this bit doesnt seem correct
sudo mkdir /data/dbb

the dir wont create if i create each seperatly then they create.
sudo mkdir /data/dbb
also note the next line “db” is referenced however in the previous line its “dbb” am I being stupid?
sudo chown $USER /data/db

Finally does anyone have a link to the none NOOBS raspian image as I thought i would give that a go.
Thanks in advance

James B

You’re right – that should be db, not dbb.


I have now found a raspbian image without NOOBS and am starting the install over again. i am itching to try all this out, think its gonna be a late night for me

david gardner

hi guys

will give it a go over the weekend, works been busy so all projects were put on ice :)

will keep you posted



I decided to restart the whole processes after inadvertently missing a step out, however I get an error when i try to do the following

sudo mkdir /data/db
error mkdir: cannot create directory ‘/data/db'; No such file or directory


Ok we are nearly there (i’m getting excited). The only thing I cannot figure out is how to connect the pi to the Arduino, is it TXD from the Pi to RXD on the arduino , hopefully not as thats not workng for me. I presumed the Pi would send commands over the serial line to the arduino which would then proccess them. However I am still getting the error
Error cannot open /dev/

Any advice on connecting Pi to arduino (which pin to which pin) would be appriciated

James B

You don’t connect pins – you plug it into the USB port on the Pi. Sorry for not making that obvious, the thought of connecting pins hadn’t occurred to me!