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:
220 Ohm resistor
PIR motion Sensor

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:
pinMode(ledpin, OUTPUT);
void loop() {
// put your main code here, to run repeatedly:
motionsensvalue=analogRead(motionpin); // reads analog data from motion sensor
if (motionsensvalue>=200){
tone(buzzpin,100); //turns on led and buzzer
else {
digitalWrite(ledpin,LOW); //turns led off led and buzzer

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



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

1 Like