If you are just starting with home automation and open-source hardware, this article is for you. Do not worry: I will not show you how to plug in your Arduino board to your computer for the first time and make a LED blink continuously. There are tutorials for that, and here we want to talk about home automation.

Well, the first thing you can think about when doing home automation is to send commands to something (can be a LED of course, but also a relay that will switch on or off a light, a motor …) via a software which is running on the device that control your home (your computer, a home server, a Raspberry Pi …). And this is exactly what you will learn in this tutorial. For the something, we will take a simple LED, because it is easy and can be replaced effortlessly with a relay that controls a light for example. And we will do it with the computer as the “brain” that sends commands, and using the language Python as it is easy to play with and it is also running on the Raspberry Pi. And to make the interface between the device to control and the computer, we will use an Arduino board.

Hardware requirements

First of all, you will need an Arduino board. I used the Arduino UNO board for this tutorial, but any other official Arduino board should be fine as well. You will also need a LED and a resistor (I used a 470 ohms resistor for this tutorial), as well as a breadboard and some wires to put everything together.

Software requirements

You do not need much software for this tutorial, you just need to have Python installed with the pySerial module (http://pyserial.sourceforge.net/).

Hardware configuration

The hardware configuration for this project is also very simple : plug in the Arduino board to your computer, and then connect the LED in series with the resistance to pin number 9. I included the schematics below:

Open Home Automation 101 : controlling an actuator using your computer & Arduino

Testing individual parts

At this point, you should just make sure that the LED is indeed working, which can save you a lot of headache in the next section. For this, simply upload the sketch below into your Arduino board:

// LED pin
int led = 9;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  // Make the LED blink
  digitalWrite(led, HIGH);
  delay(2000);
  digitalWrite(led,LOW);
  delay(2000);
}

This should make the LED switch on and off every 2 seconds. Sorry for the simplicity of this part, but you really have to be sure that your hardware is working before going further.

Putting it all together

The goal is now to send commands to the Arduino board to control the state of the LED, without having to upload a new Arduino script every time. We will develop two distinct pieces of software: one Arduino sktech that can receive commands from the computer, and one Python script that will run on the computer and that will send the commands to the board.

Let’s start with the Python code. What we need is to use this pySerial module to communicate with the Arduino board:

import serial

The serial connection then has to be initialised. Be careful here, you will have to change this piece of code according to your configuration (the /dev/tty.usbmodem1411). You can find this information in the Arduino IDE under Tools/Serial Port.

ser = serial.Serial('/dev/tty.usbmodem1411', 9600)

Before actually communicating with the Arduino board, we have to think about how we will send the messages to the board. We could of course just send zeros and ones to the board, but I suggest we use something more sophisticated to avoid transmission problems and to prepare the code for more complicated projects: use a transmission protocol. A transmission protocol defines the shape of the messages that are sent between the devices. In this tutorial I used a very simple transmission protocol: I start by transmitting a header (in this case a character: “H”), followed by a coma, followed the value we want to transmit. At the end, a message will look like H,0 or H,1.

We can define a function to format what we want to send according to the protocol:

def send_command(value):
   ser.write('H')
   cmd = str(value) + ','
   ser.write(cmd)

Finally, we can put all the code together and add some lines to make the LED blink:

import serial
import time
import numpy

# Is the serial port available ?
try:
 ser = serial.Serial('/dev/tty.usbmodem1411', 9600)
 time.sleep(2)
except:
 print 'No Arduino found'
 sys.exit()

# Function to send the command
def send_command(value):
 ser.write('H')
 cmd = str(value) + ','
 ser.write(cmd)

# Make the LED blink
while True:
 send_command(1)
 time.sleep(0.1)
 send_command(0)
 time.sleep(0.1)

On the Arduino side, the important part is to check if data is available on the serial port:

if (Serial.available())

Then, we check if the received message is valid, which means it starts with “H”:

if (Serial.read() == 'H')

If the message is valid, we store the value of the integer contained in the message:

received_value = Serial.parseInt();

The final Arduino sketch is then:

Now that we understood the code, we can finally test it. First, upload the Arduino sketch to your Arduino board. Then, you can start your Python script (for example from a terminal) using:

python remote_command.py

If everything went well you should then see you LED blinking every 100 ms. Again, the main difference between this and doing the same in an Arduino code is that you can now simply change the Python code to command your LED without having to change the Arduino sketch.

Here is a summary of all the components you will need for this project:

This is the end of this tutorial, hope you enjoyed it ! Now that you have a nice interface between your computer and your Arduino board you can use it to build more elaborate home automation projects, do not hesitate to share !