Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to BBC micro:bit and Micro Python

Introduction to BBC micro:bit and Micro Python

Andrea Grandi

April 17, 2016
Tweet

More Decks by Andrea Grandi

Other Decks in Programming

Transcript

  1. WHO AM I Note: I’m not affiliated with nor I

    work for BBC. Opinions are my own and don’t represent those of my employer. • Andrea Grandi • I live in London (UK) • Software Engineer at Marks & Spencer • Python/Django developer • passionate about micro controllers, IoT, Arduino, Golang and mobile development
  2. HOW I GOT INVOLVED INTO THE PROJECT • Invited by

    Nicholas Tollervey (one of the MicroPython/microbit core developers) at British Computing Society • A few Python developers met with selected teachers • Damien George (creator of MicroPython) was present • I got a precious micro:bit board
  3. WHAT IS MICROPYTHON • a reduced version of Python 3

    optimised to run on microcontrollers • created thanks to a Kickstarter campaign by Damien George • open source (MIT): github.com/micropython • 2700+ stars, 540+ forks • Initially created for the PyBoard • Includes a REPL (read-eval-print-loop)
  4. WHY MICROPYTHON • micro controllers have limited resources available •

    need for stripped down version of Python (no complex numbers etc…) • Python is easier to use, compared to C (faster to create prototypes)
  5. What is micro:bit project • BBC will distribute a free

    board to all 7th grade kids in UK (~ 1M boards!) • Kids should learn programming in a funny way and with more direct access to computer components • encourage kids to build things and not just being consumers • It will be available for purchase later for everyone else • released as open source once completed
  6. The board • mainly developed by BBC • most of

    the costs covered by nearly 30 partners (designers, manufacturers, distributors, etc… ) • ARM 32-bit 16 MHz CPU (nRF51822 Cortex-M0) • 256 KB flash • 16 KB RAM • 25 leds (5 x 5) • 3.3 V power needed • 5 cm x 4.2 cm size
  7. Available sensors and I/O • 3-axis accelerometer • compass •

    2 user buttons plus 1 reset • Bluetooth LE • GPIO pins for analog and digital I/O • USB port for communication, power and flashing
  8. I/O pins • 19 pins available in total • 0-16

    and 19-20 • 17 and 18 unavailable • analog: 3, 4, 10 • digital: all the others • pins can be used with crocodile clips or with a proper adaptor
  9. PyBoard • first board to get MicroPython • STM32F405RG microcontroller

    • 168 MHz Cortex M4 CPU • 1024 KB flash ROM and 192 KB RAM • Micro USB for power/serial • Micro SD card slot • 3-axis accelerometer (MMA7660) • 24 GPIO on left and right edges and 5 GPIO on bottom row • leds, buttons
  10. ESP8266 • available from different producer • 32-bit RISC CPU

    - 80 MHz • 64 KB instruction RAM, 96 KB data RAM • IEEE 802.11 b/g/n Wi-Fi • Integrated TR switch, balun, LNA, power amplifier and matching network • WEP or WPA/WPA2 authentication, or open networks • 16 GPIO pins • cheap and easy to find
  11. How to develop for BBC micro:bit • write the code

    using one of the available languages • compile it to get the *.hex file • copy the *.hex file on the device, connecting it with the micro USB cable • micro:bit led will start blinking, it will stop when it’s flashed • that’s it!
  12. Microsoft Block Editor • drag & drop blocks to describe

    what your program has to do • similar to Scratch • web based • needs Internet connection
  13. Code Kingdoms JavaScript • visual JavaScript editor • can switch

    between code editor and visual editor • web based • needs Internet connection
  14. Python (online IDE) • a simple editor for Python •

    written by Nicholas Tollervey with the help of the community • web based • needs Internet connection • produces a *.hex file you need to drag & drop on the mounted board
  15. Mu (offline Python IDE) • editor for Python • written

    by Nicholas Tollervey with the help of the community • can flash the device directly • integrated REPL • written in Python/Qt5 • open source • works offline (perfect for schools that want to avoid young kids going online)
  16. Requirements • a microbit board • a micro USB cable

    • any computer with Linux, OSX or Windows • basic Python knowledges • a good IDE (my advice is Mu)
  17. Python REPL • type Python instructions directly • minimum code

    completion available • quickest way to test microbit code before flashing a full app • REPL window can be used to debug print stuff from a flashed app
  18. Hello PyCon # microbit module let you control # the

    board components and sensors from microbit import display # This text will scroll on the 5x5 # matrix display display.scroll('Hello PyCon Italy')
  19. Using buttons from microbit import * while True: if button_a.is_pressed():

    display.show(Image.HAPPY) elif button_b.is_pressed(): break else: display.show(Image.SAD) display.clear()
  20. Displaying Images from microbit import display, Image # a few

    preloaded images are # available inside the Image class display.show(Image.PACMAN)
  21. Random module from microbit import * import random names =

    [ "Mary", "Yolanda", "Damien", "Alia", "Kushal", "Mei Xiu", "Zoltan"] display.scroll(random.choice(names))
  22. Accelerometer (1) from microbit import * while True: reading =

    accelerometer.get_x() if reading > 50: display.show("R") elif reading < -50: display.show("L") else: display.show("-")
  23. Accelerometer (2) from microbit import display, Image, accelerometer, sleep display.show(Image.HAPPY)

    # if you shake the device, you make microbit sad while True: if accelerometer.was_gesture('shake'): display.show(Image.SAD) sleep(2000) display.show(Image.HAPPY)
  24. Prototyping with breadboard toolkit • Sold by Kitronik • includes

    a breadboard and a socket connector • cables and microbit are not included • you can use all the available GPIO with this toolkit
  25. Where to find help • IRC: FreeNode - #microbit, #micropython

    • mailing list: [email protected] • documentation: https://microbit-micropython.readthedocs.org • Twitter: @micropython
  26. How to stay in touch • blog: https://www.andreagrandi.it • Twitter:

    @andreagrandi • GitHub: https://github.com/andreagrandi • email: [email protected] • IRC: Andy80 on FreeNode (#python, #django, #microbit, #micropython ) • PGP: 7238 74F6 886D 5994 323F 1781 8CFB 47AD C384 F0CC
  27. How can I get one? • there are 3 microbits

    available • fork https://github.com/andreagrandi/microantani on GitHub • create a small app for microbit in MicroPython • put the file in the root naming it firstname_lastname.py • make a pull request • the first 3 working examples I will receive will entitle the author to get a microbit for free • complete instructions and rules available in README.md • winners will be announced on Twitter @andreagrandi • boards will be distributed at 17:00 at the registration desk