Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Writing micro:bit programs

Writing micro:bit programs

Remember and Use Values

Variables

Variables are names you can create for storing, retrieving, and using values in the microbit module’s memory.  Here are three example variable declarations from the next program:

a = 42
c = "m"
d = 42.06

A variable can hold different kinds of values, such as integers (whole numbers),  decimal numbers, and characters.  Each of these kinds of values uses a different data type.

The declaration a = 42 creates a variable named a.  The value 42 is an integer, so the Python language automatically assigns the int data type to the a variable, and gives a an initial value of 42. If you do not want an initial value, you can assign a value of None to the variable, such as a = None.

Next, c = “m” declares a variable named c and initializes it to the value “m.” Since “m” is a character,  Python assigns c the data type str for storing strings or text.    

Then, d = 42.06  declares a variable named d.  The value 42.06 is a decimal number, so Python assigns d the data type float, which can hold floating-point decimal values.

Example script: variables

Now that you know how code can store values to memory, how can it retrieve and use them?  One way is to simply pass each variable to a method’s parameter. 

Here is an example, where display.scroll(Val) uses the display to scroll the value of the variable inside the parentheses.

  • Enter, name, save, and flash the script variables to your micro:bit.
#variables

from microbit import *

a = 42
c = "m"
d = 42.06

display.scroll(a)
display.scroll(c)
display.scroll(d)

The display will scroll the number 42, then it will scroll the letter m (shown below), then it will scroll the number 42.06.

 

Custom Images With Variables

Sometimes you might want to create your own images for the the micro:bit module display, which you can do with the Image method. The following example shows us how to make a custom image, and then how to store that image in a variable to make it easy to use in your script.

Example script: fading_H

  • Enter, name, save, and flash the script fading_H to your micro:bit.
#fading_H

from microbit import *

fading_H = Image("90001:"
                 "90001:"
                 "97531:"
                 "90001:"
                 "90001")
display.show(fading_H)

The example above creates an image of the letter H where the left side of the letter is very bright and the right side is dim. But how?

Notice that inside of the Image method’s parentheses, there are 5 rows of numbers and 5 columns of numbers. Each number corresponds to a different LED on the microbit, as one might expect. The top left number corresponds to the top left LED. The bottom right number corresponds to the bottom right LED.

The number 0 tells the micro:bit to turn the LED off. The number 9 tells the micro:bit to turn the LED on at maximum brightness. Any number between 0 and 9 turns the LED on with a partial brightness where a larger number corresponds to a brighter LED.

The image is then stored in a variable called fading_H. The last line of code uses display.show to show the fading_H image:

Your Turn

  • Reverse the fading_H image so that the left hand side of the image is dimmer than the right hand side of the image.
  • Create a custom image of a square.
  • Create a custom image of a triangle.
  • Light up the entire LED matrix with a fading brightness from the left hand side to the right hand side where the left hand side is the dimmest and the right hand side is the brightest.

 


Printer-friendly version
Custom Images
Prev
Solve Math Problems
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress