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.
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.
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:
sudo apt install soundconverter
in the terminal.
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")
© 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!
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, now in its 7th Edition, shows you how to make effective websites that work on any device.
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.
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.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!