Introduction: Smart Light + Touchless Switch?
How will homes in 2050 look like? No one can predict the future, but each has his/her own idea (s). If I'm right, you would want to have houses that are automated. In order to achieve something great, you will probably start with something small. Maybe can this Instructable help you.
What do you guys think of the combination of a touchless switch and a clever lighting? Wouldn't it be great? You must pay less electricity for lighting.
Step 1: What Do We Need?
As I have said, we are going to start with something small, a model so. To make this model, you need this stuff:
-Any type of Arduino board with a breadboard
-LEDs, they are just like the lamp(s) in your house
-A resistor of 10k Ohm.
-A LDR = light sensor
-A N-channel MOSFET (You can use P-channel MOSFET, but it will have an effect on the circuit that you are going to make next step)
-A simple distance sensor HC-SR04
Step 2: Electric Circuit
As you can see, the picture above is the electric circuit of this model. A distance sensor has 4 pins: VCC, TRIG, ECHO en GND. You connect the VCC with the 5V pin of the Arduino board, GND with GND, TRIG and ECHO with 2 of the digital ports, except port 0 and 1 (these ports are only for exchanging data between Arduino and/or the computer).
This MOSFET has 3 pins: GATE, DRAIN and SOURCE (from left to right). You connect the GATE with a PWM pin, DRAIN with the cathode of your LED and the SOURCE with the ground. If you are using a P-channel MOSFET, you have to change the DRAIN and the SOURCE from their place.
Step 3: Code
This code is based on the code that mr. NguyenN90 has made.
"For some of you who don't understand the code, there are two parts (in 'void loop') in my code:
-The first part is about the distance sensor. To measure the distance, the sensor sends and receives echo (kind of sound). I've used the length of an A4 sheet (29.7 cm - 11.69") to calibrate it. In this calculation "distance = (duration / 2) / 29.7", I've divided the duration in 2 because the time that the echo needs to go from this side of the sheet to another side of the same sheet, is the same as the echo goes from another side to the sensor (it is placed at this side of the sheet).
-The second part is the main part of this code. This is the structure of my code:
If your hand (or an object) is between 2 and 10 cm (0.79"-3.94", you can change this range in the program), the program will enter the first "While" and the LED (or something else) will turn on. And when you move your hand further than 10 cm (the 2nd "While"), the LED still have to turn on (like a normal switch). If your hand is between 2 and 10 cm for the 2nd time (3rd "While"), the LED has to turn off. And when you move it away from the sensor (>10 cm, 4th "While"), the LED still have to turn off until you move your hand closer than 10 cm again."
What I just want to add is about the lighting:
-The Arduino reads the analog value (from 0 to 1023) of the sensor on port A0. I've set the maximum value that it's efficient for the light intensity in a room, is about 500 (you can change this value). Why I've done that? Because I want to use the combination between sunlight (if there is) and the light of the lamp(s). So, the light intensity of your lamp(s) is/are dependent on the light intensity that there is in your room. The formula for that is:
pwm_led = (500 - analogRead(LDR)) / 500 * 255 with LDR is the light sensor.
-analogWrite(led, pwm_led). The "analogWrite" can only work on the digital pins and not on the analog pins. "led" is the name of the PWM-pin (a digital pin) that you have chosen (I've chosen pin 3) and pwm_led is the PWM value that you can calculate it with the formula above.
Step 4: And the Result Is...
Normally it should work, otherwise you have made a mistake...If you like this instructable, please click vote button and vote!