Shop OBEX P1 Docs P2 Docs Learn Events
3$ mp3 player — Parallax Forums

3$ mp3 player

I received a microbit mystery box recently and two of the items it contained were a microbit v1.5 and an amp/speaker Parallax #32781 so I thought I would put them together in a small project. The project I chose was a simple mp3 player with a microbit controller. The player I bought was
https://www.amazon.com/dp/B09GPC4VXS?ref=ppx_yo2ov_dt_b_product_details&th=1

There are quite a few modules out there that call themselves DFPlayer but they don't all have the same IC's or the same serial command set and maybe other differences. The mp3 player I am using is labelled mp3 tf 16p v3.0, and this is the reference I worked with

https://usermanual.wiki/Manual/DFPlayerMiniManual.234442183/pdf

The set up I have now is behaving reliably, it is great for many sound effects such as laser sounds, machine gun sounds, monster howls for use with robots or halloween ghosts/monsters, if you have a Youtube account you can download lots of these sound effects copyright free from youtube studio. For the demo I am using a couple of music clips. MP3 clips should be named 001.mp3 002.mp3 003.mp3 ..... etc. etc.

The micro python code has 5 methods controlled from a single button (button A). One press initializes the mp3 player after that one press toggles between play and pause, two presses selects the next track and 3 presses selects the previous track. This configuration can easily be changed if you have another preference.

Video link

Any questions and I will attempt to help.

from microbit import *
import microbit
import time

microbit.uart.init(baudrate=9600, tx=pin2)

start = 0
end = 0
elapsed = 0

count = 0
initialized = False
playing = False

Start_Byte = 0x7E
Version_Byte = 0xFF
Command_Length = 0x06
End_Byte = 0xEF
Acknowledge = 0x00

def player_init():
    write_to_player(0x09, 0, 1)  # initialize source (TF)

def player_pause():
    write_to_player(0x0E, 0, 0)

def player_play():
    write_to_player(0x0D, 0, 0)

def player_next():
    write_to_player(0x01, 0, 0)

def player_previous():
    write_to_player(0x02, 0, 0)

def write_to_player(cmd, arg1, arg2) :
    buffer = []
    checksum = 0
    checksum = -(Version_Byte + Command_Length + cmd + Acknowledge + arg1 + arg2)
    chk_0 = checksum & 0xFF
    chk_1 = (checksum >> 8) & 0xFF

    buffer = bytearray([Start_Byte, Version_Byte, Command_Length, cmd, Acknowledge,
                        arg1, arg2, chk_1, chk_0, End_Byte])

    microbit.uart.write(buffer)

while True:
    if microbit.button_a.is_pressed():
        start = time.ticks_ms()

    microbit.sleep(100)

    if start != 0:
        end = time.ticks_ms()
        elapsed = end - start

    if elapsed >= 500:
        end = 0
        elapsed = 0
        start = 0
        count = microbit.button_a.get_presses()

    if count == 1 and not initialized:
        player_init()
        count = 0
        initialized = True
    elif count == 1 and not playing:
        player_play()
        playing = True
        count = 0
    elif count == 1 and playing:
        player_pause()
        playing = False
        count = 0
    elif count == 2 and playing:
        player_next()
        count = 0
    elif count == 3 and playing:
        player_previous()
        count = 0`

650 x 496 - 22K
MP3.jpg 21.9K
Sign In or Register to comment.