March 2018 Archives

Programming the Raspberry Pi with Perl; eBook fundraiser

A few weeks back, it was pointed out to me that Timm Murray was proposing to write an eBook for using Perl on the Raspberry Pi. Due to my extensive work on that platform over the last two-plus years, I had keen interest in the project.

Timm will be writing the bulk of the content using various distributions including my RPi::WiringPi along with all its related distributions, and I will be adding at least one chapter to cover my indoor grow room single-webpage environment controller, as well as performing editing duties and testing of the code.

We've been working together for a couple of weeks now, and today, I'm proud to announce the official launching of the fundraiser for the new book.

Whether you're interested in working on the Raspberry Pi, or just want to donate to a good cause for other Perl hackers, please have a look.


Pi Day!

Yesterday, when I published and wrote a blog about my new RPi::StepperMotor distribution, I didn't even think that the very next day was Pi Day.

So, although nothing significant could be done in the meantime, I updated that dist with a cleanup() method which resets the GPIO pins at the end of your script, and published version 2.3623 of RPi::WiringPi, which is the top-level framework that allows you to safely pull in all of the other RPi:: distribution objects.

Changes include:

  • bumping GPSD::Parse prereq due to having added some convenience methods to it

  • Documentation fixes and updates (all broken links now work!)

  • incorporation of said RPi::StepperMotor distribution

Nothing major, but since most of my personal programming time the last two years has gone into Raspberry Pi work for Perl, thought I'd do at least something :)

byterock, this one's for you...


Controlling a stepper motor with the Raspberry Pi

I live in an extremely remote part of Northern British Columbia, Canada. It is a minimum of an hour to get to the nearest town. We are exceptionally sparsely populated with a vast amount of land right on the second-largest lake in the province.

To that end, we have a wild abundance of wildlife everywhere. Bears, moose, wolves, coyotes, deer etc etc. I set out to set up a series (eight) wildlife cameras using Raspberry Pis (four on my house, the other four each on a separate cabin), all streaming to a central server that I can display on a television set, with all eight camera streams within a single window.

After I accomplished the bulk of that work, I wanted a way to pan and tilt my cameras individually. The tilt part I use a standard servo with the the servo() functionality of the RPi::WiringPi distribution.

For pan, I decided on a 28BYJ-48 gear reduction stepper motor, driven by a ULN2003A motor driver. I then read the datasheet, put some code together, and now we can drive said unit with Perl.

The setup is extremely basic, simply connect 5v+ and Ground to the driver board, along with four GPIO pins (image on my Github).

The new distribution is RPi::StepperMotor, and is very easy to get going with.

To the code. This is pretty well the most basic example we can have. It sets up the object, readies the pins, then swivels 180 degrees clockwise, then back counter-clockwise 180 degrees, essentially on a permanent sweep:

use warnings;
use strict;

use RPi::StepperMotor;

my $sm = RPi::StepperMotor->new(
    pins => [12, 16, 20, 21]

while (1){

NOTE: It is up to the user to unset the pins after they are done. Shortly, this distribution will appear within the RPi::WiringPi framework, so pin cleanup will be done automatically upon crash or program exit if you use that distribution and instantiate a stepper motor object through there. It's already in the repo, doing the full test suite so I should have that up tomorrow.

I've included also a stepper binary for quick command line movement of the motor:

Usage:   stepper <cw|ccw> degrees [speed]

Example: stepper cw 180 [full]

The stepper motor can be driven in "half" speed (ie. we execute all steps per movement) or "full" speed, which skips every second step. Full is half as accurate, but twice as fast. You can set the speed ('half' by default) in the instantiation call:

my $sm = RPi::StepperMotor->new(pins => $pins, speed => 'full');

...or at runtime with the speed() method:


The only other option is the delay we introduce in between each step. This, too can be set in the instantiation call, or at runtime with the delay() method. The default delay is 0.01 seconds.

my $sm = RPi::StepperMotor->new(pins => $pins, delay => 0.2);



Have fun!

About Steve Bertrand

user-pic Just Another Perl Hacker