View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Jonathan Stowe > Device-PiGlow-1.1 > Device::PiGlow



Annotate this POD

View/Report Bugs
Module Version: 1.1   Source  


Device::PiGlow - Interface to the PiGlow board using i2c


    use Device::PiGlow;

    my $pg = Device::PiGlow->new();

    my $values = [0x01,0x02,0x04,0x08,0x10,0x18,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xC0,0xE0,0xFF];


    sleep 10;

See the examples directory for more ways of using this.


The PiGlow from Pimoroni ( is a small board that plugs in to the Raspberry PI's GPIO header with 18 LEDs on that can be addressed individually via i2c.

This module uses Device::SMBus to abstract the interface to the device so that it can be controlled from a Perl programme.

It is assumed that you have installed the OS packages required to make i2c work and have configured and tested the i2c appropriately. The only difference that seems to affect the PiGlow device is that it only seems to be reported by i2cdetect if you use the "quick write" probe flag:

   sudo i2cdetect -y -q 1

(assuming you have a Rev B. Pi - if not you should supply 0 instead of 1.) I have no way of knowing the compatibility of the "quick write" with any other devices you may have plugged in to the Pi, so I wouldn't recommend doing this with any other devices unless you know that they won't be adversely affected by "quick write". The PiGlow has a fixed address anyway so the information isn't that useful.



The constructor. This takes two optional attributes which are passed on directly to the Device::SMBus constructor:


This sets the device path, it defaults to /dev/i2c-1 (assuming a newer Raspberry PI,) You will want to set this if you are using an older PI or an OS that creates a different device.


This sets the i2c device address, this defaults to 0x54. Unless you have somehow altered the address you shouldn't need to change this.


This is the Device::SMBus object we will be using to interact with i2c. It will be initialised with the attributes described above. You may want this if you need to do something to the PiGlow I haven't thought of.


This updates the values set to the LED registers to the LEDs and changes the display.


This sets the state of the device to active.


This turns on all three banks of LEDs.


This writes the PWM values supplied as an Array Reference and immediately calls update to apply the values to the LEDs.

The array must be exactly 18 elements long.

The optional second argument will cause the gamma correction to be applied if the value is true.


This is convenience to turn off (set to brightness 0) all the LEDs at once. It calls update immediately.


This sets the leds specified in the array reference in the first argument ( values 0 - 17 to index the LEDs ) all to the single value specified.

Gamma adjustment is applied.

This does not call update, this should be done afterwards in order to update the LED values.


This provides a mapping between the logical order of the leds (indexed 0 - 17 ) to the registers that control them.


The arrangement of the LEDs can be thought of as being arrange logically as 6 "rings". This provides access to the rings indexed 0-5.


Sets all of the LEDs in the logical ring indexed 0 - 5 to the value specified. Gamma correction is applied to the value.

This isn't immediately applied to the LEDs, update should be called after all the changes have been applied.


This returns an Array Ref of Array references that reference the LEDs in each "arm" of the PiGlow.


Sets the LEDs in the specified "arm" of the PiGlow to the specified value.

Value has gamma correction applied.

Update isn't applied and the update method should be called when all the required updates have been performed.


This returns a Hash reference mapping the names of the coloured LEDs to the groups of LEDs of that colour.

The delegate colours returns the keys, get_colour_leds returns the list of LEDs


Sets the LEDs in the specified "colour" of the PiGlow to the specified value.

Value has gamma correction applied.

Update isn't applied and the update method should be called when all the required updates have been performed.


This is a map of input PWM values (0 - 255) to gamma corrected values that produce a more even range of brightness in the LEDs.

The values were lifted from the piglow library for Node.js which in turn borrowed them from elsewhere.


This applies the gamma adjustment mapping to the supplied array ref.


Resets the device to its default state. That is to say all LEDs off.

It will be necessary to re-enable the groups of LEDs again after calling this.


$self->writeBlockData($register_address, $values)

Writes a maximum of 32 bytes in a single block to the i2c device. The supplied $values should be an array ref containing the bytes to be written.

The register address supplied should be the first of a consecutive set of addresses equal to the number of values supplied. Supplying an address that doesn't fit that description is unlikely to work well and will almost certainly result in undefined behaviour in the device.


These define the command registers used by the SN3218 IC used in PiGlow


If set to 1 the device will be ready for operation, if 0 then it will be "shutdown"


This should be used for a block write to enable (or disable) all three groups of LEDs in one go. The values are a 6 bit mask, one bit for each LED in the group.


A bit mask to enable the LEDs in group 1


A bit mask to enable the LEDs in group 2


A bit mask to enable the LEDs in group three.


This should be used in a block write to set the PWM values of all 18 LEDs at once. The values should be 8 bit values.

There are also CMD_SET_PWN_VALUE_[1 .. 18] to set the LEDs individually.


The written LED values are stored in a temporary register and are not applied to the LEDs until an 8 bit value is written to this register/


Writing a value to this register will restore the device to its power on default (i.e. all LEDs blank)


Jonathan Stowe <>


This is licensed under the same terms as Perl itself. Please see the LICENSE file in the distribution files for the full details.


I wrote this because I had the device and I prefer to use Perl. It probably does everything I would like it to do. If you want it to do something else or find a bug or infelicity, please feel free to fork the code at github and send me a pull request:

bug reports without patches are likely to be ignored unless you want to do do something with it that I think is fun and interesting.


This was largely a no brainer. The author of Device::SMBus did all the hard work on the Perl side and the implementation details were largely translated from the PyGlow library.



syntax highlighting: