• Dhanush kumar S

Controlling LED using Blynk App - IoT Project

Updated: Aug 23, 2020

Imagine yourself as an office employee. Further in the morning, you went to your workplace without switching off the lamp in your home. Just imagine, how many units of current will be wasted until you get back home in the evening. In order to avoid this kind of energy wastage, the concept of a smart lighting system is being introduced. It is nothing but controlling of electrical appliances such as lamps using the internet and mobile application.


This article gives a detailed explanation of this concept starting from circuit assembling till the end output. Here, instead of electrical appliances, we are using #LEDs for better understanding. #Blynk is a mobile application designed to control electrical appliances. The article provides step by step instructions involved in this interfacing which is pretty simple, starting from the list of electronic components required.


Apparatus Required:


The list provides all the hardware and software components required for the interfacing.


Hardware components:


1.LDR Sensor:

This is a cadmium-sulfide cell works based on the principle of Photoconductivity. It is basically a resistor whose resistance value changes depending upon the intensity of light. When the light falls on the surface of the senor, material conductivity varies as a result of which resistance also varies.


2. Raspberry pi 3:

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


3. 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.


4. USB Cable:

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


5. Capacitor:

The resistance measured by the LDR sensor is done with the help of time constant concept. The time required to charge a capacitor is equal to the multiplied value of resistance of the circuit and capacitance. Here, a 0.1 uF capacitor is used.


Software components:


6.SSH Terminal:

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


7.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:

In this section, let's see the connections made to the interface #Bluetooth module to the Raspberrypi device.


What goes where?

  • The Blue line connects one terminal of the LDR sensor to GPIO 17 pin of the raspberry pi module. The other end of the sensor is connected to one end of the capacitor.

  • The orange line connects this end to GPIO 27 pin of the raspberry pi.

  • Another end of the capacitor is connected by the Black wire to the ground (GND) pin of the pi module.


Now, we shall see the interface of LEDs with the raspberry pi. The anode of LED 1, LED 2, LED 3 are first connected to the resistors to avoid damage of the board and LED at any circumstances. Then the same end is connected to the GPIO 18, 15, and 16 pins respectively. The cathode of all three LEDs is connected to the overall ground.


After completing the hardware connections and the app configurations, the circuit is connected to the power supply.


Code:

For GPIO pins set up and output declaration RPi. GPIO library is imported. The next command imports the LDR values from the file. To read the virtual button's value on the app to the raspberry pi, the Blynklib library is imported. To use some system-specific parameters sys library is imported.

import RPi.GPIO as GPIO
from ldr_pr import LDR
import BlynkLib
from blynkapi import Blynk
import sys


Whenever we create a new project in the Blynk app, an e-mail is sent with the project name and the authentication code. That is implemented in the code for security purposes. Then the virtual buttons are allocated with the respective LEDs as mentioned above.

BLYNK_AUTH = 'QR3RkZ_bA7ieCMqtOTZrArhFR8IWV5ud'

LED_1 = Blynk(BLYNK_AUTH, pin = "V5")
LED_2 = Blynk(BLYNK_AUTH, pin = "V7")
LED_3 = Blynk(BLYNK_AUTH, pin = "V6")
All_LED = Blynk(BLYNK_AUTH, pin = "V3")
DEC = Blynk(BLYNK_AUTH, pin = "V8")

blynk = BlynkLib.Blynk(BLYNK_AUTH)


In @blynk.VIRTUAL_WRITE(1) function, commands required to execute the smart mode are initiated. We set up the GPIO pins to the corresponding LEDs and the warnings are meant to be false. The pins are declared as output pins. Inside the loop, we are taking the values recorded by the LDR sensor. Take 5 values and the average is calculated. The calculated average value is then assigned to a variable called p_avg. Then it is compared with the default threshold value. If it is greater than the threshold value, all three LEDs should be ON. If it is lesser, all the LEDs are meant to be in the OFF state.

@blynk.VIRTUAL_WRITE(1)

def my_write():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    p=0
    for i in range(0,5):
        p=p+LDR(11,13)
    p_avg = p/5
    blynk.virtual_write(2, '{:.2f}'.format(p_avg))
        #print(p_avg)
    if p_avg > 500:
        GPIO.output(18, GPIO.HIGH)
        GPIO.output(15,GPIO.HIGH)
        GPIO.output(16,GPIO.HIGH)
        value=1
        blynk.virtual_write(3, value)

    else:
        GPIO.output(18, GPIO.LOW)
        GPIO.output(15,GPIO.LOW)
        GPIO.output(16,GPIO.LOW)
        value=0
        blynk.virtual_write(3, value)




In @blynk.VIRTUAL_READ(4) function, commands required to execute the manual mode are initiated. Similarly, GPIO pins are initiated as mentioned above. The get_val() function, assigns specific values for each LED in the Blynk app. For LED 1, if the value is 1 the GPIO pin is in the high state. If not, it should be a low state. Similar authentication is followed for the other 2 LEDs.

@blynk.VIRTUAL_READ(4)
def my_read():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    first_led=LED_1.get_val()
    second_led=LED_2.get_val()
    third_led=LED_3.get_val()
    ld_1=int(float(first_led[0]))
    ld_2=int(float(second_led[0]))
    ld_3=int(float(third_led[0]))
    if ld_1==1:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18,GPIO.LOW)

    if ld_2==1:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15,GPIO.LOW)

    if ld_3==1:
        GPIO.output(16, GPIO.HIGH)
    else:
        GPIO.output(16,GPIO.LOW)



Inside the try function, the mode on which the LEDs should be controlled is determined. If the value is 0, then the smart mode is activated. Otherwise, it is assigned as manual mode. the program gets ended if any keyboard interrupt occurs.


import RPi.GPIO as GPIO
from ldr_pr import LDR
import BlynkLib
from blynkapi import Blynk
import sys

BLYNK_AUTH = 'QR3RkZ_bA7ieCMqtOTZrArhFR8IWV5ud'

LED_1 = Blynk(BLYNK_AUTH, pin = "V5")
LED_2 = Blynk(BLYNK_AUTH, pin = "V7")
LED_3 = Blynk(BLYNK_AUTH, pin = "V6")
All_LED = Blynk(BLYNK_AUTH, pin = "V3")
DEC = Blynk(BLYNK_AUTH, pin = "V8")

blynk = BlynkLib.Blynk(BLYNK_AUTH)

@blynk.VIRTUAL_WRITE(1)

def my_write():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    p=0
    for i in range(0,5):
        p=p+LDR(11,13)
    p_avg = p/5
    blynk.virtual_write(2, '{:.2f}'.format(p_avg))
        #print(p_avg)
    if p_avg > 500:
        GPIO.output(18, GPIO.HIGH)
        GPIO.output(15,GPIO.HIGH)
        GPIO.output(16,GPIO.HIGH)
        value=1
        blynk.virtual_write(3, value)

    else:
        GPIO.output(18, GPIO.LOW)
        GPIO.output(15,GPIO.LOW)
        GPIO.output(16,GPIO.LOW)
        value=0
        blynk.virtual_write(3, value)


@blynk.VIRTUAL_READ(4)
def my_read():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(18,GPIO.OUT)
    GPIO.setup(15,GPIO.OUT)
    GPIO.setup(16,GPIO.OUT)
    first_led=LED_1.get_val()
    second_led=LED_2.get_val()
    third_led=LED_3.get_val()
    ld_1=int(float(first_led[0]))
    ld_2=int(float(second_led[0]))
    ld_3=int(float(third_led[0]))
    if ld_1==1:
        GPIO.output(18, GPIO.HIGH)
    else:
        GPIO.output(18,GPIO.LOW)

    if ld_2==1:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15,GPIO.LOW)

    if ld_3==1:
        GPIO.output(16, GPIO.HIGH)
    else:
        GPIO.output(16,GPIO.LOW)

try: 
    while True:
        dc=DEC.get_val()
        dec=int(float(dc[0]))
        if dec==0:
            my_write()

        else:
            my_read()
        blynk.run()

except KeyboardInterrupt:
    sys.exit(0)

Download the code here.

ldr_blynk (Main)
.txt
Download TXT • 2KB




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


Working:

We have to configure our Blynk app with respective buttons to show the output. Create a new project and select the device raspberry pi 3. Enter the value display settings and allocate virtual buttons for each LED. The virtual buttons are used to display the data on the app.

  • The V5 button is for LED 1.

  • The V7 button is for LED 2.

  • The V6 button is for LED 3.

  • V8 button is to select either smart mode or the manual mode.

In Smart mode, the LEDs get ON once the resistance value is above the default threshold value. Here, the threshold value is 500 ohm. In manual mode, irrespective of the threshold value the ON/OFF of the LEDs is determined manually.


After the execution of code, now let us check the working of the circuit. Now the setup is in the manual mode.

When the virtual button for LED 1 is ON, the corresponding output is,


Similarly, when the virtual button for LED 2 is ON, the output is:


When all the three virtual buttons are ON, the output is:


We can also perform operations such as all buttons are OFF, only 2 buttons in ON state, and obtain the respective output. Now the setup is switched to the smart mode. At first, all three LEDs are glowing because the LDR value is above the threshold value (500 ohms).


When the mobile flashlight is on, the LDR value starts decreasing. At some point, it gets below the default threshold value and all the LEDs get OFF.


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 a mobile application, we can understand the concept of controlling our smart home appliances from workplaces. Energy conservation is a big advantage of this theme.


See also:

  1. Article on an interfacing LCD display with raspberry pi

  2. Smart lighting system- using LEDs, LDR and raspberry pi

  3. Interfacing a Buzzer with Arduino Board


64 views
  • Facebook
  • YouTube
  • Instagram
  • LinkedIn
LearnElectronics
Contact Us
© Copyright - LearnElectronics 2021