Easily Control Lights with Raspberry Pi DIY

Recently, I have been researching microcontrollers and the security of IoT-based devices. Therefore, I started considering building a small home automation system. Although it is not yet complete, I would like to share how I use Raspberry Pi 2 and some other electronic components to control the lights in a room. Of course, I will not cover the initial setup of the Raspberry Pi here, as you can find various tutorials online.

Easily Control Lights with Raspberry Pi DIYCautions

Before we continue with the experiment, I think it is necessary to remind you about the dangers of “current” in the experiment. In the worst-case scenario, you could die or burn down your house. So, please do not attempt anything mentioned in this article that you do not understand, or seek help from an experienced electrician while making it.

Alright, let’s start DIY!

Experiment Preparation

Hardware Requirements

1. Raspberry Pi 2 (or any model with 5V output power)

2. USB wireless dongle

3. 8-channel relay

4. Some Female-Female jumper wires (40 PCS FEMALE TO FEMALE JUMPER WIRES)

5. Light socket wire

(All the above hardware is available for sale on various online platforms)

Other Requirements

1. Basic understanding of Python or any other programming language (I will be using Python)

2. Basic understanding of Linux systems

3. Focus and dedication

Process Requirements

First, connect to the Raspberry Pi via SSH and install “apache” and “php5”:

Easily Control Lights with Raspberry Pi DIY

You will need to install the Python GPIO library to control the GPIO pins of the Raspberry Pi:

Easily Control Lights with Raspberry Pi DIY

Understanding Components

Now, before we continue with the build, you need to understand the electronic components we will be using.

1. Relay

A relay is an electrical device that uses a very low voltage input to control high voltage electricity. It consists of a metal rod wrapped with a coil and two small metal nodes forming a closed circuit. One node is fixed, while the other is movable. Whenever current flows through the coil, it generates a magnetic field that attracts the movable node towards the static node, completing the circuit. By supplying a small voltage to the coil, we can control the high voltage circuit. Additionally, the static node is not physically connected to the coil, so if something goes wrong, the microcontroller-driven coil is less likely to fail.

Easily Control Lights with Raspberry Pi DIY

In this experiment, I am using an 8-channel relay, which can control 8 devices simultaneously. You can choose your own relay or relay board, but make sure to handle it within the rated voltage of the relay to avoid any accidents.

2. Jumper Wires

Jumper wires are the simple connecting wires we use to link the Raspberry Pi GPIO pins to the relay.

3. Raspberry Pi 2

We use Raspberry Pi 2 as a microcontroller to manipulate the relay. It has 40 GPIO (General Purpose Input/Output) pins. You can see the layout of these pins below, which we will use to power the relay and control the switches.

Easily Control Lights with Raspberry Pi DIY

Connecting the Circuit

The circuit is very simple. We will connect the GPIO pins to the relay board. First, connect the “GND” on the relay board to any “GND” on the Raspberry Pi. Then link the relay’s “IND1” to GPIO PIN 17, which we will use as the output to control the first relay. Finally, connect the relay’s “VCC” to the “5V” GPIO pin on the Raspberry Pi. Let’s set this up simply and directly:

Easily Control Lights with Raspberry Pi DIY

Now we come to the most challenging part, where we need to connect the relay to the light socket that powers the main circuit. However, I would like to first introduce a simple operation to turn the light on and off using a DC power supply.

We typically connect two wires to the light bulb to provide current. One wire is the “neutral” wire, while the other is the actual “live” wire carrying the current. There is also a switch that controls the entire circuit. Therefore, when the switch (closed) connects to the current flowing through the light bulb and the live wire, the circuit is complete, and the light bulb lights up. When the switch (open) breaks the circuit and the current to the light bulb, the light bulb does not light up. Here is a small circuit diagram to explain the situation:

Easily Control Lights with Raspberry Pi DIY

During our experiment, we need the “live wire” to break the circuit through our relay, thus using the relay switch to control the current flowing. Therefore, when the relay is on, the light bulb should light up in the closed circuit, and vice versa. Please refer to the complete circuit:

Easily Control Lights with Raspberry Pi DIY

Control Script

Now, we finally reach the software part. I wrote a simple Python script to control the relay switch, using GPIO PIN 17, and a PHP code that can run the Python script on any mobile terminal. You can find this code on my GitHub (and CSS).

Easily Control Lights with Raspberry Pi DIY

Easily Control Lights with Raspberry Pi DIY

Note: You will need to add the “www-data” user to the sudoers file.

Note+: The PHP code is only for testing; we do not recommend running it in a public environment.

I will soon complete this setup and hope to return with an updated post. Please try this light control yourself before then, but be sure to pay attention to safety.

P.S: I am not very experienced with circuits, so if there are any mistakes or missed points, I would be happy to know. Please feel free to share your valuable opinions in the comments 🙂 (The editor will select excellent comments to translate for the original author)

Video demonstration:

1.

2.

This article is reprinted from FreeBuf Hacker and Geek, please indicate the source when reprinting

http://www.freebuf.com/news/topnews/72796.html

Leave a Comment