« Turtorial Examples for ESP32-MicroPython | トップページ | Turtorial Examples for CC3200-MicroPython »

2020年3月10日 (火)

Turtorial Examples for RpiZero-MicroPython

2020/3/10

Turtorial Examples for RpiZero-MicroPython

RpiZero_BareMetal

Quick reference for the Raspberry Pi Zero W MicroPython

LEDonBoard_blink.py

import utime from machine import Pin # set gpio47 to output mode led=Pin(47,Pin.OUT) # LED on board while(1): led(0) # LED on utime.sleep_ms(300) led(1) # LED off utime.sleep_ms(300)

Pin/GPIO

from machine import Pin ld = Pin(47, Pin.OUT) # create output pin on GPIO47 (on-board LED) ld.value(0) # set pin to off/low ld(1) # set pin to on/high p0 = Pin(17, Pin.IN) # create input pin on GPIO17 (board pin #11) print(p0.value()) # get value, 0 or 1 p2 = Pin(27, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor p2.value() p3 = Pin(22, Pin.OUT, value=1) # set pin high on creation p3.value() p3 = Pin(22, Pin.OUT, value=0) p3.value()

PWM

The hardware PWM only works on following pins:
GPIO12, GPIO18, GPIO40, GPIO52 for PWM0,
GPIO13, GPIO19, GPIO41, GPIO45, GPIO53 for PWM1.

You may also use ID of 0 or 1 without specifying output pin
if you want to configure alternate functions separately.

The default PWM system clock is configured to be 960KHz and
the default PWM period is 960.
It means that the default PWM output frequency is 1KHz
and you can change its duty cycle in 1/960 step.

import math from time import sleep from machine import PWM, Pin p18 = PWM(Pin(18)) # use GPIO18(p12) for PWM0 p18.init(freq=1000) # 1000Hz (ticks = 960000/freq) for p in range(100): p18.duty_u16(math.floor(65536*p/100))# PWM duty = arg/65536 sleep(0.05) for p in range(100): p18.duty_u16(math.floor(65536-65536*p/100)) sleep(0.05) p18.active(False) # stop PWM0 output

ADC

Hardware is not available.

SPI

The hardware SPI bus only works on following pins:
MOSI is board pin #19 = GPIO10,
MISO is board pin #21 = GPIO9,
SCK is board pin #23 = GPIO11.
Software SPI is currently not implemented.

Use the machine.SPI class with bus ID of 0. .

see NeoPixel(are using SPI)

I2C

The hardware I2C bus only works on following pins:
SDA is board pin #3 = GPIO2,
SCL is board pin #5 = GPIO3.
Software I2C is currently not implemented.

Use the machine.I2C class with bus ID of 1.
Bus 0 and bus 2 are not available for users.

Nucleo_TMP102_test.py

# Nucleo import utime from machine import Pin, I2C from tmp102 import Tmp102 i2c = I2C(1) sensor = Tmp102(i2c, 0x48) while True: print('Temperature: {0:.1f}'.format(sensor.temperature)) utime.sleep(2)

RpiZero_demo_i2c_LCD.py

# Note: VCC needs 5V import i2c_lcd from machine import Pin,I2C from time import sleep i2c = I2C(1) d = i2c_lcd.Display(i2c) d.clear() d.home() d.write('LCD I2C test .py') d.move(0,1) d.write('Hello World ') d.color(0xFF,0,0) sleep(0.5) d.color(0,0xFF,0) sleep(0.5) d.color(0,0,0xFF) sleep(0.5) d.color(0x44,0xFF,0x44)

RTC

Not Implemeted??

>>> from machine import RTC Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name RTC >>>

NeoPixel

module install:

git clone https://github.com/JanBednarik/micropython-ws2812.git cd micropython-ws2812 Patch ws2812.py to ws2812_rpi.py as follows: patch#1: import pyb -> import machine as pyb patch#2: self.spi = pyb.SPI(spi_bus, pyb.SPI.MASTER, baudrate=3200000, polarity=0, phase=1) -> self.spi = pyb.SPI(spi_bus,baudrate=3200000, polarity=0, phase=1) patch#3: self.spi.send(self.buf) -> self.spi.write(self.buf) patch#4 gc.collect() -> # gc.collect() # don't use gc.collect(), that take a time because of having huge RAM Copy ws2812_rpi.py to boot SD.

sample1:

# connect the following pin to DIN of nexpixels # Pin#19 = GPIO10 (if spi_bus=0) from ws2812_rpi import WS2812 chain = WS2812(spi_bus=0, led_count=4) data = [ (255, 0, 0), # red (0, 255, 0), # greenp (0, 0, 255), # blue (85, 85, 85), # white ] chain.show(data)

sample2:

from time import sleep import math from ws2812_rpi import WS2812 ring = WS2812(spi_bus=0, led_count=16, intensity=0.1) def data_generator(led_count): data = [(0, 0, 0) for i in range(led_count)] step = 0 while True: red = int((1 + math.sin(step * 0.1324)) * 127) green = int((1 + math.sin(step * 0.1654)) * 127) blue = int((1 + math.sin(step * 0.1)) * 127) data[step % led_count] = (red, green, blue) yield data step += 1 for data in data_generator(ring.led_count): ring.show(data) sleep(0.01)

DHT

N.A.

Networking

N.A.

Performance Test

performanceRpiZero.py

# Peformace Test RpiZero import time def performanceTest(): msec = time.ticks_ms endTime = msec() + 10000 count = 0 while msec() < endTime: count += 1 print("Count: ", count) performanceTest()

以上

|

« Turtorial Examples for ESP32-MicroPython | トップページ | Turtorial Examples for CC3200-MicroPython »

linux」カテゴリの記事

RaspberryPi」カテゴリの記事

MicroPython」カテゴリの記事

コメント

コメントを書く



(ウェブ上には掲載しません)




« Turtorial Examples for ESP32-MicroPython | トップページ | Turtorial Examples for CC3200-MicroPython »