Getting Started with Embedded Python:
(MicroPython and CircuitPython)
By: Ayan Pahwa
About Me
Embedded Software
Engineer at Mentor
Graphics - A Siemens
Business
IoT Enthusiast
Security Researcher
FPV Drone Pilot
Mentor Graphics is world
leader in Electronics Design
Automation(Tools Business).
I work for Automotive
Embedded Software Division,
which deals in providing
custom solutions, OS and
BSP for IVI and ADAS
systems.
Contact: https://iayanpahwa.github.io
People who are really
serious about software
should make their own
hardware
- Alan Kay
Motivation
-
What is MicroPython
The MicroPython project is an open source
implementation of Python 3 that includes a small
subset of the Python standard libraries, and is
optimised to run on microcontrollers with constrained
environments like limited ROM, RAM and processing
power. It came about after a successful Kick-starter
campaign by Damien George.
Python 3
IoT
(Devices)
Microcontrollers
In a NutShell
Opportunities
~20MHz System Clock
~32Kb RAM
~16MB ROM
Single Core
Register Level Access
Microcontrollers
MicroPython
- A small stripped down version on Python3 which runs as firmware on microcontrollers,
exposes all the low level modules, acting as an operating system.
- It is packed full of advanced features such as an interactive prompt, arbitrary precision
integers, closures, list comprehension, generators, exception handling and more.
- Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.
- MicroPython aims to be as compatible with normal Python as possible to allow you to
transfer code with ease from the desktop to a microcontroller or embedded target.
- Python APIs for low level hardware modules- GPIOs, UART, PWM, ADC, i2c, SPI.
- Runs directly on bare-metal or in OS or web as emulator.
Python vs μPython vs CPython/PyPy vs Arduino
Refer: https://github.com/micropython/micropython/wiki
Boards Supported
The PyBoard
The ESP8266-NodeMCU
160Kb RAM
802.11 b/g/n
4MB Flash
GPIO, ADC, I2c, SPI
Functions & Libraries Supported
Interaction
Serial REPL (115200 BAUD RATE)
WEB REPL, works over LAN
File System mounts on host
Tools to transfer source code(ex: AMPY)
Emulation on linux host
Unicorn web based emulator(
Interaction: Serial
Loading uP on ESP8266 board
Install esptool - pip install esptool
Download uP firmware.bin from GitHub release pages for
your board.
Erase flash - esptool.py --port /path/to/ESP8266
erase_flash
Flash uP firmware - esptool.py --port /path/to/ESP8266 --
baud 460800 write_flash --flash_size=detect 0 firmware.bin
Connect Serial console - screen /dev/tty… 115200
Interaction: WebREPL
Setting up WebREPL
> import webrepl_setup
> Enter ‘E’ to enable it
> Enter and confirm password(defaults
micropythoN)
> Enter ‘y’ to reboot and save changes
Interaction: WebREPL
Interaction: Unicorn
https://micropython.org/unicorn
Interaction: Live
https://micropython.org/live
Interaction: Emulator
Interaction: h3llO WORlD
> from machine import Pin
> from time import sleep
> led = Pin(13, Pin.Out)
> led.on()
> sleep(1)
> led.off()
> sleep(1)
(DEMO)
HELLO WORLD OF ELECTRONICS
Interaction: Advance
File System on Flash to store:
WiFi credentials (SSID, PASSOWRD)
boot.py - POST operations
main.py - main executable
You can mount the fs over network, or
transfer files over webREPL or tools like
AMPY.
Interaction: Internals
CircuitPython
https://github.com/adafruit/circuitpython
Adafruit fork of MicroPython maintained for
educational purpose around boards sell by
Adafruit industries.
Centred Around ATMEL SAMD21 and ESP8266 SoCs.
Various new modules added like capacitive touch
APIs, Sound outputs, USB HID etc.
Bluetooth Low energy support with newly
supported NRF SoC port.
DISCLAIMER: Other than the stunts will be performed
by experts, No matter how many times you test before,
chances of live demo failures are incalculable
SHOW TIME
DEMOS

PyCon_India_2017_MicroPython_Ayan

  • 1.
    Getting Started withEmbedded Python: (MicroPython and CircuitPython) By: Ayan Pahwa
  • 2.
    About Me Embedded Software Engineerat Mentor Graphics - A Siemens Business IoT Enthusiast Security Researcher FPV Drone Pilot Mentor Graphics is world leader in Electronics Design Automation(Tools Business). I work for Automotive Embedded Software Division, which deals in providing custom solutions, OS and BSP for IVI and ADAS systems. Contact: https://iayanpahwa.github.io
  • 3.
    People who arereally serious about software should make their own hardware - Alan Kay Motivation -
  • 4.
    What is MicroPython TheMicroPython project is an open source implementation of Python 3 that includes a small subset of the Python standard libraries, and is optimised to run on microcontrollers with constrained environments like limited ROM, RAM and processing power. It came about after a successful Kick-starter campaign by Damien George.
  • 5.
  • 6.
  • 8.
    ~20MHz System Clock ~32KbRAM ~16MB ROM Single Core Register Level Access Microcontrollers
  • 9.
    MicroPython - A smallstripped down version on Python3 which runs as firmware on microcontrollers, exposes all the low level modules, acting as an operating system. - It is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling and more. - Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM. - MicroPython aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded target. - Python APIs for low level hardware modules- GPIOs, UART, PWM, ADC, i2c, SPI. - Runs directly on bare-metal or in OS or web as emulator.
  • 10.
    Python vs μPythonvs CPython/PyPy vs Arduino Refer: https://github.com/micropython/micropython/wiki
  • 11.
  • 12.
  • 13.
    The ESP8266-NodeMCU 160Kb RAM 802.11b/g/n 4MB Flash GPIO, ADC, I2c, SPI
  • 14.
  • 15.
    Interaction Serial REPL (115200BAUD RATE) WEB REPL, works over LAN File System mounts on host Tools to transfer source code(ex: AMPY) Emulation on linux host Unicorn web based emulator(
  • 16.
    Interaction: Serial Loading uPon ESP8266 board Install esptool - pip install esptool Download uP firmware.bin from GitHub release pages for your board. Erase flash - esptool.py --port /path/to/ESP8266 erase_flash Flash uP firmware - esptool.py --port /path/to/ESP8266 -- baud 460800 write_flash --flash_size=detect 0 firmware.bin Connect Serial console - screen /dev/tty… 115200
  • 17.
    Interaction: WebREPL Setting upWebREPL > import webrepl_setup > Enter ‘E’ to enable it > Enter and confirm password(defaults micropythoN) > Enter ‘y’ to reboot and save changes
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
    Interaction: h3llO WORlD >from machine import Pin > from time import sleep > led = Pin(13, Pin.Out) > led.on() > sleep(1) > led.off() > sleep(1)
  • 23.
  • 24.
    Interaction: Advance File Systemon Flash to store: WiFi credentials (SSID, PASSOWRD) boot.py - POST operations main.py - main executable You can mount the fs over network, or transfer files over webREPL or tools like AMPY.
  • 25.
  • 26.
    CircuitPython https://github.com/adafruit/circuitpython Adafruit fork ofMicroPython maintained for educational purpose around boards sell by Adafruit industries. Centred Around ATMEL SAMD21 and ESP8266 SoCs. Various new modules added like capacitive touch APIs, Sound outputs, USB HID etc. Bluetooth Low energy support with newly supported NRF SoC port.
  • 27.
    DISCLAIMER: Other thanthe stunts will be performed by experts, No matter how many times you test before, chances of live demo failures are incalculable SHOW TIME
  • 28.