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
./configure
make
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
make
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

#!/usr/bin/perl

use Device::BCM2835;
use strict;

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

# Blink pin 11:
# Set RPi pin 11 to be an output
Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_GPIO_P1_11,
&Device::BCM2835::BCM2835_GPIO_FSEL_OUTP);

while (1){
  # Turn it on
  Device::BCM2835::gpio_write(&Device::BCM2835::RPI_GPIO_P1_11, 1);
  Device::BCM2835::delay(500); # Milliseconds
  # Turn it off
  Device::BCM2835::gpio_write(&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

Leave a Reply

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