Movement detection in Adruino and ESPHome

I made an example of how we can interact with Mini PIR sensors with Arduino and ESPHome projects.
Jan 20, 2024 — 3 mins read — Electronics

Movement detection in Adruino and ESPHome

Detecting motion is the basis of a lot of automation, so in this article and video, I'm going through the basics of using a Mini PIR sensor with Arduino-based projects, as well as using the sensor with ESPHome in Home Assistant.

The Mini PIR sensor is really useful because of its small size and it can be easily integrated into any project. It is powered by 5V so in the examples below where I'm using an ESP32 board to control the sensor, I'm using the 5V pin coming in directly from the USB lead.

The sensor needs to be connected to GND and its middle pin is the output signal pin that goes high to 3.3V, whenever a movement is detected. I have this pin connected to pin 25 on the ESP32 board and I'm setting up that pin for a digital input.

To interface the sensor with Arduino, you need to check the state of the input pin inside the loop function and if that pin is high, you can know that a movement was detected and do the actions required by the project. The full code I used in my example is available below.

void setup()  {
  Serial.begin(9600);
  pinMode(25,INPUT);
  pinMode(32,OUTPUT);
  digitalWrite(25,LOW);
  digitalWrite(32,LOW);
}
void loop()  {
    if(digitalRead(25)==HIGH)  {
      Serial.println("Somebody is here.");
      digitalWrite(32,HIGH);
    }
    else  {
      Serial.println("Nobody.");
      digitalWrite(32,LOW);
    }
    delay(1000);
}

I used an LED on pin 32 to act as an indicator when the sensor is active so whenever I detect a movement in the code, I turn that pin high to turn on the LED.



With ESPHome, the wiring is the same as before, but now, the checking and configuration of the pins is done through the YAML configuration file of the device in ESPHome.

The PIR sensor is defined as a binary sensor on pin 25, with a device class set to motion, so that the UI knows how to properly display it with the right icon and statuses.

The status LED is defined as an output on pin 32 and it is being controlled by the on_press and on_release automations of the binary sensor.

The full code is available below.

binary_sensor:
  - platform: gpio
    pin: 25
    name: "PIR Sensor"
    device_class: motion
    on_press:
      then:
        - output.turn_on: status_led
    on_release:
      then:
        - output.turn_off: status_led

output:
  - platform: gpio
    id: status_led
    pin: 32

The device is currently not set to control anything when a movement is detected but this can be connected to whatever else you have in your smart home setup. If you have an interesting example of how you use movement detection in your setup, then please let me know in the comments below.

Tools and materials used in the video:

PIR arduino esphome
Read next

Trail mapping with GPS, ESP32 and microSD card

Where I live, hiking is a big thing as we have plenty of mountains around and there are a ton of trails that people can explore and enjoy.Ho...

You might also enojy this

Environment monitor for Home Assistant

I started exploring Home Assistant and to start integrating devices into it I wanted to have a monitor in my living room that can monitor th...