Introduction: 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---->
Step 1: Components
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.
Step 2: Coding
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.
Step 3: Testing the Prototype
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.
Step 4: Build Cables for the LED and HC-SR04
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.
Step 5: Installing the Lights and Ultrasound Sensor to Ceeling
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.
Step 6: Final Coding and Testing
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.
Step 7: Done.
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!