list of products from Amazon (Not mine, i just link them!) Scroll Down For Article! Note: Elegoo boards are not Arduino, but they are compatible with Arduino IDE and are mutch more affordable!
This Arduino code implements a simple motion detection system using a sensor connected to digital pin 2 and an LED connected to pin 13. The program initializes the LED as an output and the sensor as an input, allowing it to read motion status. When motion is detected (the sensor state is HIGH), the LED lights up, and a message is printed to the Serial Monitor indicating that motion has been detected. If motion stops (the sensor state is LOW), the LED turns off, and a message is printed to indicate that motion has ceased.
The code uses a variable state to track the current motion status, ensuring that messages are printed only when the state changes. This provides a clear indication of motion events without flooding the Serial Monitor with repeated messages during continuous motion detection.
This project is an excellent introduction to using Arduino for motion detection and LED control, making it suitable for beginners interested in basic electronics and programming.
SCHEMATIC:
CODE:
int led = 13; // the pin that the LED is atteched to int sensor = 2; // the pin that the sensor is atteched to int state = LOW; // by default, no motion detected int val = 0; // variable to store the sensor status (value)
void setup() { pinMode(led, OUTPUT); // initalize LED as an output pinMode(sensor, INPUT); // initialize sensor as an input Serial.begin(9600); // initialize serial }
void loop(){ val = digitalRead(sensor); // read sensor value if (val == HIGH) { // check if the sensor is HIGH digitalWrite(led, HIGH); // turn LED ON delay(500); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
} else { digitalWrite(led, LOW); // turn LED OFF delay(500); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
Car Windshield Sun Shade Umbrella, Foldable Windshield Sunshade Front Cover with UV Block, 360°Rotation Bendable Front Window Heat Insulation Protection for Auto Windshield Covers - 55"*31.5"
Car Windshield Sun Shade - Foldable Reflective Sun Visor for Cars, SUVs, Trucks, and Vehicles | Heat Blocking Sunshade to Keep Interior Cool | Summer Automotive Accessories
0 Comments