# Rpi 3b+ rpi dacpro

**URL:** <https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148>\
**Category:** Raspberry Pi\
**Created:** [2 February 2024 01:34 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148 "2024-02-02T01:34:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [2 February 2024 01:34 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/1 "2024-02-02T01:34:52Z")

</div>

Hi all, i have installed pi os and have been able to play a song from the usb drive with vlc with output from the dac and it sounds great, i wrote a script to play a song from usb and that works great. I then wrote another script to be able to use gpio and call songs like a jukebox, folder 01 track 01 with the ht16k33 displaying the key presses, that works great but i do not get any audio and the debugging shows the song playing, my issue is then when i run the other script i dont get any sound or through vlc. If reboot the sound is fine again. Its only when i run the script with the button inputs no sound. Will post scripts a little later cheers

---

<div class="post-metadata">

**Author:** ![Jack](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/jack/32/9287_2.png) [@Jack](https://forum.core-electronics.com.au/u/Jack)\
**Post date:** [2 February 2024 02:54 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/2 "2024-02-02T02:54:21Z")

</div>

Hi Paul,

It’d be great if you could include those scripts. Its a funny issue so I think the answer will lay there.

---

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [2 February 2024 05:11 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/3 "2024-02-02T05:11:35Z")

</div>

here is the script that plays the usb drive fine.  
audiotest.py

```auto

import vlc
import time

# Update the path to your USB drive and the specific folder and file
# Example: /media/cd50/MUSIC/01/your_track.mp3
# Replace 'your_track.mp3' with the actual name of the music file you want to play
music_file_path = '/media/cd50/MUSIC/01/01.mp3'

# Create a VLC instance
player = vlc.Instance()

# Create a new player
media_player = player.media_player_new()

# Load the media file
media = player.media_new(music_file_path)

# Set the media player media
media_player.set_media(media)

# Play the media
media_player.play()

# Let it play for the duration of the track or until stopped
# You might want to implement a better way to handle playback duration
time.sleep(300) # Adjust or remove based on your requirement

# Stop playing after the duration
media_player.stop()

```

Here is my script using gpio buttons and display

```auto
import RPi.GPIO as GPIO
import time
import board
import busio
import vlc
from adafruit_ht16k33 import segments

# Set up logging for VLC
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Setup for the 14-segment display
i2c = busio.I2C(board.SCL, board.SDA)
display = segments.Seg14x4(i2c)
display.fill(0) # Clear the display

# Create a VLC instance
player = vlc.Instance()
media_player = player.media_player_new()

# GPIO setup
GPIO.setmode(GPIO.BCM)
button_pins = [4, 17, 27, 26, 10, 9, 11, 5, 6, 13] # Button pins
reset_pin = 19 # Reset button pin
last_four_digits = [] # Holds the last four digits entered

def play_music(folder, track):
    music_base_path = '/media/cd50/MUSIC' # Corrected USB drive mount point
    file_path = f"{music_base_path}/{folder}/{track}.mp3" # Adjust extension if necessary
    try:
        media = player.media_new(file_path)
        media_player.set_media(media)
        media_player.play()
        logging.debug(f"Playing: {file_path}")
    except Exception as e:
        logging.error(f"Error playing {file_path}: {e}")

def update_display():
    display.fill(0) # Clear the display
    display.print(''.join(last_four_digits)) # Show the last four digits

def button_callback(channel):
    global last_four_digits
    if channel == reset_pin:
        last_four_digits = [] # Clear the digits
    else:
        # Map the channel to the corresponding digit
        digit = str(button_pins.index(channel) if channel != button_pins[0] else 0)
        last_four_digits.append(digit)

        # Check if a complete selection is made
        if len(last_four_digits) == 4:
            folder = ''.join(last_four_digits[:2])
            track = ''.join(last_four_digits[2:])
            play_music(folder, track) # Play the selected music
            last_four_digits = [] # Reset the input after playing a track

    update_display()

# Setup buttons
for pin in button_pins + [reset_pin]:
    GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_callback, bouncetime=300)

try:
    while True:
        time.sleep(0.1)
except KeyboardInterrupt:
    media_player.stop() # Stop music playback
    display.fill(0)
    GPIO.cleanup()

```

Regards Paul

---

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [2 February 2024 05:16 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/4 "2024-02-02T05:16:38Z")

</div>

not sure why the font has gone weird, ok clicked preformatted text which fixed it

---

<div class="post-metadata">

**Author:** ![Michael](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael/32/3545_2.png) [@Michael](https://forum.core-electronics.com.au/u/Michael)\
**Post date:** [2 February 2024 05:34 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/5 "2024-02-02T05:34:27Z")

</div>

Hi @paul139837 - if i had to guess it might be problematic playing music from inside an interrupt callback.

Perhaps it is better to set a state flag that is polled in the main loop → So `media_player.play()` is only ever called during the main loop.

Now the main loop might look something like this:

```auto
folder = None
track = None
selection_made = False

def button_callback(channel):
    global last_four_digits, folder, track
    if channel == reset_pin:
        last_four_digits = [] # Clear the digits
    else:
        # Map the channel to the corresponding digit
        digit = str(button_pins.index(channel) if channel != button_pins[0] else 0)
        last_four_digits.append(digit)

        # Check if a complete selection is made
        if len(last_four_digits) == 4:
            folder = ''.join(last_four_digits[:2])
            track = ''.join(last_four_digits[2:])
            selection_made = True
            last_four_digits = [] # Reset the input after playing a track

    update_display()

while True:
    if selection_made == True:
        selection_made = False
        play_music(folder, track) # Play the selected music
    time.sleep(0.1)

```

This code isn’t demonstrated to be functional, it’s more to communicate the idea of using flags that pass information out of the callback.

Does that make sense?

---

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [2 February 2024 05:46 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/6 "2024-02-02T05:46:53Z")

</div>

I will take a look at that idea and see what i can come up with, thanks for your response. This now gives me something to move forward on this project  
cheers Paul.

---

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [2 February 2024 08:57 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/7 "2024-02-02T08:57:03Z")

</div>

`So i have made some changes to my script as you suggested and it has helped with the response of the button inputs upon boot, I think the issue lies with the alsa and pulse audio settings - there isn’t any /etc/asound.conf file and when I aplay -l find the dac, I think it streams via pulseaudio - pactl list short sinks should list dac but it is not showing - not sure i could be wrong.

---

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [2 February 2024 10:38 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/8 "2024-02-02T10:38:31Z")

</div>

It looks like the fault is with the code, when the script accumulates button presses, decodes them into folder and track, and plays the corresponding song: it works, thanks for your help.

---

<div class="post-metadata">

**Author:** ![paul139837](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paul139837](https://forum.core-electronics.com.au/u/paul139837)\
**Post date:** [4 February 2024 06:06 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/9 "2024-02-04T06:06:43Z")

</div>

After further investigation - I first got buttons working and then display working which uses i2c, I did not get any sound. I then reconfigured and now I have buttons working and sound working but no display - maybe a conflict with dac and display so i might try a 4 digit display that does not use i2c unless anyone has some info, I have also tried a tm1650 display with no luck.

---

<div class="post-metadata">

**Author:** ![Liam](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/liam/32/3562_2.png) [@Liam](https://forum.core-electronics.com.au/u/Liam)\
**Post date:** [6 February 2024 03:30 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/10 "2024-02-06T03:30:12Z")

</div>

Hi Paul,

Glad to hear you got the split tests working!

Would it be possible to send through some links of the products you are using?  
PS: a great reference is [https://pinout.xyz/](https://pinout.xyz/) for all things Pi!

Liam

---

<div class="post-metadata">

**Author:** ![johnr](https://avatars.discourse-cdn.com/v4/letter/j/b9bd4f/32.png) [@johnr](https://forum.core-electronics.com.au/u/johnr)\
**Post date:** [25 June 2024 07:03 UTC](https://forum.core-electronics.com.au/t/rpi-3b-rpi-dacpro/19148/11 "2024-06-25T07:03:49Z")

</div>

HI Paul. I see you have managed to somehow install the adafruit\_HT16K33 code on a raspberry pi. How did you do that please? I installed the CircuitPython version and it just gets errors.

Thank you
