HOME AUTOMATION

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arduino » HOME AUTOMATION

As an Amazon Associate we earn from qualifying purchases.

Home automation does not have to mean an expensive hub and a stack of branded devices. With an Arduino board, a wireless serial module and an Android phone, you can switch a bulb on and off and change a fan’s speed from your pocket. This project shows how that works, explains the code line by line and fixes a bug in the original program.

HOME AUTOMATION USING ANDROID MOBILE

The video below shows the project in action, followed by the project image.

Home automation project diagram using Arduino and an Android mobile phone

How the project works

The idea is simple. The phone sends a single character to the Arduino over a serial link. The Arduino reads that character and decides what to do with two output pins: one drives a bulb, the other drives a fan.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

The serial link is usually a Bluetooth module wired to the Arduino’s serial pins, with a terminal or controller app on the phone. The code runs the serial port at 9600 baud, which is a common default for low-cost Bluetooth modules, but check the datasheet of your module. If you want the wireless side explained in more detail, see our guide to home automation using Android mobile and Bluetooth.

What you need

Connections

  • Pin 8 is a plain digital output and switches the bulb through the relay module.
  • Pin 9 is used with analogWrite, so it needs to be a PWM-capable pin. On an Arduino Uno, pin 9 is one.
  • The Bluetooth module’s TX and RX lines connect to the Arduino’s serial pins. Remember that TX goes to RX and RX goes to TX, and that many modules expect a lower voltage level on their RX input.
  • All grounds must be shared between the Arduino, the module and the driver circuits.
See also
Z - SOURCE INVERTER -SINGLE PHASE SIMULATION IN MATLAB - SIMULINK

Do not connect a fan or relay coil straight to an Arduino pin. Pins can supply only a small current, so use a driver stage between the pin and the load.

Command list

The phone sends one character at a time. The program responds like this:

  • L: bulb on
  • l: bulb off
  • : fan off
  • 1 to 5: fan speed in five steps, using PWM values of 50, 100, 150, 200 and 255 (out of a maximum of 255)

Note that commands are case sensitive, so an uppercase L and a lowercase l do different things.

PROGRAM

This is the corrected program. The original ended with an else branch attached only to the check for the character ‘5’. That meant the bulb was switched off on almost every pass of the loop, so it could never stay on. Removing that branch fixes it.

int bulb = 8,fan=9;
unsigned char a;
void setup()
{
    pinMode(bulb, OUTPUT);   pinMode(fan, OUTPUT);
    digitalWrite(bulb, LOW);   digitalWrite(fan, LOW);
    Serial.begin(9600);
    Serial.println("Home automation project "); Serial.println(" by Emerging Technologies");
    digitalWrite(bulb, HIGH);Serial.println("BULB ON");
}
void loop() {
    if(Serial.available()>0){a=Serial.read();}
    if(a=='L'){digitalWrite(bulb, HIGH);
    }
    if(a=='l'){digitalWrite(bulb, LOW);
    }
    if(a=='0'){analogWrite(fan, 0);
    }
    if(a=='1'){analogWrite(fan, 50);}
    if(a=='2'){analogWrite(fan, 100);}
    if(a=='3'){analogWrite(fan, 150);}
    if(a=='4'){analogWrite(fan, 200);}
    if(a=='5'){analogWrite(fan, 255);}
}

Code walkthrough

Setup

The setup function sets both pins as outputs and starts them in the LOW (off) state. It then opens the serial port at 9600 baud, prints a short start-up message and turns the bulb on, reporting “BULB ON” back over serial.

Loop

The loop function checks whether a character has arrived. If so, it stores that character in the variable a. A series of if statements then compares a to each known command and acts on it. Because a keeps its value until a new character arrives, the same command is simply repeated harmlessly on each pass.

Why PWM for the fan

analogWrite produces a PWM signal, which switches the output on and off very quickly. A higher value means the output is on for more of each cycle, so a driven DC fan runs faster. This works for a DC fan behind a transistor or MOSFET. It is not suitable for a mains AC fan or a relay, which cannot be speed-controlled this way.

See also
ADVANCED HOME AUTOMATION SYSTEM WITH INVERTER USING ZIGBEE TECHNOLOGY

Testing the project

  1. Upload the program to the Arduino before connecting the Bluetooth module, since many boards share the serial pins with the USB upload connection.
  2. Connect the module and pair it with your phone.
  3. Open the controller app and connect to the module.
  4. Send L and l and check that the bulb switches on and off.
  5. Send 0 to 5 and check that the fan speed changes in steps.

If nothing happens, check the baud rate, the TX/RX wiring and the common ground first. These are the most frequent causes of a silent project.

Safety when switching a mains bulb

A real household bulb runs on mains voltage, which can be dangerous. Test the logic first with a low-voltage LED or lamp. If you move to mains, use a relay module rated for the load, keep the wiring inside an insulated enclosure, and have someone experienced check it before you power it up.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

Ways to extend it

  • Add more outputs for extra lights or appliances, each with its own command character.
  • Send text feedback to the phone so you can see the current state of each device.
  • Replace the phone with other controllers. For example, an IR remote and relay design uses a different microcontroller, and a low-cost infrared receiver remote approach drops the need for a phone entirely.
  • Add sensors so the system reacts on its own. Our overview of smart sensors for home automation is a good place to start.

If you would rather buy than build, a ready-made smart light switch can replace the bulb side of this project without any wiring of your own design.

FAQ

Which Arduino board can I use?

Any board with a free digital pin and a PWM-capable pin will work. The code assumes pin 8 for the bulb and pin 9 for the fan, which suits an Arduino Uno. On other boards, check which pins support PWM.

Why does the bulb need a relay?

An Arduino pin can only supply a small current at a low voltage. A relay lets that small signal switch a much larger load safely, keeping the mains side electrically separate from the board.

See also
CUK CONVERTER SIMULATION AND HARDWARE USING ARDUINO AND MATLAB SIMULINK

Can I control the fan speed with a relay?

No. A relay is designed for on/off switching, not rapid PWM. Use a transistor or MOSFET driver with a DC fan if you want variable speed.

Why did the bulb switch off right after turning on in the original code?

The original program had an else branch that turned the bulb off whenever the received character was not ‘5’. Removing that branch, as in the corrected code above, lets the bulb stay on until you send l.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works