Close

Amplifier and Basic Code Setup

A project log for Music Button

Raspberry Pi Zero plays an audio clip when a button is pushed.

ehunckehunck 04/10/2019 at 04:160 Comments
RPi Breakout Amplifier

I received 5 of the RPi Zero breakout boards in the mail.  I only planned on populating a single one of the audio output channels  with the LM386 audio amplifier.  I added in RC filters on input and gain on the amplifier as necessary.  I set the output volume of the RPi to 85% to avoid clipping while still trying to maximize the volume.  I noticed there was some noise on the power supply I was using  around 1kHz that ended up being audible.  I threw a large 1000uF capacitor on the power supply input (what I had available in bulk capacitance), and the overall idle noise was significantly reduced.  

The completed stack can be seen below. 

I got a set of small computer speakers (without internal amplifiers) at a second-hand store.  I am using a single one of these for this project.  I am now need to make a small enclosure that will have a push button switch on it that will close the wires on one of the JST connectors.  The button triggers  the audio to start or stop.

The python code running on the RPi is super simple.  It uses pygame to play a WAV file.  I run this code on startup.

import time
import RPi.GPIO as GPIO
from pygame import mixer

def button_callback(channel):
    print("button release")
    if mixer.music.get_busy():
        mixer.music.stop()
    else:
        mixer.music.load('my_audio.wav')
        mixer.music.set_volume(1.0)
        mixer.music.play()

mixer.init()
mixer.music.load('my_audio.wav')
mixer.music.set_volume(1.0)
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Set pin 10 to be an input
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=200) 

while True:
    time.sleep(5)
GPIO.cleanup() # Clean up

The line from the startup script that runs this code is below.  It sets the volume to 85%, changes the directory, and then executes the Python.

(sleep 10; sudo amixer cset numid=1 85%; cd /home/pi/Documents/music_button/; python3 music_button.py)&

Finally, a cool part was brought to my attention in the comments (see below).  I have ordered a DFPlayer Mini MP3 Player as a possible alternative to the RPi Zero setup.  This module is much cheaper and overall simpler setup.  I am continuing with my original plan until that module arrives in the mail.

Discussions