Let the devices sense!

Ok, I admit I need to stop my cheesy puns. But today, we are going to learn a boatload of things. As promised, we are building our control system via our phones, and then we will build around sensors, hopefully giving us a base to develop our projects.

For controlling the LED through the phone, the code in the Nodemcu connection with wifi is the same, but we will change the loop with the following:

while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  server.begin();
}

void loop() {
  WiFiClient client = server.available(); 

  if (client) {
    Serial.println("New client");

    String request = client.readStringUntil('\r');
    Serial.println(request);

    if (request.indexOf("/on") != -1) {
      digitalWrite(ledPin, HIGH);
    } else if (request.indexOf("/off") != -1) {
      digitalWrite(ledPin, LOW);
    }

After the modifications and connecting your nodemcu to your Arduino ide, the following will be the output.

First make sure your connections are apt and the entire board is connected to the laptop.

Then connect your Arduino board to the ide and start your serial monitor.

Then, input the IP address shown on the serial monitor to your Chrome search bar. And then the LED light will glow. You can also prepare a simple web page on and off your led through your phone. Just add the below code, which prepares the HTTP response

client.println("HTTP/1.1 200 OK");
    client.println("Content-type:text/html");
    client.println();
    client.println("<!DOCTYPE html><html>");
    client.println("<head><title>ESP8266 LED Control</title></head>");
    client.println("<body>");
    client.println("<h1>LED Control</h1>");
    client.println("<button onclick=\"location.href='/on'\">on karo</button>");
    client.println("<button onclick=\"location.href='/off'\">off karo</button>");
    client.println("</body></html>");

Lastly for today lets use the IR sensor and Lidar.

IR Sensor

An Infrared (IR) sensor is a device that uses infrared light to sense its surroundings. Here’s how it works:

  • Emission: An IR LED sends out infrared light.

  • Detection: An IR photodiode or phototransistor picks up the infrared light that bounces back from an object.

  • IR sensor

    Connecting an IR Sensor to Arduino

    Components Needed:

    • Arduino board

    • IR sensor module

    • Jumper wires

Steps:

  1. Connect the IR sensor's VCC pin to the Arduino's 5V pin.

  2. Connect the IR sensor's GND pin to the Arduino's GND pin.

  3. Connect the IR sensor's OUT pin to a digital pin on the Arduino.

    const int irSensorPin = D2; 
    const int ledPin = D4;      // defining const at 2 and 4 pin

    void setup() {

      Serial.begin(9600);
      pinMode(ledPin, OUTPUT);
      pinMode(irSensorPin, INPUT);// less data and less frequency, taking i/p from ir sensor from the real world interacivity to control
    }

    void loop() {
      int irSensorValue = digitalRead(irSensorPin); //write to set on and off but read means reading from the sensor

      Serial.println(irSensorValue);

      if (irSensorValue == LOW) {
        digitalWrite(ledPin, HIGH); // if ir sensr is low then turn else viceversa
      } else {
        digitalWrite(ledPin, LOW);
      }

      delay(100);// to detect
    }

Repeat the same steps we did to connect the board to the system and check the output.

  • You can also create an ui/ux to control the sensor. To the code for the led replace it with IR and add the following code snippet before the http response part. For more ui and ux you can use blink app and arduino iot.

  •   int irSensorValue = digitalRead(irSensorPin);
          String irMessage = irSensorValue == LOW ? "Someone is at your door" : "No one is here";
    

    LiDAR (Light Detection and Ranging)

  • It uses laser light to measure distances by timing how long it takes for the light to bounce back from an object. It's used in self-driving cars, mapping, forestry, and environmental monitoring.

  • Components Needed:

    • Arduino board (e.g., Arduino Uno)

    • LDR (Light Dependent Resistor)

    • LED

    • Jumper wires

    • Breadboard

      Connect one end of the LDR to 5V, the other to A0 to GND. Connect LED's anode to D4 through a 220-ohm resistor, cathode to GND. Use the below code. Below is also the output.

    const int ldrPin = D2;   
    const int ledPin = D4;    

    void setup() {

      Serial.begin(9600);//single sensor
      pinMode(ledPin, OUTPUT);// declaration
    }

    void loop() {

      int ldrValue = analogRead(ldrPin);// analog input


      Serial.println(ldrValue);// print


      int brightness = map(ldrValue, 0, 1023, 0, 255);// asking the maping, 0-255 is the range of analog values

      analogWrite(ledPin, brightness);// defining the value to change the brightness

      delay(100);
    }

This is it for today. Let's hope to build more projects using these fundamentals. Till then, tech talk time ticks! Code connections click!