Ahmed Hamdy
Published © GPL3+

Arduino and AC Devices - Automatic Lights

Learn how to use Relay, Ultrasonic sensor and Arduino in one cool and easy project

BeginnerFull instructions provided1 hour32,123
Arduino and AC Devices - Automatic Lights

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Energy Saving Lamp
×1
5v DC Adapter
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Relay (generic)
×1
UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1

Software apps and online services

Visual Studio 2015
Microsoft Visual Studio 2015
Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Screwdriver

Story

Read more

Schematics

Full circuit

This is the full circuit describing the whole project

Code

Smart House Light

Arduino
This code takes the input reading from the ultrasonic and controls the relay accordingly
#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. [this is an arbitrary number]

#define RELAY_LINE1_PIN 8

#include "NewPing.h"
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.


unsigned int critical_distance_cms = 50;  // Cutoff distance at which the light will switch [this is an arbitrary number]
bool state = 0;

void setup() {
  Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
  pinMode(RELAY_LINE1_PIN, OUTPUT);
  digitalWrite(RELAY_LINE1_PIN, HIGH);  // Turn the light off
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int distance = readDistance(); // Current distance of any object facing the ultrasonic sensor

  Serial.print("Ultrasonic: ");
  Serial.print(distance); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

  // Someone is near the door
  if (distance < critical_distance_cms)
  {
    while (distance < critical_distance_cms)
    {
      // Check if they moved away
      distance = readDistance();

      delay(5); // Do nothing until the person moves away from the door
    }

    state = !state; // Change the state of the relay

    if (state)
    {
      Serial.println("Door Open!");
      digitalWrite(RELAY_LINE1_PIN, LOW); // Turn the light on
    }
    else
    {
      Serial.println("Door Closed!");
      digitalWrite(RELAY_LINE1_PIN, HIGH);  // Turn the light off
    }
  }
}

// Updates the value of the Ultrasonic reading
unsigned int readDistance()
{
  // Read 7 values from the ultrasonic and get the median value ( median filter )
  // Gets rid of noisy reading
  unsigned int distance = sonar.convert_cm(sonar.ping_median(7));

  // The value 0 indicates that the ultrasonic sensor is reading nothing in front of it
  // Set this distance to max distance so the light doesn't switch unnecessarily
  if (distance == 0)
  {
    distance = MAX_DISTANCE;
  }
  
  return distance;
}

Ultrasonic test

Arduino
Code to make the ultrasonic prints the value it's reading
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
	Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
	delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
	Serial.print("Ping: ");
	Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
	Serial.println("cm");
}

Smart House Lights

This is the full project for Visual Studio with the edited library

Credits

Ahmed Hamdy

Ahmed Hamdy

7 projects • 108 followers
I Love programmable stuff. Don't hesitate to send me an e-mail >> ahmedhamdyau@gmail.com

Comments