IoT Automatic Street Light with ThingSpeak. Now we will see how to use this to update the street light data into ThingSpeak cloud. In this project, we will use the LDR to detect the day/night environment and turns on the LED during night time as well as update the data to ThingSpeak. Additionally, we have an IR Sensor to detect any motion and turn on the LED and update that data separately in the ThingSpeak.
Components Required:-
1. NodeMCU
2. Breadboard
3. LED
4. IR Sensor
5. LDR
6. Jumper Wires
7. 220 Ohm and 10K Ohm resistors – 1 each
Circuit Diagram:-

Thingspeak – Adding New Channel:-
Note 1: If you have missed the channel creation process, refer to this previous tutorial. There we have created an account, installed the thingspeak library and set up a single channel example. We are just adding another channel for the IR Sensor.

Note 2: Thingspeak provides only a 15-second interval for updating data to thingspeak. Summing up the network delay slows down the code. If you consider real-world scenario Day/Night won’t change instantly. But for IR motion detection it matters.

Step 1: Login to thingspeak and navigate to My Channels on the dashboard.
Step 2: Now click on the ‘channel settings’. Add a field for IR Sensor.
Step 3: Now if you switch to public view you can see your new field.
Step 4: Now copy the Channel ID and API Key. These two we need to update in the code.

Optional Feature:-
ThingSpeak also has a widget feature that displays a GUI type of visualization according to the field data. Let’s see how to add it.
Step 1: Click the widget button on the dashboard.
Step 2: Choose your favorite widget.
Step 3: Set the configuration values. Here I set for IR Sensor Field.
Step 4: Save and test. When IR Detected, this Light will glow in Green Color.


#include <ESP8266WiFi.h>;
#include <WiFiClient.h>;
#include <ThingSpeak.h>;
const char* ssid = “Your Wi-Fi”; //Your Network SSID
const char* password = “Your Password”; //Your Network Password
int val;
int LDRpin = A0; //LDR Pin Connected at A0 Pin
int IRpin = D2;
int tresholdLDR = 300; //LDR Threshold Value
int LEDpin = D5;
WiFiClient client;
unsigned long myChannelNumber = 123456; //Your Channel Number (Without Brackets)
const char * myWriteAPIKey = “XXXXXXXXXXXXX”; //Your Write API Key
void setup()
pinMode(LDRpin, INPUT);
// Connect to WiFi network
WiFi.begin(ssid, password);
void loop()
int LDRval = analogRead(LDRpin); //Read Analog values from LDR
int IRval = digitalRead(IRpin); //Read IR is detected or not (Digital)
Serial.println(“Dark Light”); //Print on Serial Monitor
ThingSpeak.writeField(myChannelNumber, 1, LDRval, myWriteAPIKey); //Update in ThingSpeak
LDRval = analogRead(LDRpin);
//detects IR and update to ThingSpeak
while(IRval == LOW){ //LOW Signal when motion detected on IR Sensor
ThingSpeak.writeField(myChannelNumber, 2, IRval, myWriteAPIKey); //Update in ThingSpeak
Serial.println(“IR Detected”); //Print on Serial Monitor
IRval = digitalRead(IRpin); //Read IR is detected or not
Serial.println(“Idle”); //Print on Serial Monitor
ThingSpeak.writeField(myChannelNumber, 2, IRval, myWriteAPIKey); //Update Field1 in ThingSpeak
ThingSpeak.writeField(myChannelNumber, 1, LDRval, myWriteAPIKey); //Update Field2 in ThingSpeak

About the Author


Hello! My Dear Friends. I am Subramanian. I am writing posts on androiderode about Electronics testing and equipments.

View All Articles