PIR sensor

As I would like my HomeAutomation to learn my schedule I need to give the Pi some eyes. Let’s start with a PIR (passive infrared) sensor.
The Fritzing screenshot below shows how the sensor is wired up to the cobbler. Basically the red wire goes to 5V0, the black wire to GND and the yellow data wire is hooked up to #25.

PIR Fritzing

As an initial test the following statement executed from the command line served me well

while true; do gpio read 6; done

This reports a 0 when the PIR is not triggered and a 1 when triggered.

As a next step I replicated the very same functionality in a basic perl script using WiringPi-Perl.

#!/usr/local/bin/perl -w</code>

use lib "/appco.de/WiringPi-Perl";

require "wiringpi.pm";

if (wiringpic::wiringPiSetup () == -1)
{ exit 0};

# set pin #6 (marked with #25 on the T-Cobbler) to mode INPUT
wiringpic::pinMode (6, 0);

while (1){
  # read from PIR

  # PIR not triggered
  if (wiringpic::digitalRead (6) == 0){
    print ("off \n");
    wiringpic::delay (500); # milliseconds

  # PIR triggered
  if (wiringpic::digitalRead (6) == 1){
    print ("on \n");
    wiringpic::delay (500); # milliseconds

This code writes “off” to the standard out when the PIR is not triggered and “on” when triggered until you stop the script with ctrl+c.

Blink LED in WiringPi-Perl

I reworked the script from yesterday “Preparing the GPIO basis” using the WiringPi-Perl wrapper.

#!/usr/local/bin/perl -w

use lib "/appco.de/WiringPi-Perl";

require "wiringpi.pm";

if (wiringpic::wiringPiSetup () == -1)
{ exit 0};

# set pin #0 (marked with #17 on the T-Cobbler) to mode OUTPUT
wiringpic::pinMode (0, 1);

while (1){
  # LED off
  wiringpic::digitalWrite (0, 0);
  wiringpic::delay (500); # milliseconds

  # LED on
  wiringpic::digitalWrite (0, 1);
  wiringpic::delay (500); # milliseconds

While digging into the details I found a quite useful overview of the function wrappers in the file ../WiringPi-Perl/wiringpi.pm

package wiringpi;

*wiringPiSetup = *wiringpic::wiringPiSetup;
*wiringPiSetupSys = *wiringpic::wiringPiSetupSys;
*wiringPiSetupGpio = *wiringpic::wiringPiSetupGpio;
*wiringPiGpioMode = *wiringpic::wiringPiGpioMode;
*pullUpDnControl = *wiringpic::pullUpDnControl;
*pinMode = *wiringpic::pinMode;
*digitalWrite = *wiringpic::digitalWrite;
*pwmWrite = *wiringpic::pwmWrite;
*digitalRead = *wiringpic::digitalRead;
*shiftOut = *wiringpic::shiftOut;
*shiftIn = *wiringpic::shiftIn;
*delay = *wiringpic::delay;
*delayMicroseconds = *wiringpic::delayMicroseconds;
*millis = *wiringpic::millis;
*serialOpen = *wiringpic::serialOpen;
*serialClose = *wiringpic::serialClose;
*serialPutchar = *wiringpic::serialPutchar;
*serialPuts = *wiringpic::serialPuts;
*serialDataAvail = *wiringpic::serialDataAvail;
*serialGetchar = *wiringpic::serialGetchar;
*serialPrintf = *wiringpic::serialPrintf;

Preparing the GPIO basis

To use GPIO with perl on the RaspberryPi the following installations are necessary:

  1. C library for Broadcom BCM 2835 from http://www.open.com.au/mikem/bcm2835
  2. BCM2835 perl library from CPAN http://search.cpan.org/~mikem/Device-BCM2835/lib/Device/BCM2835.pm

To install the C library for Broadcom BCM 2835 the following steps are required:

wget http://www.open.com.au/mikem/bcm2835/bcm2835-1.17.tar.gz
tar zxvf bcm2835-1.17.tar.gz
cd bcm2835-1.17
sudo make test
sudo make install

For the BCM 2835 perl library the installation works as outlined below:

wget http://search.cpan.org/CPAN/authors/id/M/MI/MIKEM/Device-BCM2835-1.6.tar.gz
tar –xvfz Device-BCM2835-1.6.tar.gz
perl Makefile.PL
sudo make test
sudo make install

To verify the functionality I used the following test bed

Blink LED Breadboard

Blink LED Schaltplan

I used the example script from http://elinux.org/RPi_Low-level_peripherals#GPIO_Driving_Example_.28Perl.29


use Device::BCM2835;
use strict;

# call set_debug(1) to do a non-destructive test on non-RPi hardware
|| die "Could not init library";

# Blink pin 11:
# Set RPi pin 11 to be an output

while (1){
  # Turn it on
  Device::BCM2835::gpio_write(&amp;Device::BCM2835::RPI_GPIO_P1_11, 1);
  Device::BCM2835::delay(500); # Milliseconds
  # Turn it off
  Device::BCM2835::gpio_write(&amp;Device::BCM2835::RPI_GPIO_P1_11, 0);
  Device::BCM2835::delay(500); # Milliseconds

This script lets the LED blink for 500 milliseconds and continues to run until you stop it with ctrl+c.

Blink LED Wiring