• Dhanush kumar S

Want to make a Smart Lighting System with 3 LEDs using HC-05, RPi, and mobile?

Updated: Apr 16

Nowadays, the idea of a #Smart Lighting System is getting emerged rapidly. You would have watched the advertisements on television that a man will be controlling home lights from his workplace.

Do you know how that works? It's nothing but an integrated work of #Bluetooth and mobile application. As you proceed, you will get to know about the working of the Bluetooth device and how this circuit works?


However, this article provides step by step guide to perform the #interfacing, starting from the list of required components.


APPARATUS REQUIRED:


The following list provides the hardware and software components required to perform this interfacing.

Hardware components:


1.Bluetooth Module [HC-05]:

This module follows the IEEE 802.15.1 Standardized protocol. It helps us to construct a Wireless Personal Area Network (PAN), and for the transmission of data, the device employs frequency hopping spread spectrum (FHSS). To interact with other devices, it uses the mode of serial communication. The module consists of 6 pins and it can be operated either in master or slave configuration.


2.Raspberry Pi 3:

We are using #Raspberrypi to connect with the Bluetooth HC-05 module and display the output. It contains several I/O pins using which we can connect the module and then by coding the Raspberry pi, we will receive the commands/signals that are sent from the mobile.


3.USB Cable:

This cable is used to connect the #Raspberrypi to the power supply. It also helps in the transmission of data.


4.Jumper wires:


These are simple connecting wires, that are used to connect the ends without any soldering. The male wires have the ends that can be connected to other ends and the female wires are the ones that have the receiving end.


Software components:


5.SSH Terminal:

The SHH Terminal is used in this project to connect the Raspberry Pi module to the terminal of another computer.


6.Python IDE:

Python Programming is done in an IDE (Integrated Development Environment) is a platform where we can write the code, run, and test the working.


CIRCUIT:


First, we shall connect the Bluetooth module HC-05 with a raspberry pi device.


What goes where?

  • The RED wire connects the VCC of the Bluetooth module to the 5V (VCC) of the raspberry pi.

  • The BLACK wire connects the ground pin of HC-05 to the ground pin of the raspberry pi.

  • The YELLOW wire connects the transmitting pin [TX] of HC-05 to the receiving pin [RX] of the raspberry pi device.

  • The BLUE wire connects the receiving pin [RX] of HC-05 to the transmitting pin[TX] of the raspberry pi.


After finishing this, we shall connect the LEDs fixed in the breadboard to the interface of HC-05 and raspberry pi.

We can connect the LEDs in any of these pins but should be mentioned accordingly in code files. In the above-given circuit, the PINK wire is connected with GPIO 27 pin and that shall be our first LED. The BLUE wire connects the second LED with GPIO 17 pin. And the ORANGE wire connects our third LED with GPIO 18 pin. The overall ground is then connected to the GND pin of the raspberry pi.


Then power supply is given to the circuit, the raspberry pi gets the boot-up and the LED of Bluetooth module HC-05 flashes rapidly indicating that it is ready to connect.


Code:

At first, we have to import Rpi.gpio and serial libraries from the #python library. The serial library helps in serial communication through the Bluetooth device and the Rpi.gpio library helps for configuring the GPIO pins in raspberry devices. A variable called uart _channel is created and inside that, a serial function is composing three arguments.


· CODE FILE: /dev/ttyAMAO

· BAUD RATE: 9600

· TIMEOUT: 2


 import RPi.GPIO as GPIO
import serial

uart_channel = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=2)

After initializing the variable, While loop is constructed for the continuous execution of commands. Inside the while loop, the command GPIO. setmode(GPIO.BCM) is executed to give the name to pins such as GPIO 17, GPIO 18.

Then the warnings are set to be false so that even if they arise, they cannot be displayed. For the first LED, GPIO 27 pin is initialized as output, and similarly, for the second LED, pin 17 and for the third LED, pin 18 is initialized as output. To read the data from the mobile app, a function called uart_channel. read () is employed.

while True:
    try:
       GPIO.setmode(GPIO.BCM)
       GPIO.setwarnings(False)
       GPIO.setup(27,GPIO.OUT) #First LED
       GPIO.setup(17, GPIO.OUT) #Second LED
       GPIO.setup(18, GPIO.OUT) #Third LED

       data = uart_channel.read()

  • For data=1, the first LED should glow. So the pin 27 is set to be high and the other two pins are set to low.

  • For data=2, the second LED should glow. So the pin 17 is set to be high and the other two pins are set to low.

  • For data=3, the third LED should glow. So the pin 18 is set to be high and the other two pins are set to low.

  • For data=4, all LEDs should glow. So all three pins are set to be high.

  • for data=5. no LED should glow. So all three pins are set to low.

if data == "1":
           GPIO.output(27, GPIO.HIGH)
           GPIO.output(17, GPIO.LOW)
           GPIO.output(18, GPIO.LOW)
           print("LED:1 ON")

       if data == "2":
           GPIO.output(27, GPIO.LOW)
           GPIO.output(17, GPIO.HIGH)
           GPIO.output(18, GPIO.LOW)
           print("LED:2 ON")

       if data == "3":
           GPIO.output(27, GPIO.LOW)
           GPIO.output(17, GPIO.LOW)
           GPIO.output(18, GPIO.HIGH)
           print("LED:3 ON")

       if data == "4":
           GPIO.output(27, GPIO.HIGH)
           GPIO.output(17, GPIO.HIGH)
           GPIO.output(18, GPIO.HIGH)
           print("All LEDS ON")

       if data == "5":
           GPIO.output(27, GPIO.LOW)
           GPIO.output(17, GPIO.LOW)
           GPIO.output(18, GPIO.LOW)
           print("All LEDs OFF")



If any keyboard interrupt occurs, an exception is thrown and the function breaks itself out of the while loop.


import RPi.GPIO as GPIO
import serial

uart_channel = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=2)

while True:
    try:
       GPIO.setmode(GPIO.BCM)
       GPIO.setwarnings(False)
       GPIO.setup(27,GPIO.OUT) #First LED
       GPIO.setup(17, GPIO.OUT) #Second LED
       GPIO.setup(18, GPIO.OUT) #Third LED

       data = uart_channel.read()
       if data == "1":
           GPIO.output(27, GPIO.HIGH)
           GPIO.output(17, GPIO.LOW)
           GPIO.output(18, GPIO.LOW)
           print("LED:1 ON")

       if data == "2":
           GPIO.output(27, GPIO.LOW)
           GPIO.output(17, GPIO.HIGH)
           GPIO.output(18, GPIO.LOW)
           print("LED:2 ON")

       if data == "3":
           GPIO.output(27, GPIO.LOW)
           GPIO.output(17, GPIO.LOW)
           GPIO.output(18, GPIO.HIGH)
           print("LED:3 ON")

       if data == "4":
           GPIO.output(27, GPIO.HIGH)
           GPIO.output(17, GPIO.HIGH)
           GPIO.output(18, GPIO.HIGH)
           print("All LEDS ON")

       if data == "5":
           GPIO.output(27, GPIO.LOW)
           GPIO.output(17, GPIO.LOW)
           GPIO.output(18, GPIO.LOW)
           print("All LEDs OFF")

    except KeyboardInterrupt:
        print("\nProgram ended")
        break

GPIO.cleanup()

Download the code here,

HC05_LED (Main)
.txt
TXT • 1KB




Note: This file is in the text format and should be converted to python-format (.py) before downloading.


Working:


The Bluetooth terminal HC-05 is the mobile application employed here to establish a connection with HC-05. In that application-specific buttons are created to perform the operations.

The application is now configured with buttons related to this experiment such as LED 1, LED 2, LED 3, all LED off, all LED on. The ASCII command of LED 1 On is 1 and LED 2 On is 2, LED 3 On is 3.

For all LEDs on, the command is 4 and all LED off its 5.

After making these configurations, we can check the credibility of this setup by giving the necessary commands in mobile application and the glow of LEDs respectively.


After the execution of code, according to the input commands given in the mobile application the LED glows.

The ASCII command is given as 1,

And the first LED glows,


The ASCII command is given as 2,


The second LED glows,


The ASCII command is given as 3,


The third LED glows,


The ASCII command is given as 4,


All LEDs are glowing,


The ASCII command is given as 5,


And no LED is glowing,


The practical implementation of this interfacing is presented here as a video.


Video credits: Sai Parimal


As mentioned before, nowadays the demand for a smart lighting system is getting increased. By experimenting with this prototype of controlling LEDs using mobile apps, we can understand the concept of controlling our smart home appliances from workplaces. Energy conservation is a big advantage of this theme.


See also:


Article on interfacing LCD with raspberry pi.

Article on interfacing 4*4 keypad with raspberry pi

Smart lighting system- using LED's, LDR and raspberry pi


35 views