Skip to main content

Room automation with Raspberry Pi and Arduino

One of the first things I did with Raspberry Pi was connecting relay board to it and than switch the light, which was connected to the relay, on and off using command line. This wasn’t very useful since every time I wanted to switch light, I had to first SSH to Raspberry and then use the command like “gpio -g write 17 1” to turn on the light. Another problem was that I could only switch two lights because I had to run cables from physical light switch to relays. If I wanted to switch the light that I have on the other side of the room, I would have to install cables through the room. Then I came up with the solution of using few Arduinos + nRF24L01 wireless modules and user friendly web app.

List of parts that I used to make this project:

The first problem I encountered was getting arduinos to communicate using nRF24L01 modules. I read on forums that arduino might not provide enough current through 3.3V for powering nRF24L01 modules and I had to solder 10uF capacitor to Vcc and ground pins on the module.

nRF24L01+ module with 10uF capacitor
nRF24L01+ module with 10uF capacitor

You can check how to connect nRF24L01 module to Arduino here. I used this library for nRF24L01+: RF24 library.

After I got nRF24L01 modules to work, I wrote the arduino program that sends either value 1 or 2 to other Arduino, whenever push button was pressed or released. For the arduino used as receiver I wrote another program that receives data and switches GPIO pin state according to received value. Later I put data in array, so I could control multiple arduinos.

Arduino transmitter – the one connected to raspberry. You only need one.

Arduino receiver – connected to relay, which is switching the device on/off. You can use more of them.

I had to get Raspbery Pi and Arduino-transmitter to comunicate, so I replaced push button with transistor and raspberry pi circuit. Therefore when I used command for changing the GPIO state on raspberry pi – “gpio -g write 17 1”, The current would flow through base of the NPN transistor and hence closing the circuit on the arduino side, so arduino reads different state on gpio pin, causing it to send data to other arduino which controls the light.

Here is my final diagram (both resistors are 10k):

Diagram
Diagram

Last step was to make web interface. First I wrote back-end in php for switching GPIO state of one pin on the Raspberry. It executed command to read current state of the GPIO pin and if the state was off, it executed same command as I used before to turn it on and vice versa. So whenever I reloaded the web page, the light turned on or off. I found some jQuerry mobile buttons on w3schools that I later used in my UI. I put gpio pins and names of devices in the array and used foreach loop so when I added another device – air conditioner, I only had to insert GPIO pin and name of the device in the array.

nRF24L01 module peeking from air conditioner (Arduino is inside)
nRF24L01 module peeking from air conditioner (Arduino is inside)

Web app code:

Video demo:

Here is the picture of the final transmitter side setup. I have two GPIO pins on Raspberry connected to relay next to it and two connected to arduino through transistor circuit. Oh, and Aruino is powered by Raspberry pi’s USB.

Transmitter side final configuration
Transmitter side final configuration

If you are doing this project yourself, make sure you have installed apache2, php and wiringPi on your Raspberry Pi:

11 thoughts to “Room automation with Raspberry Pi and Arduino”

  1. Hello, how you solve problem with your big light… when you turn wall switch off, you can turn on light by phone… You use wall switch for stairs?

    1. Hello, sorry for late answer. Yes, I use a 3 way switch for the big light and I also connected three wires to the relay. But in this case I can’t monitor whenever the big light is on or off.

  2. excuse me im pretty new to this rasberry pi stuff but want to control my bed room light with my phone basic stuff to you but i dont understand how to wire the device to my outlet i have a 3 way switch in my room so i think its a benifit i know a bit about electrical i can install 3 way switches so i know the black white and red wires in it i just dont know how to use them wires to connect to the rasberry pi to make it so i can control it anyway you can email me to explane how i would wire it all up in simple turms? my email is chrisrau66@outlook.com i would appreciate it if you could email me about this subject and explan stuff a little better to me thanks

    1. You need Raspberry Pi and relay module. Then, connect ground pin on Raspberry Pi to GDN pin on relay module, 5V to VCC and GPIO17 (or any other gpio pin) to IN1 as shown on this picture: http://i.imgur.com/KGFA1HU.jpg. You can find GPIO pinout for Raspberry pi here: http://i.imgur.com/sTrGZy4.jpg.

      For the lights, you would probably need longer wires to connect the ones in your outlet to relays. You need to connect red wire to middle pin on the relay and another two on pins next to it. I would advise you to use a multimeter to test conductivity between pins on the relay in both states (1 and 0).

      To switch relay on and off, install wiringPi (instructions in post above) on Raspberry Pi and use this command to set GPIO pin to output and change its state to 1 (3.3V out):
      gpio -g mode 17 out
      gpio -g write 17 1

      To turn it off, use:
      gpio -g write 17 0

      Commands can be executed in terminal on Raspbian (default OS on Raspbery Pi) or you can connect to Raspberry Pi remotely through SSH.
      To find IP address of raspberry pi type: “ifconfig” in terminal. You will also need this IP address to access it from your web browser.

      To control lights through website, you need to install apache2 and php on Raspbian, then navigate to /var/www using command “cd /var/www” and create and edit file index.php using command “nano index.php”. Paste code from this post under Web app code and save the file.
      Then the website should be accessible using any device on your LAN.

      Please let me know if you have any further questions.

  3. hello,
    thanks for putting this simple and nice example. Have few questions for you, hope to see your reply on them.
    1. What is the benefit of using 2 Arduinos + 1 raspi? why not 1raspi and 1 arduino only?
    2. How to add more slave arduinos? do I need to define different pipe address for each slave arduino? or use the Array “state” to add for more arduinos?
    3. Does each rpi gpio pin required to direct each slave arduino?
    4. which software did you use to build the circuit ? it is very nice and I would to use it to build mine

    1. I’m glad you like it:)
      1. I couldn’t get library for Raspberry Pi working, so I took this approach.
      2. To add more Arduinos, you just need to define new int for each GPIO on Raspberry Pi and Arduino. You also have to wire a transistor between Raspberry Pi and Arduino.
      3. Yes
      4. You can get the software for making circuit diagrams here: http://fritzing.org/home/

      However, I would recommend you to use ESP8266 instead of nRF24L01+ for home automation, since it’s much easier to make and add more slaves. ESP8266 replaces Raspberry pi, Arduino and nRF24L01.
      You can read more about it here: https://www.bukovac.si/wemos-d1-arduino-compatible-esp8266/

  4. I have noticed you don’t monetize your page, don’t waste your traffic, you can earn additional bucks every month because you’ve got hi quality content.
    If you want to know how to make extra money, search for:
    Boorfe’s tips best adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *