Introduction: Arduino Ac Light With Hc Sensor
An Arduino project we did for school.
We managed to put a sensor on the AC light, when you are coming in a range of 3meters the lamp wil go on.
It was a very nice project to do and we learnt a lot of it.
Step 1: Things You Need
Packlist:
Arduino
lamp
sensor
relayt module
Step 1:
Connect the relayt module onto the Arduino.
Step 2:
Connect the relayt module and the sensor
Step 3:
Connect the ac light to the relayt module and put some 230 volt on it.
Step 4:
Run the code, its in the .pdf file. Try to read out the code and find a good spot for the sensor to control the relayt module.
Code:
#define RELAY1 10
int trigger = 7;
int echo = 8;
void setup() {
pinMode(10, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigger, LOW);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
long duration = pulseIn(echo, HIGH);
long r = 3.4 * duration / 2;
float distance = r / 100;
Serial.print(“duration:”);
Serial.print(duration);
Serial.print(“ , distance=”);
Serial.print(distance);
Serial.println();
if (distance > 180) {
digitalWrite(RELAY1, 0);
} else {
digitalWrite(RELAY1, 1);
}
}