Starting an Home Automation project

Good evening. This is my first project on Arduino. I'm very seduced by it and the opensource community around it :slight_smile:
Now, I'm pretty a newbye about eletronic things but I'm a good developer (php/mysql/java/android/blablabla) so I can understand well c code behind Arduino sketch.

I want to start from small ideas and project's steps and grow it day by day understanding it in the deep.
First of all what I want to do it's to control lights system.

I've some questions:

  • Arduino has a limited number of pins. If I have to controll lights in all my rooms (1room=1group of lights) I cannot use only 1 arduino, rights? So I need to use more arduinos and allow them to "talk"?
  • How can I develop and manage them? What do I need to do that (the elettronic parts). Are there some "already made" (something that is developed for arduino) parts to use? Something that you can advice me.

The first step of the project must do these things:

  • on/off group of lights
  • check stat of group lights
  • turn on smoothly lights / turn off smoothly

Thank you very much for your time :slight_smile: I will update about this project (it will work with an Android application)

I learned a few things.

Arduino can use multiplexor to multiply its pins. Multiplexor doen't allow simultaneus acces to pins.
To control group of lights I can use relay but I don't understand if it's a standard eletronic component or if I need to build one like this: Arduino Playground - HomePage

If I don't understand wrong a relay is used to turn on / off lights, isn't it? And if I'm not wrong I can also check if the relay is turned on or off. Where can I found an example code about it?

Last question is: how can I control the power of the light to make them more ligher or less? (tell me if I don't explain well).
Is there a standard component or have I to build one?

a relay is used to turn on / off lights, isn't it?

Often a relay is a good choice yes.

And if I'm not wrong I can also check if the relay is turned on or off.

Use a double-pole relay and use the spare pole to read the relay status, or keep a variable in RAM that tells you what the current state is.

Where can I found an example code about it?

DigitalWrite (relayPin, HIGH); should about do it.

how can I control the power of the light to make them more ligher or less?

I assume we're talking AC mains. Normally a Triac can be used to adjust the brightness. It can also be used to turn on/off the light so in this case it's a better choice than a relay.

However, a normal triac is not isolated from the mains (that's bad) but you can get opto-isolated versions that are or just use an opto-coupler.

I can use relay but I don't understand if it's a standard eletronic component or if I need to build one like this:

You need to do as the PDF says or something equivalent. But that's not "building a relay", it's just controlling one.

How can I develop and manage them? What do I need to do that (the elettronic parts). Are there some "already made" (something that is developed for arduino) parts to use?

You need to develop a "network" of some kind. Given the number of times we get asked this I'm surprised nobody is ever interested in posts about new networks :), maybe there's already too many around.


Rob

Graynomad:

How can I develop and manage them? What do I need to do that (the elettronic parts). Are there some "already made" (something that is developed for arduino) parts to use?

You need to develop a "network" of some kind. Given the number of times we get asked this I'm surprised nobody is ever interested in posts about new networks :), maybe there's already too many around.

I'm surprised too about this thing!

Thank you very much for your answers :slight_smile:
I'll try to search for items you said.

Home Automation has also been great interest to me, and I have spent a good deal of time trying different things with regards to lights, garage door, pool pump, hvac, etc.

  • Arduino has a limited number of pins. If I have to controll lights in all my rooms (1room=1group of lights) I cannot use only 1 arduino, rights? So I need to use more arduinos and allow them to "talk"?

Not necessarily. I ordered a Centipede Shield (Centipede Shield V2 - Macetech Electronics Store) which provides an additional 64 digital pins at the expense of 2 analog pins. There are multiplexing options for analog pins if you need more. The biggest issue I've come across is a communications medium:

  • Ethernet - Just using this as low voltage wiring. Using 1 pin as 5v, 1 as Gnd, you can control up to 6 digital switches at once. The drawbacks are having to run the wires to each of the switches, as well as the limited length without using some sort of amplification. Using a simple relay circuit, I've been able to control a light switch. This is the cheapest option, but also the hardest to implement.
  • X10 - Essentially you would replace all the light switches with x10 compatible ones, and use a PL513/TW513 to send the commands from the Arduino. I've been having issues with sending the commands through my interface, but I think my transceiver might be busted. This is fairly cheap, but gives you no way to know the status without storing it. The problem with storing it is that for it to be accurate, you can't utilize the switches on the wall, or have to find a way to account for those. This isn't much more expensive than running wires, but severely limited on what you can do.
  • Wireless - This is the most expensive, but easiest to implement. It essentially requires a master Arduino and a slave arduino for each station (I use Picoduino's). I'm waiting on my Transmitter/Receiver to come in so I can get started on this. For me, it's too expensive to implement for the small stuff like lights and outlets, but a good option for stuff like the garage door (My first hands on project), HVAC, Sprinklers, Pool Pump, etc.
  • How can I develop and manage them? What do I need to do that (the elettronic parts). Are there some "already made" (something that is developed for arduino) parts to use? Something that you can advice me.

The first step of the project must do these things:

  • on/off group of lights
  • check stat of group lights
  • turn on smoothly lights / turn off smoothly

For anything where you will be switching high AC voltage, relays is a good start. Here is a good kit from Sparkfun that allows you to control a Light (or anything really) from a single digital pin and a 5v power source. The link is just to the PCB, so you would need to get the parts listed on the page if you don't already have them:

For checking the status, the best way I have come up with is to replace the light switch with a push button type and use 2 pins: 1 will control the relay, and 1 will toggle the relay pin. The relay pin can be triggered by the code, so that takes care of digital communication (Android), while the control pin would be hooked up to the light switch (Now a momentary button) that will toggle the relay pin.

Sorry I can't help you with the dimming, I haven't looked into it.

I actually have a half completed android program, web controlled arduino sketch (Similar to the webduino firmware). If you want to look at the code, or wanted to exchange ideas, send me a message.

Arrch:
Home Automation has also been great interest to me, and I have spent a good deal of time trying different things with regards to lights, garage door, pool pump, hvac, etc.

  • Arduino has a limited number of pins. If I have to controll lights in all my rooms (1room=1group of lights) I cannot use only 1 arduino, rights? So I need to use more arduinos and allow them to "talk"?

Not necessarily. I ordered a Centipede Shield (Centipede Shield V2 - Macetech Electronics Store) which provides an additional 64 digital pins at the expense of 2 analog pins. There are multiplexing options for analog pins if you need more. The biggest issue I've come across is a communications medium:

  • Ethernet - Just using this as low voltage wiring. Using 1 pin as 5v, 1 as Gnd, you can control up to 6 digital switches at once. The drawbacks are having to run the wires to each of the switches, as well as the limited length without using some sort of amplification. Using a simple relay circuit, I've been able to control a light switch. This is the cheapest option, but also the hardest to implement.
  • X10 - Essentially you would replace all the light switches with x10 compatible ones, and use a PL513/TW513 to send the commands from the Arduino. I've been having issues with sending the commands through my interface, but I think my transceiver might be busted. This is fairly cheap, but gives you no way to know the status without storing it. The problem with storing it is that for it to be accurate, you can't utilize the switches on the wall, or have to find a way to account for those. This isn't much more expensive than running wires, but severely limited on what you can do.
  • Wireless - This is the most expensive, but easiest to implement. It essentially requires a master Arduino and a slave arduino for each station (I use Picoduino's). I'm waiting on my Transmitter/Receiver to come in so I can get started on this. For me, it's too expensive to implement for the small stuff like lights and outlets, but a good option for stuff like the garage door (My first hands on project), HVAC, Sprinklers, Pool Pump, etc.

First of all, thank you very much for your reply :slight_smile:
In these days I spent a lot of time learning and reading about arduino. I understand that I can use multiplexor if I need more pins.
Yes, I'm facing the ethernet problem too. I mean, if I have a master arduino in the middle of the house how can I controll all lights?
The problem is easily solveable if the house is new and I can modify eltric cicuits bringing them to arduino.

But I don't think this is the right way to do things. I've to think about a genetal pourpose way :slight_smile: As you said maybe the best way it's to use a wireless system with master/slave arduino.
The big problem here (in Italy) is that houses are very old with very very very big walls, so I don't know how is powerful the wireless signal. I will test it, but I think the best way is to use, when it's possible the wired solution, also for latency issues.

Can you explain me what are those X10? I don't think that it's a common thing here in Italy/Europe.

Arrch:

  • How can I develop and manage them? What do I need to do that (the elettronic parts). Are there some "already made" (something that is developed for arduino) parts to use? Something that you can advice me.

The first step of the project must do these things:

  • on/off group of lights
  • check stat of group lights
  • turn on smoothly lights / turn off smoothly

For anything where you will be switching high AC voltage, relays is a good start. Here is a good kit from Sparkfun that allows you to control a Light (or anything really) from a single digital pin and a 5v power source. The link is just to the PCB, so you would need to get the parts listed on the page if you don't already have them:
Relay Control PCB - COM-09096 - SparkFun Electronics

For checking the status, the best way I have come up with is to replace the light switch with a push button type and use 2 pins: 1 will control the relay, and 1 will toggle the relay pin. The relay pin can be triggered by the code, so that takes care of digital communication (Android), while the control pin would be hooked up to the light switch (Now a momentary button) that will toggle the relay pin.

Sorry I can't help you with the dimming, I haven't looked into it.

I actually have a half completed android program, web controlled android sketch (Similar to the webduino firmware). If you want to look at the code, or wanted to exchange ideas, send me a message.

Yes I also saw those board. As soon I have a bit of money I will order it and I'll hope that everything will arrive in Italy :smiley:
I'm also interested understanding (I'm really noob about electronic stuffs) the second idea about the relay (to sync sync the light switch with the arduino pin).
You can contact me back in private to share things. At the moment I've developed only the sketch code to handle more actions ( request ).
As I said I've only the arduino diecimila and not the ethernet shield, so I'm not developing for android yet.

Also, if you want, as soon as I can I will connect to #arduino @ irc.freenode.net (for me now it's 23:09, so goodnight!)

Yes, I'm facing the ethernet problem too. I mean, if I have a master arduino in the middle of the house how can I controll all lights?

I meant using Cat-5e cables as low voltages lines from the light switch to the Arduino. This is how I had it hooked up:

The problem is that you have no way of detecting if the light is switched unless you get a DPDT light switch and use another pin. I couldn't find a cheap one that would go right into the wall, so that idea is on hold.

But I don't think this is the right way to do things. I've to think about a genetal pourpose way :slight_smile: As you said maybe the best way it's to use a wireless system with master/slave arduino.
The big problem here (in Italy) is that houses are very old with very very very big walls, so I don't know how is powerful the wireless signal. I will test it, but I think the best way is to use, when it's possible the wired solution, also for latency issues.

Here's a pretty good article about the different wireless technology:

http://www.sparkfun.com/tutorials/128

For now, I'm just going to be trying the general wireless using a pair of basic transmitters/receivers. According to the above, they have the 2nd best range below Cellular. The only disadvantage to this, is that you have to write your own protocol. There is a library, VirtualWire, that essentially provides you with what you need.

Can you explain me what are those X10? I don't think that it's a common thing here in Italy/Europe.

X10 is a powerline protocol, that transmits data across a power line. A bit is sent at every zero crossing. I'm not familiar with it's popularity across the pond, but it was invented by a Scotsman, so I wouldn't think it was that foreign. It's built for simple on, off, dim and brighten commands, so it's ideal for lights. As I mentioned, the problem is that it's not easy to determine the state of the light. There is limited support for two-way communication, but if you get an x10 light switch that can transmit, the price goes up to the point where you may as well use wireless. Plus, there isn't much support for receiving x10 signals on the Arduino.

More info on the protocol:

Arduino Implementation:
http://www.arduino.cc/en/Tutorial/X10

I'm also interested understanding (I'm really noob about electronic stuffs) the second idea about the relay (to sync sync the light switch with the arduino pin).

The following is what I used as a starting point for my current code to control a light switch, from both the network and a physical switch. Eventually, I turned to class into an array of pins and added support for the Centipede shield, but this is the basic concept. As you can see, a push of a momentary button would trigger the light switch, as would calling it from the code. Replace the LED with a relay driven light switch (Example I linked before) and presto!

Main Sketch .pde file:

// File: ButtonToggleTesting.pde
// Sketch to test the ButtonToggle Class
// Author: Arrch (Arrchduino@gmail.com)
// Date: 6/20/11

#include "ButtonToggle.h" // Include our library

#define readPin 2 // Connected to a momentary button. pulled low
#define outPin 13 // Output to an LED

ButtonToggle btn(readPin, outPin);

char readByte;

void setup() {
  Serial.begin(9600); // Used to accept the toggle command
}

void loop() {
  btn.check(); // Checks the button and changes the state accordingly
  if (Serial.available() > 0) { // We have serial data available
    readByte = Serial.read(); // Get the data
    if (readByte = 'p') // If we receive a p
      btn.toggle(); // Toggle the out pin
  }
}

ButtonToggle class header:

// File: ButtonToggle.h
// Implements the ButtonToggle class
// Author: Arrch (Arrchduino@gmail.com)
// Date: 6/20/11
// A ButtonToggle uses an input pin and an output pin. When the input pin is pulled low,
// the output pin is toggled HIGH or LOW depending on it's previous state

#ifndef ButtonToggle_h
#define ButtonToggle_h

#include "WProgram.h" // Needed for class definitions

class ButtonToggle {
  public:
    ButtonToggle(int readPin, int outPin); // Constructor with read and out pins
    void setReadPin(int readPin); // Change the read pin
    void setOutPin(int outPin); // Change the out pin
    void check(); // Run on every loop to check for read pin being pulled low
    void toggle(); // Toggles the out pin
    bool isOn(); // Sees if the device is on
  private:
    int _readPin; // pin hooked up to momentary button, pulled LOW
    int _outPin; // pin hooked up to LED
    int _lastState; // Last state of the read pin
    int _lastDebounce; // Time that the last state change occured
    bool _isOn; // State of the output pin
    bool _ready; // Set to true after the button is realeased so it is ready to accept another click
};

#endif

ButtonToggle Implementation:

// File: ButtonToggle.cpp

#include "WProgram.h"
#include "ButtonToggle.h"

#define debounceDelay 50 // ms
ButtonToggle::ButtonToggle(int readPin, int outPin)
{ 
  _readPin = readPin;
  _outPin = outPin;
  _lastState = LOW;
  _lastDebounce = millis();
  _isOn = false;
  _ready = true;
  
  pinMode(readPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void ButtonToggle::check() 
{ 
  int time = millis(); // Get the current time
  int state = digitalRead(_readPin); // Get the current state
  
  if (state != _lastState) // State change
    _lastDebounce = millis(); // Reset the doubounce timer on state change
  
  if ((millis() - _lastDebounce) > debounceDelay) { // state has remained constant for the delay time
    if (_ready && (state == HIGH) ) {
      toggle(); // Change the out pin's state
      _ready = false; // So we don't toggle again until the button is pressed again
    }
    if (state == LOW)
      _ready = true; // Button is released, set to ready
  }
  _lastState = state;
}

void ButtonToggle::toggle() { 
  if (_isOn) {
    digitalWrite(_outPin, LOW);
    _isOn = false;
  } else {
    digitalWrite(_outPin, HIGH);
    _isOn = true;
  } 
}

You can contact me back in private to share things. At the moment I've developed only the sketch code to handle more actions ( request ).
As I said I've only the arduino diecimila and not the ethernet shield, so I'm not developing for android yet.

I don't mind sharing information publicly, my code just isn't easy to understand and is pretty complex, so there isn't much point in posting it in it's entirety without being able to describe the context of it.

Ok I've some problem understanding english/tecnical words. I'm going to check theme now :smiley:

Just asking because as I said it's mi first time here with arduino: is not possibile to hook my problem on certain events? Like, I hook my code to the event, from 0v to 5v.

It's built for simple on, off, dim and brighten commands, so it's ideal for lights. As I mentioned, the problem is that it's not easy to determine the state of the light. There is limited support for two-way communication, but if you get an x10 light switch that can transmit, the price goes up to the point where you may as well use wireless. Plus, there isn't much support for receiving x10 signals on the Arduino.

As I said I want to build a working automation system with low money, otherwise I can go with others system that costs thousends of $ :smiley: I want to fully use the power of arduino!
Anyway what I don't get it's this:

you can set a pin to HIGH or LOW, so you can turn on or turn off the lights.
Thank, to check if the light is on or off, can't you just read from that pin and read the status on it? (as I said, I'm just asking because atm I cannot check what I'm talking about)
I know that there's a problem:

this will be unsync when a person use the manual switch. So the light will be off but arduino, checking on the relay will se that the light is off.
Now I will take a look to that x10 and I'll edit my post.

Coffe time :stuck_out_tongue:

You can read an output or just store a copy in memory, either way you can tell what the relay should be. But as you pointed out what if you have a manual switch that can change the relay state?

You can run the switch to the processor and let it make all the decisions. But there can be problems when you have a failure in the system and the processor decides it knows best about the relay's state.

You can have the switch manually control but also run a wire from the switch to the processor, likewise you can read this to see what state the relay should be in.

A relay is just a switch, a relay with one switch is called "single pole", a relay with two switches is called "double pole". You can use this to your advantage.

Get a double pole relay, use one pole to control the light and the other to feed back the relay state to the processor.

Now there is no real chance for confusion because the processor can read the relay state at any time.

You can (in fact should) also organize things so the button will work the light regardless of the processor, this gives you a backup for times when the auto system is not working (there will be many such times :))


Rob

Graynomad:
You can read an output or just store a copy in memory, either way you can tell what the relay should be. But as you pointed out what if you have a manual switch that can change the relay state?
You can run the switch to the processor and let it make all the decisions. But there can be problems when you have a failure in the system and the processor decides it knows best about the relay's state.
You can have the switch manually control but also run a wire from the switch to the processor, likewise you can read this to see what state the relay should be in.
A relay is just a switch, a relay with one switch is called "single pole", a relay with two switches is called "double pole". You can use this to your advantage.
Get a double pole relay, use one pole to control the light and the other to feed back the relay state to the processor.
Now there is no real chance for confusion because the processor can read the relay state at any time.
You can (in fact should) also organize things so the button will work the light regardless of the processor, this gives you a backup for times when the auto system is not working (there will be many such times :))

Great explaination Rob! thank you very much, now I understand how to do it :slight_smile:

This morning I was reading about X10 on Wikipedia, it's a great things (has some point of failure about delay) but it really cost a lot.
Ok you can use 1 module to control more lights, but it really costs a lot!

StErMi:
This morning I was reading about X10 on Wikipedia, it's a great things (has some point of failure about delay) but it really cost a lot.
Ok you can use 1 module to control more lights, but it really costs a lot!

It shouldn't cost that much. The transmitter that interfaces with the Arduino is about $10-20, and you can get receivers for about $10 a piece. Buying the commercial brand transmitter/receiver kits will definitely jack the cost up, but those have their own internal controls, ad don't need an Arduino.

Arrch:

StErMi:
This morning I was reading about X10 on Wikipedia, it's a great things (has some point of failure about delay) but it really cost a lot.
Ok you can use 1 module to control more lights, but it really costs a lot!

It shouldn't cost that much. The transmitter that interfaces with the Arduino is about $10-20, and you can get receivers for about $10 a piece. Buying the commercial brand transmitter/receiver kits will definitely jack the cost up, but those have their own internal controls, ad don't need an Arduino.

Can you please make me a real example of transmitter and receiver? (I mean a link to a shop, so I understand what I'm looking for).
Anyway this solution it's not so expansive if you have a few receiver (a few lights or group of lights) because if you have 16 device you're going to spend $160 + $10 for trasmitter.

Here is the transmitter:

And the receiver:

That's just a quick look; you should be able to find it cheaper. The second one is intended for a plug-in, but you can dismantle it and just use the circuitry. Also, those are for US style plugs. I can't really help on finding the equivalent for Europe.

I was taking a look on italian price for x10 components... they cost more than 30euros... DAMN IT!

Here is a different approach:
http://www.intertechno.at/produkte/zwischenstecker/schalter/PA3-1000.html this kit sells for 20€. They also have receivers that can be fitted in the walls.
The only drawback I see is that 2 receivers cannot stand closer than 0.5 m.

Buy a set like this, a 433MHz transmiter then look on the playground at Arduino Playground - HomeEasy or similar libraries.
If you cannot find a library to match your receivers then you have to decode the remote and replicate the signal or wire it to the arduino.

I already did it and I have almost 10 devices controlled by one arduino.

From a cost point of view I cannot see anything better

good luck

It seems you're trying to do what I've accomplished.

If you need a parts list, I'll provide one. I think I did all of it for fairly cheap. Just a lot of research and programming time... but if you want to follow my route, I'll save you the headache and trouble my sharing my codes and source code for the app.