• LearnElectronics

Smart Lighting System using ThingSpeak IoT Cloud and Raspberry Pi

Updated: Nov 8, 2020

This article will teach you how to how to interface a Fire Alert to Gmail using Raspberry Pi. Smart Lighting is a #technology where the system can make a decision on its own and can turn the lighting on/off, change colors, etc using voice recognition, #sensors mobile app, and many other condition-based technologies. Let us not wait anymore and jump into the circuit right away.


1. Raspberry Pi: Raspberry Pi is a single-board microcomputer which utilizes the SoC, DDR RAM #memory, ethernet port, USB host, and micro HDMI on it. It provides the possibility to interact with the outside world and using its processing power in a compact board at a low cost. In this application, we have used the Python IDE to simplify it.

2. SD Card for Raspberry Pi: It is important that we use the right MicroSD for storing and installing the Pi in the system. It is always recommended to use a class-10 SDcard which is capable of storing all the information regarding the #RaspberryPi from installation to application.

5. USB Cable for Raspberry Pi: This cord is used to temporarily connect the Raspberry Pi to the #power supply.

4. Connecting Wires (Male-Female): As simple as it looks, these are used to connect ends without soldering. While male wired have protruding pins to plug into things, female jumpers are the ends that are used to plug into.

5. LDR: An LDR or a Light Dependent Resistor is a sensor that varies the resistance of the component depending on the intensity of the light that falls upon its surface. This is one of the most important elements that we need in a #smartsystem and we will see this working in detail.

6. Ceramic Capacitor: The ceramic capacitor is a fixed value capacitor that can store #energy and it called so as it is made using ceramic material. The metal layers in this component act as electrodes and are made using two layers of ceramic.

7. Breadboard: This is the surface where all the components are connected and help to connect any basic circuit without soldering.


The circuit as shown below has an LDR which is connected to one of the pins of the capacitor. This is used for drawing the resistance values based on the time-constant concept. The other end is connected to the ground. The point where the LDR and capacitor are getting connected together is known as a junction. This junction is connected to the Raspberry at GPIO 27 and the other end is connected to GPIO 17.

The LEDs used in this circuit represent the light bulbs at our homes and are connected to GPIOs 18, 15, and 16 as per the board naming. The GPIOs are correspondingly connected to the resistors which are connected to anode and cathode is connected to the ground.

The next task is connecting all this to the cloud, sync the data, and get widget-based visuals. In order to configure the ThingSpeak cloud, we need to sync the data in the server and get real-time analysis and widget-based visuals. By doing this, the setup can be made and seen on a software basis.

Setting up the cloud: By going to the ThingSpeak website, we can understand the basics behind this technology and use it for prototyping and proof of concepts, mainly in the field of IoT and Analytics.

Sign in or Sign up according to your login credentials and set up a new channel and fill the fields according to your preference. Once the setup is done, the channel is all yours.

All the widgets necessary for this particular for this project have already been added, but you always have the freedom to add the ones that wish to and modify the widgets depending on the purpose.

The LDR values have been set up, display settings, and fields modified. The units, intervals, and range are given according to the necessity of the hour. The LED status fields can be enabled completely according to your wish as there are so many options to choose from according to your preferences.

Once this is all complete, the program can be run and you are good to go with the channel.

Hardware Implementation:

The LDR sensor is connected to pins 17 and 27 along with board pins 11 and 13. The circuit also consists of a 0.1 uF capacitor and LEDs all connected to the Raspberry.

After completing this step, if we go to the terminal and run the program, we will be able to see that raspberry is running and the gauge is going up; the LED status will be switched to HIGH.


The main program consists of setting up the pins, importing resistance values from the LDR, and importing system-specific data. The next step deals with the URL and importing time for delay.

The next commands are for accessing the ThingSpeak account and add the basic URL along with the API connected to the URL to project the data constantly.

The loop sets the board, sets up a warning, and connects onto pins 18, 15, and 16. The condition checks if the average is greater than 500 and then turns the light on thereby indicating the status. This value of LED is updated in the cloud thereafter. Similarly, the else condition makes sure that whenever there is sufficient light, the LEDs stay OFF to save electricity.

import RPi.GPIO as GPIO
from ldr_pr import LDR
import sys
import urllib2
from time import sleep

# Enter Your API key here
myAPI = 'Your-Write-API-Key' 
# URL where we will send the data, Don't change it
baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI 

    while True:
        for i in range(0,5):
        p_avg = p/5

        if p_avg > 500:
            GPIO.output(18, GPIO.HIGH)
            GPIO.output(15, GPIO.HIGH)
            GPIO.output(16, GPIO.HIGH)
            conn = urllib2.urlopen(baseURL + '&field1=%s&field2=%s&field3=%s&field4=%s&field5=%d' % (p_avg, value_1, value_2, value_3, value))

            GPIO.output(18, GPIO.LOW)
            GPIO.output(15, GPIO.LOW)
            GPIO.output(16, GPIO.LOW)
            conn = urllib2.urlopen(baseURL + '&field1=%s&field2=%s&field3=%s&field4=%s&field5=%d' % (p_avg, value_1, value_2, value_3, value))
except KeyboardInterrupt:

Download the code from here.

ldr_ts (Main)


  1. Want to make a Smart Lighting System with 3 LEDs using HC-05, RPi, and mobile? Join us!

  2. Smart Lighting System