Sie sind auf Seite 1von 10

MicroPython

MicroPython is a lean and efficient implementation of the Python 3programming language that includes a
small subset of the Python standard library and is optimised to run on microcontrollers and in constrained
environments.
The MicroPython pyboard is a compact electronic circuit board that runs MicroPython on the bare metal,
giving you a low-level Python operating system that can be used to control all kinds of electronic projects.

MicroPython 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 system.
TEST DRIVE A PYBOARD BUY A PYBOARD USE MICROPYTHON ONLINE

Proper Python with hardware-specific modules

MicroPython is a full Python compiler and runtime that runs on the bare-metal. You get an interactive
prompt (the REPL) to execute commands immediately, along with the ability to run and import scripts
from the built-in filesystem. The REPL has history, tab completion, auto-indent and paste mode for a great
user experience.

MicroPython strives to be as compatible as possible with normal Python (known as CPython) so that if you
know Python you already know MicroPython. On the other hand, the more you learn about MicroPython
the better you become at Python.

In addition to implementing a selection of core Python libraries, MicroPython includes modules such as
"machine" for accessing low-level hardware.

import pyb

# turn on an LED
pyb.LED(1).on()

# print some text to the serial console


print('Hello MicroPython!')


Example 1 / 7

The pyboard

The pyboard is the official MicroPython microcontroller board with full support for software features. The
hardware has:
o STM32F405RG microcontroller

o 168 MHz Cortex M4 CPU with hardware floating point

o 1024KiB flash ROM and 192KiB RAM

o Micro USB connector for power and serial communication

o Micro SD card slot, supporting standard and high capacity SD cards

o 3-axis accelerometer (MMA7660)

o Real time clock with optional battery backup

o 24 GPIO on left and right edges and 5 GPIO on bottom row, plus LED and switch GPIO available on
bottom row

o 3x 12-bit analog to digital converters, available on 16 pins, 4 with analog ground shielding

o 2x 12-bit digital to analog (DAC) converters, available on pins X5 and X6

o 4 LEDs (red, green, yellow and blue)

o 1 reset and 1 user switch

o On-board 3.3V LDO voltage regulator, capable of supplying up to 250mA, input voltage range 3.6V
to 16V
o DFU bootloader in ROM for easy upgrading of firmware
Visit the store to order!

Watch MicroPython in action

Completely free, open source software


MicroPython is written in C99 and the entire MicroPython core is available for general use under the very
liberalMIT license. Most libraries and extension modules (some of which are from a third party) are also
available under MIT or similar licenses.

You can freely use and adapt MicroPython for personal use, in education, and in commercial products.

MicroPython is developed in the open on GitHub and the source code is available at the GitHub page, and
on the download page. Everyone is welcome to contribute to the project.

Code: state-of-the-art and highly robust

MicroPython employs many advanced coding techniques, and lots of tricks to maintain a compact size
while still having a full set of features.

Some of the more notable items are:

o highly configurable due to many compile-time configuration


options

o support for many architectures (x86, x86-64, ARM, ARM Thumb,


Xtensa)

o extensive test suite with over 590 tests, and more than 18,500
individual testcases
o code coverage at 98.4% for the core and at 96.3% for the core plus
extended modules
o fast start-up time from boot to loading of first script (150
microseconds to get to boot.py, on PYBv1.1 running at 168MHz)
o a simple, fast and robust mark-sweep garbage collector for heap
memory

o a MemoryError exception is raised if the heap is exhausted

o a RuntimeError exception is raised if the stack limit is reached

o support for running Python code on a hard interrupt with minimal


latency

o errors have a backtrace and report the line number of the source
code

o constant folding in the parser/compiler

o pointer tagging to fit small integers, strings and objects in a


machine word
o transparent transition from small integers to big integers

o support for 64-bit NaN boxing object model

o support for 30-bit stuffed floats, which don't require heap memory

o a cross-compiler and frozen bytecode, to have pre-compiled scripts


that don't take any RAM (except for any dynamic objects they
create)

o multithreading via the "_thread" module, with an optional global-


interpreter-lock (still work in progress, only available on selected
ports)

o a native emitter that targets machine code directly rather than the
bytecode virtual machine

o inline assembler (currently Thumb and Xtensa instruction sets only)

Online resources

You can learn more about MicroPython and keep up-to-date with developments via the following
resources:
o subscribe to the newsletter
o read the documentation
o join the community at the forum
o submit bug reports, and follow and join in development on GitHub
Take me to the store!

uick reference for the pyboard


The below pinout is for PYBv1.0. You can also view pinouts for other versions of the
pyboard:PYBv1.1 or PYBLITEv1.0-AC or PYBLITEv1.0.
General board control
See pyb .

import pyb

pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)


pyb.wfi() # pause CPU, waiting for interrupt
pyb.freq() # get CPU and bus frequencies
pyb.freq(60000000) # set CPU freq to 60MHz
pyb.stop() # stop CPU, waiting for external interrupt
Delay and timing
Use the time module:

import time

time.sleep(1) # sleep for 1 second


time.sleep_ms(500) # sleep for 500 milliseconds
time.sleep_us(10) # sleep for 10 microseconds
start = time.ticks_ms() # get value of millisecond counter
delta = time.ticks_diff(time.ticks_ms(), start) # compute time difference
Internal LEDs
See pyb.LED.

from pyb import LED

led = LED(1) # 1=red, 2=green, 3=yellow, 4=blue


led.toggle()
led.on()
led.off()

# LEDs 3 and 4 support PWM intensity (0-255)


LED(4).intensity() # get intensity
LED(4).intensity(128) # set intensity to half
Internal switch
See pyb.Switch.

from pyb import Switch

sw = Switch()
sw.value() # returns True or False
sw.callback(lambda: pyb.LED(1).toggle())
Pins and GPIO
See pyb.Pin.

from pyb import Pin

p_out = Pin('X1', Pin.OUT_PP)


p_out.high()
p_out.low()

p_in = Pin('X2', Pin.IN, Pin.PULL_UP)


p_in.value() # get value, 0 or 1
Servo control
See pyb.Servo.
from pyb import Servo

s1 = Servo(1) # servo on position 1 (X1, VIN, GND)


s1.angle(45) # move to 45 degrees
s1.angle(-60, 1500) # move to -60 degrees in 1500ms
s1.speed(50) # for continuous rotation servos
External interrupts
See pyb.ExtInt.

from pyb import Pin, ExtInt

callback = lambda e: print("intr")


ext = ExtInt(Pin('Y1'), ExtInt.IRQ_RISING, Pin.PULL_NONE, callback)
Timers
See pyb.Timer.

from pyb import Timer

tim = Timer(1, freq=1000)


tim.counter() # get counter value
tim.freq(0.5) # 0.5 Hz
tim.callback(lambda t: pyb.LED(1).toggle())
RTC (real time clock)
See pyb.RTC

from pyb import RTC

rtc = RTC()
rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time
rtc.datetime() # get date and time
PWM (pulse width modulation)
See pyb.Pin and pyb.Timer.

from pyb import Pin, Timer

p = Pin('X1') # X1 has TIM2, CH1


tim = Timer(2, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)
ADC (analog to digital conversion)
See pyb.Pin and pyb.ADC.

from pyb import Pin, ADC

adc = ADC(Pin('X19'))
adc.read() # read value, 0-4095
DAC (digital to analog conversion)
See pyb.Pin and pyb.DAC.

from pyb import Pin, DAC

dac = DAC(Pin('X5'))
dac.write(120) # output between 0 and 255
UART (serial bus)
See pyb.UART.

from pyb import UART

uart = UART(1, 9600)


uart.write('hello')
uart.read(5) # read up to 5 bytes
SPI bus
See pyb.SPI.

from pyb import SPI

spi = SPI(1, SPI.MASTER, baudrate=200000, polarity=1, phase=0)


spi.send('hello')
spi.recv(5) # receive 5 bytes on the bus
spi.send_recv('hello') # send and receive 5 bytes
I2C bus
See pyb.I2C.

from pyb import I2C

i2c = I2C(1, I2C.MASTER, baudrate=100000)


i2c.scan() # returns list of slave addresses
i2c.send('hello', 0x42) # send 5 bytes to slave with address 0x42
i2c.recv(5, 0x42) # receive 5 bytes from slave
i2c.mem_read(2, 0x42, 0x10) # read 2 bytes from slave 0x42, slave memory 0x10
i2c.mem_write('xy', 0x42, 0x10) # write 2 bytes to slave 0x42, slave memory 0x10
CAN bus (controller area network)
See pyb.CAN.

from pyb import CAN

can = CAN(1, CAN.LOOPBACK)


can.setfilter(0, CAN.LIST16, 0, (123, 124, 125, 126))
can.send('message!', 123) # send a message with id 123
can.recv(0) # receive message on FIFO 0
Internal accelerometer
See pyb.Accel.

from pyb import Accel

accel = Accel()
print(accel.x(), accel.y(), accel.z(), accel.tilt())
Next Previous

Das könnte Ihnen auch gefallen