Microsoft MakeCode

Microbit V2 audio module is missing?

Hi there,

I’ve got a question, regarding this documentaton:
https://microbit-micropython.readthedocs.io/en/v1.0.1/audio.html

In my Microsoft MakeCode Editor it marks me audio as red, because it cannot find any such module, also not in the extensions.

So, this Microsoft gadget is uncomplete, or the documentation is not for the v2?

I would like to use microbit’s own built in speaker. (you can also write an example to use it).

Thanks is advance for your help

There are two different versions of python for the Micro:bit:

  • MicroPython, which is what that documentation is from. To use the MicroPython version go to: https://python.microbit.org/v/2 (Not MakeCode)
  • MakeCode’s version, which supports different features and has different functions/methods that aren’t usually compatible with the MicroPython version (That is, MakeCode has a different API). MakeCode’s documentation is at Reference. If you click on a palette (like Basic) and then click on each command (like showNumer) you can see the three different types of support for the concept: Block-based, JavaScript, and MakeCode’s Python.

For more details on MakeCode’s Python vs. MicroPython see: MakeCode Python and MicroPython

Bill

@erkman, there is a simple way to do this with the Micro:bit, and it’s in the Music Section. The MakeCode Micro:bit Python code will be listed below too.


Open up your MakeCode editor

open up the Music section
image
Scroll all the way down the Music section till you reach the micro:bit (V2) sign
image
Attach a set built-in speaker <ON/OFF> block to your on start or forever block and set it to <ON>

Then attach any music making block and do whatever you want! If you upload this to the Micro:bit V2 it should play the sounds you coded it to run.

The next part is the same code but in the MakeCode Micro:bit Python Editor (you’ll probably find this more useful, since you’re using MicroPython, but just in case other people with this issue come to this post and want the block solution, it’s there for them):


My code, you can use something different if you want.

Hope this helped :slight_smile:

Hi Guys.
Thank you for the information.
This is a lettle pretty sad that the modules are not compatible which each other’s platforms:
For e.x. Bluetooth modul is working for communication through pc via bluezero (I use Ubuntu server 20) only via the for education based makecode website but there is NO split command that works or there is not any break command to break out of any loop.

Another thing:
You have to choose useage between extensions. Ex. Bluetooth or Datetime. If you use them booth in the same script you get an error code of 071 (this code is not doxumented any where).

Yes, I know this gadget is more for education but I thought before using a microcontroller this would be a good entry.
Now I see it is very limited.

1 Like

Yep @erkman, as you said, the Micro:bit is designed for learning to code with a pre-set microcontroller, maybe consider the Raspberry Pi?

Hi, Yes, I’ve already have a Raspy, my Ubuntu Server is running there, and for me set up a server is much easier than it was for Microbit. But Im an experienced, finally, I got it work, so I see now my server important paramters on the Microbit Screen, with mani warnings (accustic also). So what I have done with Microbit:
My server refresh with its data every minute (there is a cronjob for that):
The microbit shows me server time, there is an alarm clock, show server CPU temp, load and 1-2 extra thing which I can see next to my bed without using the smartphone :slight_smile:

The built in speaker is good for acustic notifications for that purpose (scripted in microbit the thresholds for watching) my server is within bluetooth range. Bonus: at night the leds are dimmed automacially adjusted to the months…So my microbit isn’t a toy anymore :slight_smile:

1 Like

@Erkman Your project sounds really cool and I hope you share more details here (maybe create an instructable and/or a video showing it off)!

Some background and updates on what you’ve experienced:

MicroPython, which is a “true” python, uses too much memory to support bluetooth on the V1 microbit. It sounds like work is underway to add bluetooth support to the V2.

MakeCode translates code to native ARM instructions, which leaves enough space for Bluetooth (but just barely enough on the V1). MakeCode chose to use an API that was consistent among the three languages they support (blocks, static TypeScript, and Python). That seems like a reasonable choice to me. I prefer the event handling approach used by MakeCode’s API.

I’m not sure what you mean about break and split. It looks like you can break out of loops in MakeCode and strings do support .split().

A few details about the datetime module (assuming you’re referring to microbit-pxt-datetime):

  • It was developed before the V2 existed. There wasn’t enough memory for both datetime and bluetooth on the V1, so using both led to memory errors (020).

  • It was developed for some specific applications and bluetooth wasn’t an important feature, although radio was. You can use serial, radio, and two micro:bits to “relay” messages without bluetooth. See the Remote Data Collection example. That approach would work with both the V1 and V2.

  • This is an open source project that the developer (me) does in free time. As with all open source/free projects, there may be imperfections. Other developers are welcome to contribute patches and users are encouraged to share details about problems with the developer(s).

  • I’ve now updated the code to avoid the 071 (CPU Fault) error:

    • I’ve only done one quick test with bluetooth on a V2 and it seemed to work.
    • There still isn’t enough memory for it to work on a V1 with bluetooth (020 error).
    • I ran my non-bluetooth test suites to try to ensure that it continues to work when there isn’t bluetooth.
    • I’ll try to do some more testing if/when time permits, but my job is very busy this time of year and leaves little time for this work.
    • The latest release is v2.0.23. If you go to the Python view, expand the “Explorer” in the simulator pane, and the hit the refresh button on the module, it should update. Or you can start a new project from scratch, but you should still look at the Explorer to confirm you’re using v2.0.23 or later.

    Bill

1 Like

Hi @bsiever,

I use the V2 with bluetooth, and it works as expected with very limited funtions. (with bluetooth uart read you get the data :slight_smile: )

Yes I maybe I can share, but this is very specific useage of the tools I don’t know if it is interessting anybody.
Thanks for the error code update, I thought it was something with memory because it would be more obviously if running with two modules which are incompatible (why?) with each other.

Yes, I’ve used makecode editor for the bluetooth support (micropython doesn’t have any unfortunately)

I do not know, how you tested, but if you want to import microbit-pxt-datetime and bluetooth in the same project, it seems working in the simulator, but if you deploy your code on the real microbit, then you get this 071 error code on startup, and then you have to choose, which is more important for you… :frowning:

Maybe there is a split command and break command in micropython (and javascipt also), but in the makecode however it does not work nor effect. I know what I say since I used to use them years ago. Maybe this is a bug, try this, nothing will happen no error codes also:
array_string = “1,2,3,4,5,A”
array_s = array_string.split(",")
for i in range(array_s.length):
basic.show_string(array_s[i])

Erkman

Here’s a working example (on the V2) of the datetime being used with Bluetooth: https://makecode.microbit.org/_hHu51icChhdM

string split is finicky and doesn’t work well. I may be good to open an issue on the GitHub repo for pxt showing your example. That being said, here’s an example that does work:

array_string = ""

def on_forever():
    global array_string
    array_string = "1,2,3,4,5,A"
    for value in array_string.split(","):
        serial.write_line("" + (value))
basic.forever(on_forever)

Link to example: https://makecode.microbit.org/_A04TAgD2V3E0

Bill