Page 1 of 1

### UART MP3 VOICE MODULE

Posted: Sun Jun 02, 2019 1:52 pm
I am trying to code in Micropython for Microbit with Gravity UART MP3 Voice Module
[url]https://wiki.dfrobot.com/Voice_Module_S ... 4#target_5[/url]

[code]
from microbit import *
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin12, rx=pin16)
buffer = {0xAA, 0x02, 0x00, 0xAC}
uart.write(buffer)
[/code]

Nothing happened and no sound output.

Please let me know what is the problem with my code. I have tried in Makecode and confirm that the pins and the module is working and it can play the file in the module.

### Re: UART MP3 VOICE MODULE

Posted: Sun Jun 02, 2019 11:53 pm
Unlike C, In Python (and MicroPython), if you write

Code: Select all

``````x = { 1, 9, 3 }
``````
that creates a set containing the numbers 1, 9, 3. One important thing about sets is that the order doesn't matter.

In this case, you need an array of bytes to send to the device, in a specific order.

MicroPython happily converts your set of numbers to a bytearray of numbers (when you pass it to uart.write) but what you get isn't what you expect:

Code: Select all

``````>>> buffer = {0xAA, 0x02, 0x00, 0xAC}
>>> bytearray(buffer)
bytearray(b'\x00\xac\xaa\x02')
``````
Also if you look at the example code that you linked to, you also need to specify the track number. Converting their play function for example:

Code: Select all

``````def play(track):
buffer = b'\xaa\x07\x02\x00' + bytes([track, track + 0xb3])
uart.write(buffer);
}
``````

### Re: UART MP3 VOICE MODULE

Posted: Mon Jun 03, 2019 4:28 pm
Do you mean below? Sorry I am quite new to Microphython.

Code: Select all

``````from microbit import *
def play(track):
buffer = b'\xaa\x07\x02\x00' + bytes([track, track + 0xb3])
uart.write(buffer)

uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin12, rx=pin16)
play(1)
``````