Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

Motion Sensor - Detecting Movement with Arduino

The Importance and Utility of Motion Sensors

Motion sensors are essential components in various applications, including security systems, automation, robotics, and interactive installations. These sensors can detect movement in their surroundings and provide valuable information for decision-making processes. By using motion sensors, we can create intelligent systems that respond to human presence or movement, enhancing safety, efficiency, and user experience.

Project: Motion-Activated LED Light

In this project, we will create a motion-activated LED light using an Arduino and a motion sensor. The objective is to turn on an LED when motion is detected and turn it off after a certain period of inactivity. This project can be used as a basis for more advanced applications, such as automatic lighting systems or security alarms.

List of Components:

Examples:

Example 1: Basic Motion Detection

int motionPin = 2; // Motion sensor connected to digital pin 2
int ledPin = 13; // LED connected to digital pin 13

void setup() {
  pinMode(motionPin, INPUT); // Set motion sensor pin as input
  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  int motionState = digitalRead(motionPin); // Read motion sensor state

  if (motionState == HIGH) { // If motion is detected
    digitalWrite(ledPin, HIGH); // Turn on LED
    delay(5000); // Wait for 5 seconds
    digitalWrite(ledPin, LOW); // Turn off LED
  }
}

Explanation:

  • We define the motion sensor pin as an input and the LED pin as an output in the setup() function.
  • In the loop() function, we read the state of the motion sensor using digitalRead().
  • If motion is detected (motionState is HIGH), we turn on the LED using digitalWrite(), wait for 5 seconds using delay(), and then turn off the LED.

Example 2: Delayed Motion Detection

int motionPin = 2; // Motion sensor connected to digital pin 2
int ledPin = 13; // LED connected to digital pin 13
int motionDetected = 0; // Flag to track motion detection

void setup() {
  pinMode(motionPin, INPUT); // Set motion sensor pin as input
  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  int motionState = digitalRead(motionPin); // Read motion sensor state

  if (motionState == HIGH) { // If motion is detected
    motionDetected = 1; // Set motion detected flag
    digitalWrite(ledPin, HIGH); // Turn on LED
  }

  if (motionDetected == 1 && motionState == LOW) { // If motion stops
    delay(5000); // Wait for 5 seconds
    motionDetected = 0; // Reset motion detected flag
    digitalWrite(ledPin, LOW); // Turn off LED
  }
}

Explanation:

  • We introduce a flag, motionDetected, to track if motion has been detected.
  • When motion is detected, we set the flag to 1 and turn on the LED.
  • If motion stops (motionState is LOW) and the flag is set, we wait for 5 seconds, reset the flag, and turn off the LED.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.