Motion Sensor That Lights a Led

Hello, I've recently been to an Arduino Level 1 Workshop and after purchasing a starter kit, I came up with my first project.

I want to use a motion sensor to sense me when I come home at night so that a 3W Led will light up so I can see to unlock the house door.

I've come quite far but I have some questions and I also want any advice on how to improve my code if it can be improved.

My code:

#define SENSOR_PIN 2
#define LED_PIN 3

void setup() {
  pinMode (LED_PIN, OUTPUT);
  pinMode (SENSOR_PIN, INPUT);

}

void loop() {
  int sensorValue = digitalRead (SENSOR_PIN);
  if (sensorValue == HIGH)
  {
    digitalWrite (LED_PIN, HIGH);
    delay(15000);
    digitalWrite (LED_PIN, LOW);
  }
  delay(1000);
}

Pretty simple, lights up the led light for 15 seconds and then waits for a second to read the sensor again to avoid immediate re-triggering.

As for the circuit, I have connected the two +s of the sensor and the 3W led to two 5V ports on the arduino uno board and of course their two -s to two GND ports. I wanted to ask, will I need any resistors? I have some 1k resistors that came in the starter pack. If so, I will need to make it with a breadboard instead of connecting the modules directly on the arduino board, no?

Also, I plan to power it up with a 9v battery. Any ideas of how to mount it on the wall above/near the door? Any protective case to put it in or something?

Thank you in advance!

What is "the sensor"?
lEDs always need serial resistors.
Use pen and paper to make schematics, not words.
Improving code.... Check up the "Blink without delay" topic. Using delay quickly brings the to a dead end.

Please post a data sheet for the 3W LED. 3W at 5V is 600mA. Way way more than an Arduino output pin can supply. 20mA is the recommended maximum current for an output pin. You will need a driver for the LED. Probably a constant current LED driver.

Like this? Those smoke alarm batteries have little current capability. You will need to determine the actual current requirements of the project and buy the correct battery or power supply.

1 Like

Perhaps breadboard (solderless) for development, but for a permanent installation I would make a PC board or wire (solder) to a piece of protoboard. Soldering required for secure connections.

For any battery long term power, you'll have to start thinking about how to spend as little energy as possible whilst waiting for motion triggering.

PIR sensors don't use so much power that they can't assume the roll of the controlling element.

You'll sooner or later need to look into sleep modes for the Arduino board, there is no better place than

which is a slog but has everything including complete sketches for various scenarios.

I always advice ppl to read the entire thing like they know what he is talking about… he does a good job and after the Nth pass through it you will be able to steal borrow perfect code for your sitch.

a7

Why don't you try your code with the White LED that came with the kit.

An HC SR501 PIR Motion Sensor. It's a very simple project, I don't need to make schematics on pen and paper. "Using delay quickly brings the to a dead end"...I don't understand what this means.



Can't find the data sheet of the LED, from where I have bought it from it says "LED 3W High-Power Module".

My code works, the sensor does pick up movement and lights up the led.

OK, that's a bit different than hooking up a 3 watt LED to an Arduino pin.

The module switches the LED power following a digital signal that is easily within the Arduino's limit on current from an output pin.

Sry, we just very literal around these parts.

So you still have to use a better battery, and no matter the battery, you will benefit from power saving modes that are available.

a7

Refer few basic projects for your leaning on Motion sensor like PIR and HC-SR04

Arduino with PIR

# Ultrasonic with Arduino

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.