DJ Set With Raspberry Pi and Adafruit Circuit Playground Bluefruit
by jackmcclelland1 in Circuits > Speakers
693 Views, 1 Favorites, 0 Comments
DJ Set With Raspberry Pi and Adafruit Circuit Playground Bluefruit
Hi! My name's Jack and I'm a senior at Boston College studying Computer Science and Finance. This semester, I'm taking Physical Computing with Professor Gallaugher. We've been following a flipped classroom model and I made this project utilizing Professor Gallaugher's Circuit Python YouTube tutorials.
For my Make Art Project, I made a DJ mixer that plays songs and lights up based on the sound playing from the set.
The sound-activated lights are implemented utilizing an Adafruit Circuit Playground Bluefruit as well as an LED strip. The audio is played from a Raspberry Pi, which can store full-length songs (the Bluefruit only has enough storage for short audio clips). The Pi is connected to an Adafruit MPR121 12-Key Capacitive Touch Sensor, and depending on what you touch, different songs will play.
I was inspired to create this project because DJ'ing is a side-hustle of mine (I'm co-president of the DJ club at Boston College and have played dozens of gigs at BC). While tough at times (especially with the Raspberry Pi), this was a fun project to work on because it blended my passion for DJ'ing with the course material!
If you have any questions, don't hesitate to reach out and I'll try my best to answer – I'm jack.m.mcclelland@gmail.com.
Supplies
All materials:
- Bluefruit materials (middle of image):
- Adafruit Circuit Playground Bluefruit
- LED light strip
- Power source (battery pack, USB cord connected to laptop or wall outlet, etc)
- MPR121 materials (bottom of image):
- Adafruit MPR121 12-Key Capacitive Touch Sensor
- STEMMA QT cables (connecting MPR121 to Raspberry Pi)
- Any other alligator clips or connectors that make this connection easy
- Alligator clips to connect the touch sensors on the MPR121 to the outside of the DJ set (recommended)
- Raspberry Pi materials (right of image):
- Raspberry Pi
- Power source (USB cord connected to laptop or wall outlet)
- Speaker (with an mp3 audio jack)
- Frame materials (top of image):
- Box to house your DJ set (I used a custom-designed lasercut box made out of 1/8" dark blue acylic)
- Optional materials (left of image):
- USB to USBC adaptor (to connect your Mac laptop to your Raspberry Pi)
- Masking tap (to tape materials inside your box)
- Any other materials you add to expand upon this project, suggestions include:
- Another speaker
- Potentiometers to control the volume of tracks playing
- Other buttons or sensors, whatever you want!
Bluefruit Sound-activated Lights
The first step is getting the sound-activated lights all set up.
The first step involves the following materials:
- Adafruit Circuit Playground Bluefruit
- Battery pack (optional, but highly recommended)
- LED strip
First, for getting the Bluefruit set up on your computer so your code will run, there are a series of tutorials you can watch to get set up and troubleshoot any issues that come up:
- Professor Gallaugher's Circuit Python YouTube tutorials
The next step is to get the hardware set up.
- Attaching the LED strip to the Bluefruit:
- I attached the red alligator clip from the LED strip to the VOUT pin on the Bluefruit
- I then attached the white alligator clip to A1 (remember what you attach this to for the code)
- Finally, attach the black alligator clip to GND on the Bluefruit
- Next, make sure your Bluefruit is receiving power.
- I used an external battery pack; you can also plug the Bluefruit into your laptop, outlet, etc
Finally, the code. Make sure if you plugged the white alligator clip from the LED strip into a pin other than A1, you'll need to change that in the code below. Specifically, watch out for this line:
strip = neopixel.NeoPixel(board.A1, NUM_PIXELS, brightness=0.5, auto_write=False)
Here's the code!
import array import math import audiobusio import board import neopixel PEAK_COLOR = (100, 0, 255) NUM_PIXELS = 30 CURVE = 2 SCALE_EXPONENT = math.pow(10, CURVE * -0.1) NUM_SAMPLES = 160 def constrain(value, floor, ceiling): return max(floor, min(value, ceiling)) def log_scale(input_value, input_min, input_max, output_min, output_max): normalized_input_value = (input_value - input_min) / \ (input_max - input_min) return output_min + \ math.pow(normalized_input_value, SCALE_EXPONENT) \ * (output_max - output_min) def normalized_rms(values): minbuf = int(mean(values)) samples_sum = sum( float(sample - minbuf) * (sample - minbuf) for sample in values ) return math.sqrt(samples_sum / len(values)) def mean(values): return sum(values) / len(values) def volume_color(volume): return 200, volume * (255 // NUM_PIXELS), 0 strip = neopixel.NeoPixel(board.A1, NUM_PIXELS, brightness=0.5, auto_write=False) strip.fill(0) strip.show() mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16) samples = array.array('H', [0] * NUM_SAMPLES) mic.record(samples, len(samples)) input_floor = normalized_rms(samples) + 10 input_ceiling = input_floor + 500 peak = 0 while True: mic.record(samples, len(samples)) magnitude = normalized_rms(samples) c = log_scale(constrain(magnitude, input_floor, input_ceiling), input_floor, input_ceiling, 0, NUM_PIXELS) strip.fill(0) for i in range(NUM_PIXELS): if i < c: strip[i] = volume_color(i) if c >= peak: peak = min(c, NUM_PIXELS - 1) elif peak > 0: peak = peak - 1 if peak > 0: strip[int(peak)] = PEAK_COLOR strip.show()
Raspberry Pi – sound and Capacitive Touch
The next step involves connecting the sound aspect of the DJ set.
This step involves the following materials:
- Raspberry Pi
- USB cord to power the Pi
- Speaker with mp3 jack attachment (and any cords needed to power or charge the speaker)
- MPR121 capacitive touch sensor chip
- Alligator clips and connections to connect the Pi to the MPR121 (STEMMA QT for example)
- Alligator clips to go from the MPR121 to the exterior of the DJ set (to make touch sensors accessible)
Again, for getting the Raspberry Pi set up on your computer and network so your code will run, there are a series of tutorials you can watch to get set up and troubleshoot any issues that come up:
- Professor Gallaugher's Circuit Python YouTube tutorials
The next step is to get the hardware set up.
- Basic Raspberry Pi setup:
- Plug the Pi into power through the power cord
- Make sure the SD card is inserted (correctly) into the underside of the Pi
- Speaker setup:
- Plug the speaker into the audio out jack of the Pi (luckily this is an easy step!)
- Raspberry Pi <> MPR121 setup:
- I used a STEMMA QT that led to alligator clips that led to connections that plugged into the Pi
- For more detailed instructions on how to do this, refer to Professor Gallaugher's videos
- Raspberry Pi School - Using STEMMA QT on a Raspberry Pi
- CircuitPython School - Use Capacitive Touch to Build a Raspberry Pi based Drum Machine
- Finally, plug some alligator clips onto the capacitive touch sensors of the MPR121
- This will make it easier for the user to trigger the touch sensors
The final step is getting your audio files set up. Download any audio files as .wav or .mp3 files (I used .mp3 files as this was easiest). A few notes here:
- To be able to drag and drop these audio files between your Pi, see the following video:
- Physical Computing - Getting a Raspberry Pi to Appear in the Mac Finder
- In the code below, make sure you're setting the path to the folder within your Pi that you want
path = "/home/pi/128-bpm/"
- Above, you can see my folder within the Pi home folder storing audio files is called "128-bpm"
- If yours is different, this is the place to customize that
- Finally, make sure you replace the code with the proper names for your files
sound_files = ["1.mp3", "2.mp3", "3.mp3", "4.mp3", "5.mp3", "6.mp3"]
- Above, you can see my files are named 1-6; if yours are different, customize it!
Finally, the code.
import adafruit_mpr121, board, pygame i2c = board.I2C() touch_pad = adafruit_mpr121.MPR121(i2c) path = "/home/pi/128-bpm/" sound_files = ["1.mp3", "2.mp3", "3.mp3", "4.mp3", "5.mp3", "6.mp3"] # Pygame setup pygame.mixer.init() speaker_volume = 0.5 pygame.mixer.music.set_volume(speaker_volume) while True: for i in range(12): if touch_pad[i].value: print(f"You touched pad # {i}!") pygame.mixer.music.load(path + sound_files[i]) pygame.mixer.music.play() while pygame.mixer.music.get_busy() == True: continue
Creating the DJ Set's Box or Frame
The last step is to put the electronics together.
- I used a lasercutter to cut a custom-designed box out of 1/8" dark blue acrylic material
- For the design I used, see one of the images in this step
- In this case, I used red for cut, black for engrave; turns out these were switched on the lasercutter
- You can also add the option to add notches or patterns that make the box sides fit together
- I'd definitely include holes any speakers an switches you have
- You also might want to include holes for the capacitive touch alligator clips
Once you have the designs cut, assemble the box.
Finally, fix all of the electronics inside the box. This can be done using tape, hot glue, or other methods.
After that, you're done! Congrats. Again, let me know if you have any questions at jack.m.mcclelland@gmail.com.
Happy building!