Controlling LED with Motion Sensors in Raspberry Pi using python.

Controlling LED with Motion Sensors in Raspberry Pi using python.

In the tutorials series of raspberry pi here is the next article. In this article we are going to learn controlling LED with Motion Sensors in Raspberry Pi using python.

So lets start with Controlling LED with Motion Sensors in Raspberry Pi using python.

What you need:

Raspberry PI 3 Model B [This is what I used.]

Motion sensor module HCSR-501

Few wires to Connect, a bread board would be awesome.

LED lights.



First connect the Motion sensor to the Raspberry Pi using the GPIO pins. There are three pins on Motion sensor which are VCC, GND, OUT.

VCC is connected to 5v supply on raspberry pi, out in connect to the pin in which you want the input, I connected it to GPIO13, pin 13.

GND is connected to GND of raspberry pi.

Insert the LED in Breadboard.

Connect one end of led to the register with 100 ohm.

Now connect the other end to the GND on Raspberry PI.

Next you need to do is connect other end of register which is not connected to LED to any pin Lets connect it to pin 7 i.e. GPIO 7

Now you connection is complete. We need register to stay safe of abrupt current flow.

Next what you need to do is to write a simple code in python which can do the rest.



What the Codes does:

In first line we imported the library which will provide us with the function we will use for accessing GPIOs.

Next to assign the pins 13 for PIR sensor and 7 for Led to variables.

The next line sets the mode to GPIO board.

Then we set the motion sensor pin as input and LED pin as output .

Then we run a loop which check the current state of motion sensor. If the state is True i.e. active it turns on the LED else it passes.

Thus we simply made a system which blinks the LED when any motion is detected.


Liked the article, then share it. Subscribe for more such articles.

Also read

Raspberry Pi blink LED tutorial.

Raspberry Pi Motion Sensor Tutorial.

Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.