Add disco lights to your Raspberry Pi using the PiGlow add-on

What is the PiGlow?

The PiGlow was one of the earliest add-on boards for the Raspberry Pi, and it's still on sale now. It only requires 20 GPIO pins, so it works with the earliest models of the Raspberry Pi, as well as the latest generations, including the Raspberry Pi 4.

The PiGlow adds 18 LEDs to your Raspberry Pi, arranged in a spiral made up of three legs of six LEDs. Each LED has a fixed colour: you can't make the red LEDs light up green, for example.

The PiGlow reminds me of the boxes of disco lights you see around a DJ deck at a wedding, so I thought I'd see whether the lights can be animated while music plays.

The PiGlow add on board, with three spirals of six LEDs illuminated.

You can use the PiGlow board to add disco lights to your Raspberry Pi!

Using Pygame to play music

I'm using the Python playsound module to play the music in Raspberry Radio, because it's concise and easy to use. You can alternatively use Pygame. The downside is that you need to convert your MP3 files to OGG format. The upside is that you can run animations, such as disco lights, while the music is playing.

Here's how to convert your MP3 files to OGG format:

  1. Install Soundconverter. You can use the Add/Remove software tool or enter: sudo apt install soundconverter in the terminal.
  2. Run Soundconverter and click the tool icon to access the settings. Choose where the OGG files should be stored, and check the file conversion settings are right. Click Close when you've finished.

    Soundconverter menu options, showing you can change where the OGG files are stored.

  3. Click the folder icon to browse to the folder of MP3s you'd like to convert. Then click the Convert button in the top left.

    Soundconverter main window showing list of files.

    How to play music using Pygame and add PiGlow animations while the music plays

    Here's the code to play an OGG file using Pygame, with an LED animation on the PiGlow add-on board. It uses Pygame to play the audio, and while it is playing, it runs an animation sequence on the PiGlow. This animation sequence code is taken from the PiGlow documentation.

    Change the audio filename to your chosen song. You can combine this code with my Raspberry Radio code to play lots of tracks with illuminations.

    import pygame
    import piglow, time
    
    def play_mp3(mp3_filename):
        mixer = pygame.mixer
        mixer.init()
        sound_to_play = mixer.Sound(mp3_filename)
        sound_to_play.set_volume(0.5)  # tweak to balance with voice volume
        channel = sound_to_play.play()
        
        i = 0
        
        while channel.get_busy():  # Don't return until MP3 finishes playing
            piglow.all(0)
            piglow.set(i % 18, [15, 31, 63, 127, 255, 127, 63, 31, 15])
            piglow.show()
            i += 1
            time.sleep(0.1)
    
    play_mp3("test.ogg")   
    

    More Raspberry Pi projects

    Find more Raspberry Pi projects and tutorials here.

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About