Raspberry Pi Programming - Push Button, And Replacing It With PIR Sensor

In my previous article, we learned how to control LED using Python script.

In this article, I will introduce new gpiozero library to you. Using this library, we will see how we can use push button to glow the LED and then, replace this push button with PIR Sensor.

What is gpiozero?

In a previous article, we saw how to use RPi.GPIO library to control GPIO pins on Raspberry Pi. gpiozero is another way to control GPIO. Using this library, we can program writing minimum lines of code. We will see the example in our code snippet.

Controlling LED with push button

In this simple example, we will see how to use push button to control the LED. This will simply work just like electrical switches we use in our homes. When we push the button, the LED will glow and on releasing that, the LED will stop glowing.



Components needed,

  1. Raspberry Pi (I am using RPi 2)
  2. Push button
  3. PIR Sensor
  4. LED
  5. BreadBoard
  6. Jumper cables

Pins used to complete the circuit.

  1. Pin 6 (Ground)
  2. Pin 15 (GPIO 22)
  3. Pin 23 (GPIO 11)

Code snippet

Go to terminal, write sudo nano led2.py and then add this code in the file.

  1. from gpiozero import LED, Button    
  2. from signal import pause    
  3. led = LED(11)    
  4. button = Button(22)    
  5. button.when_pressed = led.on    
  6. button.when_released = led.off    
  7. pause()    
Press Ctrl + X. Then, press ‘Y’ and press Enter.

 

As compared to the previous article, here we have imported gpiozero. Using this library, we have to import LED, Button interface to be used in our program. In gpiozero library, there are lots of preconfigured interfaces available, we simply need to import those interfaces and use in our program.

If we skip pause function / method, then our script will work and instantly end. To keep our script running, we have to add pause(). This will be imported from signal library.

Now, using LED button interfaces, we will create our LED and button objects. But for that, we need to pass the GPIO pin numbers, where our actual LED and Button will be attached. One thing to consider here is that these pin numbers are based on BCM (with GPIO prefix). In our example, we are using GPIO 11 (pin 23) and GPIO 22 (pin 15).

gpiozero provides us a lot of functions, like when_pressed and when_released. Both of these functions are associated with button. When button changes its state from inactive to active (pressed), then led will change its state from Off to On, and vice-versa when button will be released.

Now, our code is ready to be tested. But before that we need to install gpiozero library. Following command is not at all needed if you are using latest version of Raspbian Jessie, but if you are not sure then just run this command to install gpiozero library,
  • sudo apt-get update
  • sudo apt-get install python3-gpiozero python-gpiozero

This way, our environment is ready to test our Python script. To run the script, run

  • python led2.py

Now, on the button press, your LED should glow and on release, it should stop glowing. If this is the case, then you are ready to move to replace the button with a PIR Sensor.

Replacing Push button with PIR Sensor

Passive Infrared Sensor is a motion sensor that uses infrared light to detect an object's presence within a field of view.

A PIR sensor is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors.


Now, we will replace our push button with PIR Sensor to glow our LED. We are just replacing the press and release events of button with sensor’s input data, like presence of an object and no object available.

PIR Sensor has three pins Vcc (+5v), Gnd (ground), and Output pin - this pin actually provides us the input if it senses any object within its range.


(This is the back side of the PIR sensor)

To create the circuit, we will use push button’s input pin in PIR’s out pin. We will get the +5v from RPi’s pin 2 and ground from the same pin 6.



Wires seem confusing in the picture, but they're actually not. Red wire and purple wire are connected with +5v of RPi and sensor. Black wire and grey wire are connected with Gnd of RPi and sensor. White and blue wires are connected with GPIO 22 (pin 15) and sensor’s out pin. LED’s circuit will remain same.

Use the following script to run this circuit. Go to terminal, write sudo nano pirled.py, and then add this code in the file.

  1. from gpiozero import LED, MotionSensor  
  2. from signal import pause  
  3. led = LED(11)  
  4. pir = MotionSensor(22, queue_len=1)  
  5. pir.when_motion = led.on  
  6. pir.when_notmotion = led.off  
  7. pause()  
Press Ctrl + X, press ‘Y’, and press Enter. Just like button interface, gpiozero has MotionSensor interface and others as well. Execute the script by,

python pirled.py


If everything goes well, on moving your hand in front of PIR, your LED should glow.