Libraries are scripts developed for the specific purpose of enhancing MicroBlocks feature functionality and to provide a means to interface to the variety of micro devices and their capabilities.
While MicroBlocks comes with many libraries provided, it is also possible for the users to develop their own libraries.
To learn how to create your own library, see Creating A New Library.
Picture above shows the display when the Libraries + entry is clicked. This entry, when clicked, lists all the libraries that come with the MicroBlocks IDE.
As can be seen, there are six library categories (directories) listed. These categories contain the various hardware and software libraries as their names indicate. They will be described later.
In addition, there are seven individual library names listed below the directories. These form the most used library names, as such they will be addressed here first.
It should be noted that libraries can be loaded regardless of the attached micro device capabilities. Some library blocks will display error messages if they are used on a micro device lacking he functionality the library was intended for. Other blocks will simply execute with no visible impact or error messages.
Some libraries require hardware features that are not available on all boards. For example, the Radio library works only on boards with a Nordic nRF5x processor. Other libraries support external devices, such as Servo motors. When using a library, make sure you have the required hardware.
This describes the most frequently used libraries. These libraries enable users to access and control the sensors and hardware features found on the most popular microcontrollers. For example, here are the hardware features of the micro:bit v2:
The micro:bit has a combined accelerometer and magnetometer chip that provides 3-axis sensing and magnetic field strength sensing. It also includes some on-board gesture detection (such as fall detection) in hardware.
The NRF52 application processor has an on-board core temperature sensor and provides an estimate of ambient temperature.
These three blocks report the tilt values in three axis. The normal value range is -100 to +100 but values can reach -200 to 200 when shaken. (100 means 1 G of acceleration.)
Acceleration is the total acceration (or "G force") experienced by the device regardless of the direction. It is always positive, with a range of 0-346.
The light level has a maximum range of 0 - 1023. Light sensors vary in sensitivity, even among boards of the same type (e.g. micro:bits), so you'll need to experiment to discover the working range of your board's light sensor.
Temperature is reported in degrees Celsius. The range of the temperature sensor varies across boards but it is usually at least -40C to 50C. Note that the built-in temperature sensors are not calibrated and vary between boards. Check your board against a trusted thermometer to figure out how to many degrees to add or subtract to get the actual temperature.
The display is a 5x5 array of LEDs. It is connected to the micro:bit as a 5x5 matrix. Runtime software repeatedly refreshes this matrix at a high speed, such that it is within the user persistence of vision range, and no flicker is detected.
This LED matrix is also used to sense ambient light, by repeatedly switching some of the LED drive pins into inputs and sampling the voltage decay time, which is roughly proportional to ambient light levels.
The display matrix is live and can be manipulated by setting and unsetting the various LEDs. Once a pattern is set, the block will display the same pattern on the micro device LED panel.
Any pattern shown on the display will be cleared to all LEDs off.
Individual LED s can be turned on using this block. The range of the x and y parameters are 1-5.
X parameter represents LEDs in a row, from left to right.
Y parameter represents LEDs in a column, from top to bottom.
Individual LED s can be turned off using this block. The range of the x and y parameters are 1-5.
This block will display a single character on the LED display.
Displayed characters are limited to the ASCII set.
NeoPixel LED hardware are very interesting in that they contain multiple rgbLEDs in a compact housing. There can be from one to many LEDs packaged in some form of a strip or an array layout.
This library extends control over these LEDs by changing their colors and enable manipulation of single or multiple LEDs.
NeoPixel LED hardware is usually driven by an assigned pin. Also, external NeoPixel strips can be attached to micro devices and be controlled over a designated pin.
This block sets up the NeoPixel hardware by specifying how many LEDs there should be and over what pin number they will be controlled.
Even if there is only a single NeoPixel LED in the hardware, this block allows defining virtual LEDs up to 10. These then can be manipulated using the other blocks in this library.
This block allows color settings for 10 NeoPixels. As mentioned above, if there is only a single NeoPixel in the system, one can still assign up to 10 NeoPixel colors virtually.
The numbering sequence (1-10) is from left to right and top to bottom.
All NeoPixel LEDs will be turned off.
All defined NeoPixels (real or virtual) will ve set to the same color specified.
NeoPixel with the specified number will be set to color selected. These can be real or virtual NeoPixels.
Since NeoPixels are usually in a strip or array format, this block rotates the control by the specified increment, thus activating that particular one. If the NeoPixels are virtual, then the specified LEDs color will be displayed on the single LED present.
This reporter block enables the setting of the color in RGB format, by specifying three values of 0-255 for each of the three colors, red, green, and blue.
This reporter block returns a random color code that can be used in blocks that allow NeoPixel color input.
micro:bit devices with an on-board 2.4GHz transceiver make it possible to implement a microbit-radio protocol. This protocol provides a very simple small-packet broadcast radio interface between other devices that support it. Payload size varies from a standard 32 bytes to 255 bytes (if configured).
Additionally, the software adds a group code (0-255) to each data payload, allowing for simple user managed device addressing and filtering to take place.
Radio channel transmission speed are 1Mbps or 2Mbps. Transmmission power can be modulated between eight user configurable settings from 0(-30dbm) to 7 (+4dbm).
Used to send packets with numeric payloads.
On the receiving end, this block will generate a message type of number, and can be retrieved by the use of radio last number block.
Used to send packets with string payloads. Normal packet sizes allow sending up to 19 bytes. However, longer messages are possible by breaking down the messages into 19 byte chunks via user code.
On the receiving end, this block will generate a message type of string, and can be retrieved by the use of radio last string block.
Used to send packets with pair type payloads, where the pair consists of a string value and a number value.
On the receiving end, this block will generate a message type of pair, and can be retrieved by the use of both the radio last string and radio last number blocks, where these represent the two parts of the pair: string = number.
When the micro device eceives a radio message, this block will return true.
By using it in an event check block, such as when <condition>, it is possible to program responses to radio messages received.
Used to get the content of the last numeric radio message received.
Used to get the content of the last string radio message received.
The group number is used to enable a level of networking amongst devices with radio capability. Same radio group number, ranging from 0-255, is configured for each micro device that belongs to the same group. All devices sharing the same group number can then exchange messages.
Group numbers can be changed whenever needed. The default group number is 0. If the group number is dynamically changed while in operation, and then the micro device is reset, the group number will default to 0 again and lose the previous number.
The only way to consistently change and assign a different group number is via the of this block with the user script.
Radio transmission ranges vary depending on the transmission strength of the signal.
Radio power can be set, starting at 0 being the weakest and shortest distance, to 7 being the strongest and longest distance.
Radio power can be used to detect distance between different micro devices, and develop clever applications that make use of this feature.
The 5x5 matrix size of the LED displays on some models of the micro devices are rather restricting to display lengthy text and numeric data.
To provide a bit better support for the longer varieties of data, the scrolling library comes in handy. It has the ability to slide text and numbers across the screen with varying speeds that the user can control.
Will move text from right to left on the LED display panel.
The length of the text can be any number of characters, barring any memory limitations for the storage of the string; usually about 600 - 1000 characters.
An optional pause parameter allows control of the scrolling speed. The higher the delay, the slower the scroll speed is.
Will move numbers from right to left on the LED display panel.
The supported number range is -1073741824 to 1073741823.
An optional pause parameter allows control of the scrolling speed. The higher the delay, the slower the scroll speed is.
Stops any in progress scrolling activity.
A servo combines a motor and digital controller into a single unit. There are two kinds of servos. A position servo (the original form of hobby servo motor) moves the servo arm to a fixed position (angle) and holds that position. Position servers have a fixed range of angles, usually -90 to +90 degrees. A continuous rotation servo motor spins continuously, and its speed and direction (clockwise or counterclockwise) can be controlled.
Both types of servo are controlled by sending a series of digital pulses on the control line. The width of the pulse determins either the angle of the position servo or the speed and direction of the continuous rotation servo.
The set servo to degrees command is meant to control position servos while the set servo to speed block is meant to control a continuous rotation servos. However, since the control signals are the same, a position servo will respond to the speed command or vice versa. It is up to the the user to use the command that matches the servo they are using.
Sets the angle of a position servo. It takes time for the servo to change positions, so it's good add a **wait milliseconds" block after this block. The servo will remain at the given angle until told to move to another postion.
Sets the speed of a continuous rotation servo. The absolute value of the argument determines the power level and negative values make the servo rotate in the opposite direction from positive ones.
Many servos stall (stop turning) if the power level is too low. There is a lot of variation between servos, you'll need to experiment to find the power levels that work best for your servo.
Stops sending servo control pulses. This will make continuous rotation servos stop rotating. Position servos will remain in their last position.
The stop button in the IDE will also stop sending servo control pulses, which can be helpful if your servo has gotten out of control.
An on-board MEMs microphone provides a sound input to the micro:bit and a built in LED indicator on the front of the board shows the user when this is powered.
In addition to outputting sound via PWM on the pins, the micro:bit has a PCB mounted magnetic speaker to which sound output is mirrored.
Play the given note in the given octave for the given number of milliseconds. Note names are A-G (upper or lowercase) optionally followed by # for a sharp or _ for a flat.
Play the note specified by the key number on the piano keyboard (0-127) where middle C is 60. This block is useful when doing mathematical transformations of music (e.g. transposing to another key).
Play a note specified in Hertz (Hz). Middle C is about 261 Hz, the A above that is 440 Hz (the note to which orchestras tune).
Specify the pin used to play tones. A piezo buzzer or headphones are connected to this pin. On boards with built-in speakers, the built-in speaker will be used if this block is omitted.