Intel Edison IoT: Automatic Lamp (Relay, HC-SR04)

by mkarvonen in Circuits > Arduino

7223 Views, 64 Favorites, 0 Comments

Intel Edison IoT: Automatic Lamp (Relay, HC-SR04)

asd.jpg
Intel Edison IoT: Automatic lamp (Relay, HC-SR04)

The project is simply a automatic lamp that turns on when it "sees" movement. As this is home automation.

With this you can:

1. Save power.

2. Save your own time.

3. Ensure safe moving with hands full of stuff since the light turns on automatically after entering the room.

So let's start building---->

Components

DSCN0709.JPG

The board used is Intel Edison with Arduino breakout kit.

On the kit there is simple shield board.

Then you will need few meters of CAT cable, LCD screen, HC-SR04 ultrasound sensor, a relay, a lamp (I used LED cable. The same one as i used here.) and of course connection cables to the shield.

Relay is used because it can handle much more current and voltage than an Arduino board can handle.

So it is safe to use even with high currents and high AC Voltage without burning the Arduino board.

In the picture you can see a halogen lamp that can also be used. The LED cable was much more convenient in this project. Also it consumes a lot less power.

Coding

Code.png

Connect the board to the PC and start writing the code.

First you must declare and define the used components and library's and global variables

#define echoPin 7
#define trigPin 8 #define LEDPin 13 #include #include "rgb_lcd.h"

const int relayPin = 8;

rgb_lcd lcd;

const int colorR = 255; const int colorG = 255; const int colorB = 255;

int maximumRange = 400; int minimumRange = 0; long duration, distance;

Then start working with the void setup.

void setup() {
Serial.begin (115200); pinMode(relayPin, OUTPUT);

lcd.begin(16, 2); lcd.setRGB(colorR, colorG, colorB); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LEDPin, OUTPUT); }

After the setup comes the main program in to the void loop.

by chancing the if(distance <100) to something else the ultrasound sensor can be calibrated to the room in use. That means if the distance is less than 100 cm turn the light on.

void loop() {
digitalWrite(trigPin, LOW); delayMicroseconds(2);

digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration/58.2;

lcd.clear(); if (distance >= maximumRange || distance <= minimumRange){

Serial.println("Out of range"); lcd.setCursor(0,0); lcd.print("Out of range"); digitalWrite(LEDPin, HIGH);

}

else {

Serial.println(distance); lcd.print(distance); lcd.setCursor(3,0); lcd.print("Cm"); digitalWrite(LEDPin, LOW);

} if(distance < 100) { lcd.clear(); digitalWrite(relayPin, HIGH); lcd.setCursor(0,0); lcd.print("WELCOME HOME!"); Serial.println("Welcome Home!"); delay(30000);

} else { digitalWrite(relayPin, LOW); } delay(200); }

You can download the whole code from the end of the project.

Testing the Prototype

DSCN0713.JPG
DSCN0747.JPG
DSCN0714.JPG
DSCN0718.JPG
DSCN0715.JPG
DSCN0716.JPG
DSCN0717.JPG
DSCN0719.JPG
DSCN0732.JPG

In prototype phase the relay is conductive when the distance is less than 20 cm.

The distance has to be applied to the used room after the installation and calculations are done.

In example IF room height is 300 cm the used distance can be about 200 cm. But more about that later.

Also the time that the lamp is on is 30 seconds witch is enough for my usage.

Build Cables for the LED and HC-SR04

DSCN0722.JPG
DSCN0723.JPG
DSCN0724.JPG
DSCN0726.JPG
DSCN0728.JPG
DSCN0729.JPG
DSCN0737.JPG
DSCN0738.JPG
DSCN0739.JPG
DSCN0742.JPG
DSCN0744.JPG

The cable of choice is standard Ethernet CAT-6 cable. Mostly because inside the cable there is 4 pairs of cables inside. Making the cable best to use since i will need many cables in compact pack.

Strip the cable(s) and remove one pair of them, since they are not needed for this project.

Then solder the shield connector to the other end of the cable. This connector is used for the HC-SR04 ultrasound sensor. Keep in mind the cable order since you will need Vcc +5 (Red to Blue/white) and GND (Black to blue) and the Trigger(White to orange/white) and Echo (Yellow to orange).

The Green and Green/white will be for the Led strip.

With the relay i will be driving the GND for the LED strip.

Installing the Lights and Ultrasound Sensor to Ceeling

DSCN0749.JPG
DSCN0750.JPG
DSCN0751.JPG
DSCN0753.JPG
DSCN0754.JPG
DSCN0755.JPG
DSCN0756.JPG
DSCN0757.JPG
DSCN0759.JPG

After soldering the cables ready i just took a glue pistol and glued it to the ceiling. I would use cable hooks but i was out of them. The glue works fine and it wont leave any marks on it when removed, witch is important as i'm living on a rented house.

Remember to put the ultrasound sensor at the right angle to ensure "long" readings as possible.

Final Coding and Testing

DSCN0760.JPG
DSCN0761.JPG
DSCN0764.JPG
DSCN0766.JPG
DSCN0767.JPG
DSCN0768.JPG
DSCN0769.JPG
DSCN0763.JPG

Final coding is done when the whole system is in its place. This is a tricky one if the room is small and full of coats and shoes like i had. The ultrasound sensor gives a "faulty" reading that the floor is closer than it actually is. The roof is about 3 meters high and the sensor shows that the height is about 179 cm. This is caused by all the coats and shoes in the room. But i can live with that. The code was set to 100 cm, witch means that if there is object closer than 100 cm the relay will switch in conductive mode and the light will burn. This setup works fine in my case. Every time when the light burns the Intel Edison will welcome you home. The whole board is hidden in the closet except the LCD screen witch is readable in entry.

Done.

DSCN0771.JPG
DSCN0773.JPG

After testing and final tuning for the code is done the only thing that is left is to let it hang and use the lamp. The lamp is ideal for bathroom or for hallway that is not in active use. Like in the example i did.

The lamp is set to burn for 30 seconds after it recognizes movement. The time can be easily changed to everything else. I found that the 30 seconds is enough to get coat off and out of the room. This saves a lot of energy since the lamp is not burning all the time and most importantly if your hands are full of stuff you don't have to reach for the switch just to see something.

Thank you for reading and if you like my project's start following me and get the latest projects first!

Downloads