Skip to content

codeadamca/arduino-motion-sensor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Arduino and a Motion Sensor

A basic example of reacting to the status of a motion sensor using an Arduino and the built-in LED.

Arduino Code

Open up Arduino Create and add the following code:

// Define a variable to store the pin number
int motionPin = 2;

// The setup function runs once when you press reset or power the board
void setup(){

  Serial.begin(9600);
  pinMode(motionPin, INPUT);

  pinMode(LED_BUILTIN, OUTPUT);

}

// The loop function runs over and over again forever
void loop (){

  // Add a delay between each loop
  delay(1000);

  // Fetch the current motion sensor status
  int motionResult = digitalRead(motionPin);

  // Output the current motion sensor result
  Serial.println(motionResult);

  // Turn the bulit-in LED on or off based on the motino sensor status
  if (motionResult == HIGH) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }

}

View the Arduino code on Arduino Create

You will need to setup the following circuit using your Arduino:

Tinkercad Circuit

View the Circuit on Tinkercad

Full tutorial URL:
https://codeadam.ca/learning/arduino-motion-sensor.html


Repo Resources


About

A basic script to detect motion using an Arduino and a motion sensor.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages