Motion Sensor with noise and LED for beginners

The purpose of this project was to create something that would alert me when there were people in another room. So if I was upstairs something would let me know if there were people downstairs and vice versa. Therefore, this Arduino makes a noise and lights an LED when motions is detected in front of the sensor. Great project for beginners and people just starting out with using Arduino.

Inspiration from: Arduino with PIR motion Sensor, LED and buzzer (Code)

The materials needed:
Arduino
Breadboard
JumperWires
LED
220 Ohm resistor
Buzzer
PIR motion Sensor

Code:
const int motionpin=A0;
const int ledpin=13;
const int buzzpin=12; // ledpin,motionpin and buzpin are not changed throughout the process
int motionsensvalue=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
pinMode(motionpin,INPUT);
pinMode(buzzpin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
motionsensvalue=analogRead(motionpin); // reads analog data from motion sensor
if (motionsensvalue>=200){
digitalWrite(ledpin,HIGH);
tone(buzzpin,100); //turns on led and buzzer
}
else {
digitalWrite(ledpin,LOW); //turns led off led and buzzer
noTone(buzzpin);
}
}

Code from: Arduino with PIR motion Sensor, LED and buzzer (Code)

ARduino1.png

ARduino1.png

1 Like

I can see no reason for motionsensevalue to have global scope, and camelCase is easier to read, thus “motionSenseValue”.

Pin numbers don’t need to be "int"s; you’re unlikely to need 32767 pins, or negative pin numbers

If you’re doing an analogRead on a pin, a pinMode is unnecessary.

LED_BUILTIN is already defined for you.

Code would be more legible if correctly indented; the IDE’s auto format tool is useful for this.

Please remember to use code tags when posting code