Micro:bit Binary Communicator
by 8330728 in Circuits > Microcontrollers
498 Views, 0 Favorites, 0 Comments
Micro:bit Binary Communicator
![F7W7DZSKVIEGYDV.png](/proxy/?url=https://content.instructables.com/FB2/R4XU/KVIEGYDY/FB2R4XUKVIEGYDY.png&filename=F7W7DZSKVIEGYDV.png)
I made a micro:bit communicator that allows people to communicate in the code binary. Micro:bit 1 works as the transmitter and Micro:bit 2 works as the receiver. This means that Micro:bit 1 can send messages to Micro:bit 2 in binary. The buttons A+B must be pressed simultaneously to activate Micro:bit 1, then Button A can be pressed to send 0 and Button B can be pressed to send 1. Hence, the Micro:bit's work as a coded communication network.
Supplies
![Screen Shot 2021-10-18 at 4.11.11 pm.png](/proxy/?url=https://content.instructables.com/FZL/9PI0/KVIEGYIL/FZL9PI0KVIEGYIL.png&filename=Screen Shot 2021-10-18 at 4.11.11 pm.png)
This project was entirely constructed in TinkerCad for circuits.
Creating a New Project
![Screen Shot 2021-11-03 at 4.15.21 pm.png](/proxy/?url=https://content.instructables.com/FCB/2759/KVIEGK8Z/FCB2759KVIEGK8Z.png&filename=Screen Shot 2021-11-03 at 4.15.21 pm.png)
- Go to the TinkerCad website at; https://www.tinkercad.com
- Log in or create account
- Go to the circuits section
- Press create new circuit
Setting Up the Circuits
![Screen Shot 2021-11-03 at 4.22.10 pm.png](/proxy/?url=https://content.instructables.com/FKM/7DW6/KVIEGKC6/FKM7DW6KVIEGKC6.png&filename=Screen Shot 2021-11-03 at 4.22.10 pm.png)
- Drag and drop two Micro:bits into the workspace
- Name the Micro:bits 1 & 2 so that they don't get confused
- Enter the code section
- Change the code from blocks to text
- Check it says ''# Python code'' at the top
Micro:bit 1 - Coding the Start Pt1
![Screen Shot 2021-11-03 at 12.24.17 pm.png](/proxy/?url=https://content.instructables.com/FSQ/LGKV/KVIEGKR2/FSQLGKVKVIEGKR2.png&filename=Screen Shot 2021-11-03 at 12.24.17 pm.png)
Micro:bit 1 is exclusively a transmitter which can be used to send messages to Micro:bit 2 which is a receiver. It can be adapted to be both my adding the receiver code from Micro:bit 2 to Micro:bit 1.
- Select Micro:bit 1 and enter the code section
- Here we are going to write the lines of code that start the program.
- The first line starts a forever loop
- The next ensures that it keeps going so long as the variable remains true(which can't be changed, so it will stay true).
- After this the Micro:bit shows a smiley face (basic.show_icon(IconNames.Happy)) to prove its on and the program is working.
- After a couple seconds, the micro:bit provides the instructions; PRESS AB TO START. (basic.show_string("PRESS AB TO START"))
- Lastly, it sets the radio group to 14 (radio.set_group(14)) so that the two micro:bits are on the same radio group.
- On a new line, with no hashtag, type;
def on_forever():
if True:
basic.show_icon(IconNames.Happy)
basic.pause (1000)
basic.show_string("PRESS AB TO START")
radio.set_group(14)
#(If It doesn't work check the indentation)
Micro:bit 1 - Coding the Start Pt2
![Screen Shot 2021-11-03 at 4.49.41 pm.png](/proxy/?url=https://content.instructables.com/F52/5D9U/KVIEGLMZ/F525D9UKVIEGLMZ.png&filename=Screen Shot 2021-11-03 at 4.49.41 pm.png)
This section of code displays the remainder of the instructions
- The if statement, says that if the buttons A & B are pressed simultaneously the program begins
- It then displays the letter A (basic.show_string("A")) and number 0 (basic.show_number(0)), This shows that the button A will send the number 0 to micro:bit 2
- The micro:bit then shows the letter B (basic.show_string("B")) and number 1(basic.show_number(1)), This shows that the button B will send the number 1 to micro:bit 2
- basic.pause(1000), means that the program will wait for 1 second.
- After the previous line of code type;
if input.button_is_pressed(Button.AB):
basic.show_string("A")
basic.pause(1000)
basic.show_number(0)
basic.pause(1000)
basic.show_string("B")
basic.pause(1000)
basic.show_number(1)
basic.pause(2000)
#(If It doesn't work check the indentation)
Micro:bit 1 - Coding Button A
![Screen Shot 2021-11-03 at 4.51.28 pm.png](/proxy/?url=https://content.instructables.com/FUH/N8TR/KVIEGLNG/FUHN8TRKVIEGLNG.png&filename=Screen Shot 2021-11-03 at 4.51.28 pm.png)
The following section of code programs button A to send 0 when pressed then display a tick to show it was sent
- if input.button_is_pressed(Button.A): - Checks that button A was pressed and starts this section of code
- basic.clear_screen() - Clears what is currently on the micro:bit's screen
- basic.show_string("0") - Displays the 0 which will be sent to micro:bit 2
- basic.pause(2000) - Waits for 2 seconds to make sure you see what will be sent
- radio.send_string("text") - Sends the message to micro:bit 2
- basic.clear_screen() - Clears the screen
- basic.show_icon(IconNames.Yes) - Shows a tick to confirm the message has sent.
- Type;
if input.button_is_pressed(Button.A):
basic.clear_screen()
basic.show_string("0")
basic.pause(2000)
radio.send_string("text")
basic.clear_screen()
basic.show_icon(IconNames.Yes)
Micro:bit 1 - Programming Button B
![Screen Shot 2021-11-03 at 4.59.21 pm.png](/proxy/?url=https://content.instructables.com/FGU/B8KP/KVIEGMMB/FGUB8KPKVIEGMMB.png&filename=Screen Shot 2021-11-03 at 4.59.21 pm.png)
The following section of code programs button B to send 1 when pressed then display a tick to show it was sent
- if input.button_is_pressed(Button.B): - Checks that button B was pressed and starts this section of code
- basic.clear_screen() - Clears what is currently on the micro:bit's screen
- basic.show_number(1) - Displays the 1 which will be sent to micro:bit 2
- basic.pause(2000) - Waits for 2 seconds to make sure you see what will be sent
- radio.send_number(1) - Sends the message to micro:bit 2
- The reason that button A sends a string and button B sends a number is because if they are the same micro:bit can occasionally confuse the signals, doing it this way prevents any chance of this happening.
- basic.clear_screen() - Clears the screen
- basic.show_icon(IconNames.Yes) - Shows a tick to confirm the message has sent.
- Type;
if input.button_is_pressed(Button.B):
basic.clear_screen()
basic.show_number(1)
basic.pause(2000)
radio.send_number(1)
basic.clear_screen()
basic.show_icon(IconNames.Yes)
Micro:bit 1 - Last Step
![Screen Shot 2021-11-03 at 5.03.38 pm.png](/proxy/?url=https://content.instructables.com/FVI/4DR7/KVIEGN4T/FVI4DR7KVIEGN4T.png&filename=Screen Shot 2021-11-03 at 5.03.38 pm.png)
This last bit of code shows if the program has failed and activates the code
- else: - This means that the line of indented code after will only occur if the initial if statement if not True
- basic.show_icon(IconNames.Sad) - This displays a sad face so that the user can visually see that the program has failed.
- basic.forever(on_forever) - This calls/activates the forever loop that was defined earlier. All of the code is within this loop, without this call line nothing will work.
- Type;
else:
basic.show_icon(IconNames.Sad)
basic.forever(on_forever)
Micro:bit 2 - Programming the Start
![Screen Shot 2021-11-03 at 5.36.37 pm.png](/proxy/?url=https://content.instructables.com/FYW/DOW1/KVIEGRN7/FYWDOW1KVIEGRN7.png&filename=Screen Shot 2021-11-03 at 5.36.37 pm.png)
This section of code is used to define variables and the forever loop
In this section the forever loop is exclusively for the radio group as this micro:bit is a receiver not a transmitter. It can be adapted to be both by adding the transmitter code from Micro:bit 1 to Micro:bit 2.
- Change the Micro:bit being coded from Micro:bit 1 to Micro:bit 2 using the drop down in the right hand corner.
- Change the code style from blocks to text
- value = 1 - This defines the value variable as 1, this is what Micro:bit 1 will be sending via radio when it wants the number 1 displayed
- text = "text" - This defines the text variable as text, this is what Micro:bit 1 will be sending via radio when it wants the number 0 displayed
- def on_forever(): - Creates a forever loop for the radio signal to be in so that it never changes
- radio.set_group(14) - Sets the radio group to 14 so that Micro:bit 1 & 2 are on the same radio group.
- basic.forever(on_forever) - Calls/activates the forever loop
- Type;
value = 1
text = "text"
def on_forever():
radio.set_group(14)
basic.forever(on_forever)
Micro:bit 2 - Programming the Receiver of Button A
![Screen Shot 2021-11-03 at 5.37.09 pm.png](/proxy/?url=https://content.instructables.com/FIE/ZT89/KVIEGRO0/FIEZT89KVIEGRO0.png&filename=Screen Shot 2021-11-03 at 5.37.09 pm.png)
This section of code is used to define what will happen when the button A signal is received
- def on_radio_received_number(arg0): - This starts the definition of what will happen when button A's signal is recieved
- global value, value = arg0 - This defines value as 'global' or the same across the whole program and states that value is equal to arg0
- basic.show_number(1) - displays the number that button A sent, which is number 1 on Micro:bit 2
- basic.pause(2000) - Pauses the program for 2 seconds so that the user can see the display
- basic.clear_screen() -Clears the screen
- basic.show_icon(IconNames.Yes) - Displays a Tick to show that the message was received
- basic.clear_screen() - Clears the screen
- radio.on_received_number(on_radio_received_number) - Calls/activates the code block
- Type;
def on_radio_received_number(arg0):
global value
value = arg0
basic.show_number(1)
basic.pause(2000)
basic.clear_screen()
basic.show_icon(IconNames.Yes)
basic.pause(2000)
basic.clear_screen()
radio.on_received_number(on_radio_received_number)
Micro:bit 2 - Programming the Receiver of Button B
![Screen Shot 2021-11-03 at 5.37.55 pm.png](/proxy/?url=https://content.instructables.com/FLH/7Z2Y/KVIEGROV/FLH7Z2YKVIEGROV.png&filename=Screen Shot 2021-11-03 at 5.37.55 pm.png)
This section of code is used to define what will happen when the button B signal is received
- def on_radio_received_string(arg0): - This starts the definition of what will happen when button B's signal is recieved
- global text, text = arg0 - This defines value as 'global' or the same across the whole program and states that text is equal to arg0
- basic.show_number(0) - displays the number that button A sent, which is number 0 on Micro:bit 2
- basic.pause(2000) - Pauses the program for 2 seconds so that the user can see the display
- basic.clear_screen() - Clears the screen
- basic.show_icon(IconNames.Yes) -
- basic.pause(2000) - Pauses the program for 2 seconds so that the user can see the display
- basic.clear_screen() - Clears the screen
- radio.on_received_string(on_radio_received_string) - Calls/activates the code block just defined
- Type;
def on_radio_received_string(arg0):
global text
text = arg0
basic.show_number(0)
basic.pause(2000)
basic.clear_screen()
basic.show_icon(IconNames.Yes)
basic.pause(2000)
basic.clear_screen()
radio.on_received_string(on_radio_received_string)