> For the complete documentation index, see [llms.txt](https://www.pythonclassroom.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pythonclassroom.com/microbit.md).

# microbit

{% embed url="<https://docs.google.com/presentation/d/1e1qwanzjt4JFRp2STMQQWAtkbIUml2bdQWcU_yA1wS8/edit?usp=sharing>" %}

## Python Editor for micro:bit

{% embed url="<https://python.microbit.org/v/2.0>" %}

## Button Press

```python
from microbit import *

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        display.scroll("AB")
    elif button_a.is_pressed():
        display.scroll("A")
    elif button_b.is_pressed():
        display.scroll("B")
    sleep(100)
```

## Detect Movement

```python
from microbit import *

while True:
    reading = accelerometer.get_x()
    if reading > 20:
        display.show("R")
    elif reading < -20:
        display.show("L")
    else:
        display.show("-")
```

## Random Numbers

```python
from microbit import *
import random

display.show(str(random.randint(1, 6)))
```

## Gestures

MicroPython is able to recognize the following gestures: up, down, left, right, face up, face down, freefall, 3g, 6g, 8g, shake. Gestures are always represented as strings. While most of the names should be obvious, the 3g, 6g and 8g gestures apply when the device encounters these levels of g-force (like when an astronaut is launched into space).

```python
from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    if gesture == "face up":
        display.show(Image.HAPPY)
    else:
        display.show(Image.ANGRY)
```

## Magic-8

A Magic-8 ball is a toy first invented in the 1950s. The idea is to ask it a yes/no question, shake it and wait for it to reveal the truth.

```python
from microbit import *
import random

answers = [
    "It is certain",
    "It is decidedly so",
    "Without a doubt",
    "Yes, definitely",
    "You may rely on it",
    "As I see it, yes",
    "Most likely",
    "Outlook good",
    "Yes",
    "Signs point to yes",
    "Reply hazy try again",
    "Ask again later",
    "Better not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Don't count on it"
    "My reply is no",
    "My sources say no",
    "Outlook not so good",
    "Very doubtful",
]

while True:
    display.show("8")
    if accelerometer.was_gesture("shake"):
        display.clear()
        sleep(1000)
        display.scroll(random.choice(answers))
```

## Built-in Images

* Image.HAPPY&#x20;
* Image.HEART\_SMALL&#x20;
* Image.HAPPY&#x20;
* Image.SMILE&#x20;
* Image.SAD&#x20;
* Image.CONFUSED&#x20;
* Image.ANGRY&#x20;
* Image.ASLEEP&#x20;
* Image.SURPRISED&#x20;
* Image.SILLY&#x20;
* Image.FABULOUS&#x20;
* Image.MEH&#x20;
* Image.YES&#x20;
* Image.NO&#x20;
* Image.CLOCK12&#x20;
* Image.CLOCK11&#x20;
* Image.CLOCK10&#x20;
* Image.CLOCK9&#x20;
* Image.CLOCK8&#x20;
* Image.CLOCK7&#x20;
* Image.CLOCK6&#x20;
* Image.CLOCK5&#x20;
* Image.CLOCK4&#x20;
* Image.CLOCK3&#x20;
* Image.CLOCK2&#x20;
* Image.CLOCK1&#x20;
* Image.ARROW\_N&#x20;
* Image.ARROW\_NE&#x20;
* Image.ARROW\_E&#x20;
* Image.ARROW\_SE&#x20;
* Image.ARROW\_S&#x20;
* Image.ARROW\_SW&#x20;
* Image.ARROW\_W&#x20;
* Image.ARROW\_NW&#x20;
* Image.TRIANGLE&#x20;
* Image.TRIANGLE\_LEFT&#x20;
* Image.CHESSBOARD&#x20;
* Image.DIAMOND&#x20;
* Image.DIAMOND\_SMALL&#x20;
* Image.SQUARE&#x20;
* Image.SQUARE\_SMALL&#x20;
* Image.RABBIT&#x20;
* Image.COW&#x20;
* Image.MUSIC\_CROTCHET&#x20;
* Image.MUSIC\_QUAVER&#x20;
* Image.MUSIC\_QUAVERS&#x20;
* Image.PITCHFORK&#x20;
* Image.XMAS&#x20;
* Image.PACMAN&#x20;
* Image.TARGET&#x20;
* Image.TSHIRT&#x20;
* Image.ROLLERSKATE&#x20;
* Image.DUCK&#x20;
* Image.HOUSE&#x20;
* Image.TORTOISE&#x20;
* Image.BUTTERFLY&#x20;
* Image.STICKFIGURE&#x20;
* Image.GHOST&#x20;
* Image.SWORD&#x20;
* Image.GIRAFFE&#x20;
* Image.SKULL&#x20;
* Image.UMBRELLA&#x20;
* Image.SNAKE


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pythonclassroom.com/microbit.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
