So, let's test our micro: bit before programming it. There are two ways to test your micro: bit.
Now, let's move to the coding part. Let us see how we can code and display "Our Name" manually. Below are the steps to follow.
This is a platform which provides an IDE to write our code for micro: bit using different programming languages like JavaScript and Python. Here, we can write our code, compile it, and we can generate HEX file to run on our micro: bit. So, let us see how can we display our name using MicroPython.
Go to https://www.microbit.co.uk/app/ and click on "Create code".
After clicking, it will ask you to choose a programming language in which you want to code for your micro: bit, like JavaScript, Python, Touch Develop, and Block Editor. Actually, Block and Touch Develop is the easiest method to write code for micro: bit, just by dragging and dropping the blocks as I have shown in this article. But I am going to show how to write code for micro: ibit using MicroPython. So, let's choose MicroPython.
After choosing MicroPython, wait for some seconds. It will prepare things for you and you will be presented with the following screen where you can see there are different sections/ tools, like Download, Snippets, Help and Save.
Download - This is used to compile your Python code and download HEX file to run on Micro:Bit.
Snippets - This is used to add code snippets in your program.
Help - This is used to get some help if you are stuck anywhere in the middle.
And, we can see that there are other buttons to zoom in and zoom out the IDE and save our code there.
Let's get started and write our Python code and run it on micro: bit. First, you need to delete that default code. You can run this code also but I am going to delete it and write it again with the description so that you can understand it better. So first, delete that code and follow the steps.
I am going to add a micro:bit library by writing the following code.
The above code actually means that we want to use all the objects and functions/methods that are available within the micro:bit library, like controling the led display, displaying our name, displaying symbols, and much more.
- while True:
- display.scroll('CSHARP')
-
In the above code,
While is the keyword that starts the loop and
True is the keyword. Whatever is there under the
While, will execute again and again forever.
In the second line of code,
display is an object that refers to the LED on the micro:bit. Calling a method in the display will allow us to control the LEDs.
scroll() is the method used to display the string on the Micro:bit by scrolling the text across the display. You can display anything just by replacing the string by your string. So, the above code will display
CSHARP on the Micro: bit display.
- from microbit import *
- while True:
- display.scroll('Hello, World!')
This is for displaying the text on the micro:bit. Now, let us see how to display symbols on the micro:bit. For this, we are going to use display object angain and another method i.e., Show(). And, for this, we just need to add one line of code.
- display.show(Image.HAPPY)
The above line of code will display HAPPY Face on the micro:bit. There are a lot of different symbols available to use. Refer
this link to know more about it.
Now, add one more line of code for the delay.
Now, this line of code will provide a delay of 2000 nanoseconds which means that after complete execution of the loop, micro:bit will wait for 2000 ns and then, it will start the loop again. I have added two more lines of code that will display SAD and ANGRY faces on Micro:bit. Given below is the final code.
- from microbit import *
- while True:
- display.scroll('Hello, World!')
- display.show(Image.HAPPY)
- display.show(Image.SAD)
- display.show(Image.ANGRY)
- sleep(2000)
That's all for coding part. This is very easy.
Now, let us run this code on our micro:bit to see the output. You need to click on the Download button there and copy it to
MICROBIT drive. Let us see how to do this.
Plug in the Micro:bit device to a computer via USB.
After plugging in, you should see HELLO on your Micro:bit by default. Go to your computer or press Window +E (PC). For Linux, go to Files and you will see MICROBIT Drive there.
Now, download your code.
Go to your download folder and copy the downloaded Hex file in MICROBIT Drive.
That's all. You're good to go now. The output demo can be seen
here.