Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


Small Speaker project, Gpio problems and how I solved them
#1
Photo 
Hi Everyone ,


I thought I would start a thread about my speaker project and hopefully help someone who has had any problems similar to mine with gpio buttons .

so I made a small speaker enclosure, designed using an enclosure calculator online (this also helped me with the port length and diameter) that was sized for my speaker driver . The problems I've since discovered are related to how small the enclosure is and how close the wiring and  the pi are to the speaker magnet and electrical interference caused by it .

The project was simple , small size enclosure , cheaper than a sonos speaker, gpio buttons for volume and play/pause , mono output for the single speaker and the ability to connect to my nextcloud storage .

I used a raspberry pi 4 , a Hifiberry amp2 and a small Peerless full range speaker driver .

Moode Audio made this childs play with the sox resampling for the mono ouput , the gpio button handler for the buttons and the NFS option in Music sources connected easily to my nextcloud drive so I could play music from there.

I can't take credit for the speaker design as I liked  this  design , however I wanted it to be apporximately 160mm in length,depth and height .

I soldered dupont wires to the ends of the the buttons, connected everything up , powered on and setup my configuration requirements in Moode.

This is where the problems started I started!, the buttons were very unreliable I would be able to play/pause and increase/decrease the volume, but  occasionally pressing the volume would cause the music strangely to start playing or touching play would increase/decrease  the volume. Even stranger randomly  the speaker would begin playing after hours of no music playing most of the time it seemed to do this when a lamp  was turned on near to the speaker box .

Obviously something is wrong I discoved through the process of elimination that the dupont connectors were not fitting snuggly on the amp2 header pins as they were not the standard size of 2.54mm.

After trying to crimp the connectors to be tighter it still didnt solve it completely , I resolved to solder the wires directly to the gpio pins albeit void the warranty  of the Hifiberry amp2. I also decided to twist the wires together and shield them using aluminium tape to resolve some of the electrical interference.
This solved 100% of my problems.( or so I thought )

In fact 99% of my problems were solved with this fix , but occasionally when a lamp  next to the speaker  was turned on, the speaker would begin playing again . Angry 

After researching my problem for ages , I fell upon a post on the raspberry pi forum that detailed my issues and the hardware and software fixes. Here is the link, but for those who didn't click it relates to how noisy mechanical buttons are  in terms of electrical noise. Some of the noise is removed by the debounce setting in gpio handler but this only solves the problem of bounce after a button press , it doesnt sort out ,or rather filter out false presses/ false positives caused by electrical interference.
(I.e the lamp turning on and causing fluctuations in the the already noisy enviroment, with the speaker magnet in a small enclosure )

 The solution was to solder onto each input wire a low pass filter as its called  using a resistor and capacitor configuration , plus an additonal pullup resistor ( although its configured in software to enable the internal one on the pi itself).
(low pass filter is used instead of "high pass" because Tim has configured the script for gpio's to use  the internal pullups instead of pulldown .)

I also followed the instructions in the link to add an additonal bit in software to filter out false positives . This works similar to debounce in that it waits a very small time before reading inputs again , but in this setup it reads  that an event was triggered waits a small time and then checks again for an  event (i.e the button is still pressed ) before it acts on the event . This filters out ghost presses or false presses causes by interference rather than actual button presses .


Code:
# Configure the pins
cursor.execute("SELECT * FROM cfg_gpio")
for row in cursor:
    #print(str(datetime.datetime.now())[:19] + ' row id=' + str(row['id']) + ', enabled=' + row['enabled'] + ', command=' + row['command'])
    if str(row['id']) == '1' and row['enabled'] == '1':
        sw_1_pin = int(row['pin'])
        sw_1_cmd = row['command'].split(' ')
        GPIO.setup(sw_1_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

        def sw_1_event(channel):
            time.sleep(0.005) # edge debounce of 5mSec
            # only deal with valid edges
            if GPIO.input(channel) == 1:
                subprocess.call(sw_1_cmd)

        GPIO.add_event_detect(sw_1_pin,
                              GPIO.RISING,
                              callback=sw_1_event,
                              bouncetime=bounce_time)
        print(str(datetime.datetime.now())[:19] + ' sw_1: pin=' +
              str(sw_1_pin) + ', enabled=' + row['enabled'] +
              ', bounce_time=' + str(bounce_time) + ', cmd=' + row['command'])


I added these lines

Code:
time.sleep(0.005) # edge debounce of 5mSec
# only deal with valid edges
if GPIO.input(channel) == 1:

As seen above to each pin I was using,  in /var/www/command/gpio-buttons.py  This was based on the add_event_detect() function under the Rising Edge: section of code .

Since adding the hardware and software fixes to my project I have not had any spontaneous playback issues and any button presses causing other button events(i.e play causing volume changes)


I hope this can help someone who has problems similar to mine reagrding gpio glitches .

I'll post pics below of my project so far , I havent finished the front cover which needs fabric  to hide the speaker .

   

   

   

   

   

   

   

   

   

   
Reply


Forum Jump: