Random Song Jukebox (Raspberry Pi)

682

12

1

Posted

Introduction: Random Song Jukebox (Raspberry Pi)

After messing around with a Google AIY Voice Kit for Raspberry Pi, I decided to re-purpose the hardware to make an offline jukebox. When a user hits the top button, a random song stored on the Pi will play. The volume knob is there to help adjust the volume and mute the song (since once a song is started, it can't be stopped).

This tutorial picks up assuming you have already bought and assembled the Google AIY Voice Kit, or similar hardware configuration (a Raspberry Pi with a speaker and button attached). You'll also need a rotary encoder for the volume knob (I used this one), as well as wires, solder and a soldering iron to attach the encoder to the device.

Step 1: Wiring the Volume Knob/rotary Encoder

Using the soldering iron, attach the rotary encoder to the Voice HAT using wires. On the encoder, the side with three pins is for signals for turning the knob in either direction (red and green wire), with the middle one (black wire) being ground. The other side with two pins is for the button in the encoder, with one (white wire) being for the signal and the other (black wire) being for ground. The wires are soldered in the Servo section of the Voice HAT, following this pinout diagram. Green is hooked to GPIO 26, red is hooked to GPIO 6, black is hooked to the ground of GPIO 26, while white is hooked to GPIO 13 and black is hooked to the ground of GPIO 13.

Step 2: Fitting the Knob in the Box

If you use the cardboard box from the Google kit like I did, it's fairly simple. I cut out a square in the inner sleeve to fit the encoder, than make a hole in the side of the box, just above the slot for SD card access, to poke the knob through. Secure it with a washer and bolt, and then put the volume knob cap over the metal rod to lock it in place.

Step 3: Software - Music When Hitting the Button

I assume you understand the basics of working with scripts on Raspberry Pis/Linux builds.

The music script button.py, available on my GitHub here, waits for the button to be pushed, at which point it pulls a random file from a directory (/home/pi/Music in my case) and then plays it using either mpg123 or aplay, depending on if the song is an mp3 or not.

The file is available on my GitHub here. Use wget to download it to your device, placing it in your /home/pi directory.


Make sure to have python, mpg123 and aplay installed. Run

sudo apt-get install python mpg123 alsa-utils

to get all the necessary files.

Make the downloaded script executable by running

sudo chmod +x /home/pi/button.py

The script assumes you are using GPIO 23, the default pin for the Google kit button. If you are using a different pin for the button, update the script accordingly.

Step 4: Software - Volume

The volume script is available on my GitHub here. You can download it using wget. Place it in your /home/pi/bin directory (make one if it doesn't already exist).

Make sure your bin directory is in your PATH. Check by typing

echo $PATH

If the directory does not show up, add it using

echo "export PATH=$HOME/bin:$PATH" >> ~/.bashrc

and restart your device after doing so.

Also make sure you have Python3 on your device to run the script. Get it using

sudo apt-get install python3 python3-rpi.gpio

Make the downloaded script executable by running

sudo chmod +x /home/pi/bin/volume-control

The script assumes you are using the same GPIO pins as I did. If not, change them in the SETTINGS section of the script. The button function of the rotary encoder, GPIO 13, is set to None by default. If you activate it, you can mute and unmute the audio with the encoder's button.

Step 5: Software - Run the Scripts at Startup

To run the scripts immediately when the device boots, call them in your rc.local file.

Go into your rc.local file with

sudo nano /etc/rc.local

In there, at the bottom, add

python /home/pi/button.py &

sudo /home/pi/bin/volume-control &

between fi and exit 0. Make sure to include the & after each command to prevent your Pi from hanging up during the boot process.

Step 6: Add Music

Add some music to the specified directory (by default, /home/pi/Music), hit the button and enjoy!

Share

Recommendations

  • LED Contest 2017

    LED Contest 2017
  • Raspberry Pi Contest 2017

    Raspberry Pi Contest 2017
  • Voice Activated Challenge

    Voice Activated Challenge

We have a be nice policy.
Please be positive and constructive.

Tips

Questions & Answers

Comments