How do the radio group codes work with Javascript Blocks?

I haven't set a radio group, how does the micro:bit allocate group numbers?

Radio not communicating withJavascript Blocks.

Radio not working in Javascript Blocks.


The 'radio' blocks in Javascript Blocks sit on top of the radio interface, that is surfaced from the Lancaster University DAL (Device Abstraction Layer). This Radio interface is based on a proprietary short range wireless interface provided in the Nordic nRF51 software development kit.

The micro:bit runtime code (the DAL) puts a group code in the first byte of all payloads transmitted via the radio interface. At the receiving end, if this group code does not match the group code set by the receiving script, the payload is ignored by the receiver, and your script will never see that data payload.

Inside the Javascript Blocks editor, you can explicitly set the radio group with the radio->set group blocks like this:


The group number can be any number from 0 to 255. The group number at the transmitting end, and receiving end, must match, if you are to transfer data between two or more micro:bits.

In the JavaScript Blocks editor, if you do not explicitly set the group number, then PXT will allocate one for you. It does this by taking a 'hash' of your script, and choosing the radio group based on that. So, if you load the same script (same .hex file) into two or more micro:bits, they will automatically be set to use the same radio group by default.

If you have two different JavaScript Blocks editor scripts, then there is a 1 in 256 chance that they may be on the same radio group. But it is quite unlikely that the same group will be used. This is useful, because if you have multiple students working on radio projects, they can just leave the group code undefined, and providing the same script is loaded into two micro:bits, those two micro:bits will be able to communicate, without interfering with other micro:bits.

The radio interface on the micro:bit sends broadcast messages, so if multiple micro:bits have the same group code set then they will all be able to see the messages sent by others. It is also possible to change the group code while the micro:bit is running your code, so you could use different group codes for different purposes, on a single micro:bit.