Most of the places have automatic street light which can sense the daytime and nighttime, and automatically turns on and off according the night and day. Here we are extending this project by adding one more constraint to turn on the light that is Street light will only glow if there is darkness and someone is passing through the street. The main objective of this project is to reduce the power consumption by glowing the Street light only when it is needed. In this project we are demonstrating the prototype of the Smart Street Light with 3 IR sensors, 1 LDR sensor and 3 LEDs - each representing one street light. We will also update the LDR sensor data to the ThingSpeak and control the LEDs (Street lights) over the internet from anywhere in the world.
Components Required
- ESP8266 NodeMCU
- Micro USB cable
- LEDs
- Jumper wires
- IR sensors
- LDR sensors
Before moving forward, lets learn about the components used in the project.
ESP8266 NodeMCU
ESP8266 NodeMCU is an open source IoT platform. It includes firmware which runs on the low cost Wi-Fi enabled ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which is based on the ESP-12 module. It has GPIO, SPI, I2C, ADC, PWM AND UART pins for communication and controlling other peripherals attached to it. On board NodeMCU has CP2102 IC which provides USB to TTL functionality.
IR Sensor
IR sensor is an electronic device which is used to detect objects by sensing infrared radiations reflected from the objects. It mainly consists of a transmitter IR LED and a receiver photodiode. It detects infrared radiations which have wavelength from 700nm to 1mm. When a specific positive voltage is applied across the transmitter LED it transmits the IR rays. If these rays fall on some object then that object reflects back the IR rays which are received by the receiver photodiode. The receiver diode generates a voltage across its terminals which depend on the intensity of light reflected by the object. Generally IR receiver LED is darker (black) whereas transmitter is transparent in color.
LDR Sensor
LDR stands for Light Dependent Resistor also known as photo-resistor. LDR is sensitive to light and its resistance changes according to the intensity of light falling on it. It is made up of high resistance semiconductor and its resistance increases in darkness and decreases in light. When light incident on the LDR exceeds some threshold, it absorbs the photons and allows electrons to jump into the conduction band. LDR generates a variable resistance which depends on the intensity of light falling on it. It is mainly used in electric circuits like street light, alarm clock, automatic brightness and contrast control etc.
Thingspeak
ThingSpeak is a cloud based data platform which is used to send and receive the data in real time using HTTP protocol. It is used in Iot applicaiotn to store and monitor the data from anywhere in the world over internet. Here are some Iot projects where ThingSpeak is used as an IoT cloud:
- IoT Wireless Weather Station using Arduino, ESP8266 and ThingSpeak
- IoT Based Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266
- IoT Based Raspberry Pi Weather Station using DHT11 and BMP180 Sensor
- IoT Based Temperature and Humidity Monitoring using ThingSpeak and ESP8266
Smart Street Light Circuit Diagram and Working
Circuit diagram for this IoT based Smart Street Light is as follows:
This circuit mainly consists ESP8266, LDR sensor, IR sensors and LEDs.
Here the LDR sensor is used to detect whether it is daytime or night time. Since LDR sensor generates variable resistance based on the amount of light falling on it, it has to be connected like a potentiometer. One end of the LDR sensor is connected to 5V and other end is connected to fixed resistance which is further connected to ground. NodeMCU has one ADC pin (A0) which is connected to point between fixed resistance and one end of the LDR sensor as shown in the circuit diagram. Since the LDR sensor gives variable resistance therefore variable voltage will be generated at A0 according to the amount of light falling on LDR.
IR sensors are used to detect if someone is crossing the street or not. It detects the obstacle or motion in the surrounding. The transmitter will transmit IR rays which will be reflected back if it falls on some object like person, animal, vehicles, etc. The reflected ray will be received by receiver diode and hence will confirm the presence of object and the corresponding LED will be glowed. This method will save significant amount of electricity as the street light will only turns on if there is someone present in the Street. IR sensor has 3 pins, two of which are VCC and ground and one is output pin. The output of IR sensor gets high if detects presence of some object. This pin is connected to GPIO pin of NodeMCU so whenever the IR sensor detects someone passing through the street it triggers the Street light. In our case one LED will be turned on.
Installing ThingSpeak library in Arduino IDE
Open Arduino IDE and Go to Sketch -> Include Library -> Manage Libraries.
Now search for ThingSpeak in library manager and install ThingSpeak library by MathWorks.
Uploading Smart Street Light data on ThingSpeak
Now we will upload the LDR sensor, IR sensors and leds data on the ThingSpeak. Go to https://thingspeak.com/ and start by signing up if you don’t have account on it
Now Click on New Channel and then give some name to your channel and then fill the fields as shown below. Field 1 is for LDR sensor data, Field 2 to Field 4 are for IR sensors, Field 5 to Field 7 are for LEDS. After filling the details scroll down and click on “Save Channel”.
Your channel will be created and now you will be able to see field charts. Now Click on API Keys and copy channel id, read and write API keys and paste them in the Arduino code given at the end of the tutorial. Now upload the Arduino code to the NodeMCU. On successfully uploading, test your project by putting objects in front of IR sensors. You will be able to see the change in the ThingSpeak charts for every change in values of LDR, ir sensors and LEDs.
Controlling the Street Light from anywhere using ThingSpeak
Now we have to control the LEDs over the internet using ThingSpeak. Click on Sharing and select the “Share channel view with everyone” radio button.
Now go to API keys and copy the URL given in “Update a Channel Feed”. We have to edit this URL to change the status of LED.
Fields for our LEDs are 5, 6 and 7. Now edit the Url as shown below:
https://api.thingspeak.com/update?api_key=FZTOUARV558GRZ8J&field5=1&field6=1&field7=1
Here we are setting field 5, field 6 and field 7 as 1 to turn on the LEDs. Copy this URL and paste it in a new tab. It will turn on the LEDs with some delay time. You can observe the change in field charts.
Code Explanation
Complete code with a demonstration Video is given at the end of this tutorial. Code can be uploaded in NodeMCU by using Arduino IDE. Here we are explaining few important part of the code.
First include all the required libraries.
#include <ESP8266WiFi.h>; #include <WiFiClient.h>; #include <ThingSpeak.h>;
Replace SSID and password given in code with you Wi-Fi SSID and password.
const char* ssid = "CircuitLoop"; const char* password = "circuitdigest101";
Copy channel number, read and write API keys from ThingSpeak as shown above.
unsigned long myChannelNumber = 795820; const char * myWriteAPIKey = "FZTOUARV558GRZ8J"; const char * myReadAPIKey = "T52GT3QQOQBVPG4V";
Define variable for GPIO pins of leds and IR sensors, ADC channel
WiFiClient client; int led_1; int led_2; int led_3; int ir1 = D0; int led1 = D5; int ir2 = D1; int led2 = D6; int ir3 = D2; int led3 = D7; int ldr = A0; int val =0;
Set the pinMode for pins of led and IR sensor on the NodeMCU.
pinMode(ir1,INPUT); pinMode(led1,OUTPUT); pinMode(ir2,INPUT); pinMode(led2,OUTPUT); pinMode(ir3,INPUT); pinMode(led3,OUTPUT);
Initialization of Wi-Fi and ThingSpeak
WiFi.begin(ssid, password); ThingSpeak.begin(client);
Now we take digital value of the IR sensors and analog value of LDR sensor and store them in variables.
int s1 = digitalRead(ir1); int s2 = digitalRead(ir2); int s3 = digitalRead(ir3); val = analogRead(ldr);
Now check the value of LDR sensor for low light. Here I have set value as 700 means if the analog value of LDR is lower than 700 then it will be night time or low light and hence it will turn on the led if IR sensors detect some obstacle or motion. If the analog value of the LDR sensor is more than 700 then it will be considered as daytime and LEDs will not glow even if IR sensor detects someone passing the street.
if(val<700) { if(s1==0) { digitalWrite(led1,LOW); } else { digitalWrite(led1,HIGH); } if(s2==0) { digitalWrite(led2,LOW); } else { digitalWrite(led2,HIGH); } if(s3==0) { digitalWrite(led3,LOW); } else { digitalWrite(led3,HIGH); } } else { digitalWrite(led1,LOW); digitalWrite(led2,LOW); digitalWrite(led3,LOW); }
Finally upload the data on the ThingSpeak cloud by using function ThingSpeak.writeField(). It take channel number, field number, data (you want to upload in respective field) and write API key. Here we are uploading LDR sensor data, IR sensors data and LEDs data to the ThingSpeak cloud.
ThingSpeak.writeField(myChannelNumber, 1,val, myWriteAPIKey); ThingSpeak.writeField(myChannelNumber, 2,s1, myWriteAPIKey); ThingSpeak.writeField(myChannelNumber, 3,s2, myWriteAPIKey); ThingSpeak.writeField(myChannelNumber, 4,s3, myWriteAPIKey); ThingSpeak.writeField(myChannelNumber, 5,led1, myWriteAPIKey); ThingSpeak.writeField(myChannelNumber, 6,led2, myWriteAPIKey); ThingSpeak.writeField(myChannelNumber, 7,led3, myWriteAPIKey);
Here is the code for changing the state of LEDs using ThingSpeak. We have already shown above the procedure to change the state of the LEDs. Led_1, led_2, led_3 stores the last state of led from the ThingSpeak using the function ThingSpeak.readIntField which takes channel number, respective field number and read API key. If the state of some led is “1” then we turn on the respective led and if the state of some led is “0” we turn off the respective led.
led_1 = ThingSpeak.readIntField(myChannelNumber, 5, myReadAPIKey); led_2 = ThingSpeak.readIntField(myChannelNumber, 6, myReadAPIKey); led_3 = ThingSpeak.readIntField(myChannelNumber, 7, myReadAPIKey); if(led_1==1) { digitalWrite(led1,HIGH); } else { digitalWrite(led1,LOW); } if(led_2==1) { digitalWrite(led2,HIGH); } else { digitalWrite(led2,LOW); } if(led_3==1) { digitalWrite(led3,HIGH); } else { digitalWrite(led3,LOW); }
This is how a Smart Street Light works, it only glows if it is a night time and someone is passing through the street. And it can be also controlled manually from anywhere in the world using ThingSpeak IoT cloud
Check the complete code with a Demonstration Video below.
#include <ESP8266WiFi.h>;
#include <WiFiClient.h>;
#include <ThingSpeak.h>;
const char* ssid = "CircuitLoop";
const char* password = "circuitdigest101";
WiFiClient client;
unsigned long myChannelNumber = 795820;
const char * myWriteAPIKey = "FZTOUARV558GRZ8J";
const char * myReadAPIKey = "T52GT3QQOQBVPG4V";
int led_1;
int led_2;
int led_3;
int ir1 = D0;
int led1 = D5;
int ir2 = D1;
int led2 = D6;
int ir3 = D2;
int led3 = D7;
int ldr = A0;
int val =0;
void setup() {
Serial.begin(9600);
delay(10);
pinMode(ir1,INPUT);
pinMode(led1,OUTPUT);
pinMode(ir2,INPUT);
pinMode(led2,OUTPUT);
pinMode(ir3,INPUT);
pinMode(led3,OUTPUT);
WiFi.begin(ssid, password);
ThingSpeak.begin(client);
}
void loop() {
int s1 = digitalRead(ir1);
int s2 = digitalRead(ir2);
int s3 = digitalRead(ir3);
s3 = not(s3);
val = analogRead(ldr);
Serial.print(s1);
Serial.print(":");
Serial.print(s2);
Serial.print(":");
Serial.print(s3);
Serial.print(" ");
Serial.println(val);
if(val<800)
{
if(s1==0)
{
digitalWrite(led1,LOW);
}
else
{
digitalWrite(led1,HIGH);
}
if(s2==0)
{
digitalWrite(led2,LOW);
}
else
{
digitalWrite(led2,HIGH);
}
if(s3==0)
{
digitalWrite(led3,LOW);
}
else
{
digitalWrite(led3,HIGH);
}
}
else
{
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
}
ThingSpeak.writeField(myChannelNumber, 1,val, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 2,s1, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 3,s2, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 4,s3, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 5,led1, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 6,led2, myWriteAPIKey);
ThingSpeak.writeField(myChannelNumber, 7,led3, myWriteAPIKey);
led_1 = ThingSpeak.readIntField(myChannelNumber, 5, myReadAPIKey);
led_2 = ThingSpeak.readIntField(myChannelNumber, 6, myReadAPIKey);
led_3 = ThingSpeak.readIntField(myChannelNumber, 7, myReadAPIKey);
if(led_1==1)
{
digitalWrite(led1,HIGH);
}
else
{
digitalWrite(led1,LOW);
}
if(led_2==1)
{
digitalWrite(led2,HIGH);
}
else
{
digitalWrite(led2,LOW);
}
if(led_3==1)
{
digitalWrite(led3,HIGH);
}
else
{
digitalWrite(led3,LOW);
}
}