Skip to content

Commit 657bff2

Browse files
committed
Test: split machine snippets
Signed-off-by: Jos Verlinde <[email protected]>
1 parent 85598e0 commit 657bff2

File tree

16 files changed

+154
-191
lines changed

16 files changed

+154
-191
lines changed

snippets/check_rp2/check_machine.py

Lines changed: 0 additions & 140 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# ADC
2+
from machine import ADC, Pin
3+
4+
adc = ADC(Pin(26)) # create ADC object on ADC pin
5+
adc.read_u16() # read value, 0-65535 across voltage range 0.0v - 3.3v
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Software I2C
2+
3+
from machine import Pin, SoftI2C
4+
5+
i2c = SoftI2C(scl=Pin(5), sda=Pin(4), freq=100_000)
6+
7+
i2c.scan() # scan for devices
8+
9+
i2c.readfrom(0x3A, 4) # read 4 bytes from device with address 0x3a
10+
i2c.writeto(0x3A, "12") # write '12' to device with address 0x3a
11+
12+
buf = bytearray(10) # create a buffer with 10 bytes
13+
i2c.writeto(0x3A, buf) # write the given buffer to the peripheral
14+
15+
# Hardware I2C
16+
from machine import Pin, I2C
17+
18+
i2c = I2C(0) # default assignment: scl=Pin(9), sda=Pin(8)
19+
i2c = I2C(1, scl=Pin(3), sda=Pin(2), freq=400_000)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# I2S
2+
3+
from machine import I2S, Pin
4+
buf = bytearray(10) # create a buffer with 10 bytes
5+
6+
i2s = I2S(0, sck=Pin(16), ws=Pin(17), sd=Pin(18), mode=I2S.TX, bits=16, format=I2S.STEREO, rate=44100, ibuf=40000) # create I2S object
7+
i2s.write(buf) # write buffer of audio samples to I2S device
8+
9+
i2s = I2S(1, sck=Pin(0), ws=Pin(1), sd=Pin(2), mode=I2S.RX, bits=16, format=I2S.MONO, rate=22050, ibuf=40000) # create I2S object
10+
i2s.readinto(buf) # fill buffer with audio samples from I2S device
11+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# PWM
2+
3+
from machine import Pin, PWM
4+
5+
pwm0 = PWM(Pin(0)) # create PWM object from a pin
6+
pwm0.freq() # get current frequency
7+
pwm0.freq(1000) # set frequency
8+
pwm0.duty_u16() # get current duty cycle, range 0-65535
9+
pwm0.duty_u16(200) # set duty cycle, range 0-65535
10+
pwm0.deinit() # turn off PWM on the pin
11+
12+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# the hello world of IoT
2+
# This short script serves as a sanity check.
3+
# It makes the onboard LED blink
4+
5+
# ref: https://docs.micropython.org/en/latest/rp2/quickref.html
6+
7+
import utime as time
8+
from machine import Pin
9+
10+
11+
# Blink
12+
13+
# led = Pin()
14+
led = Pin(1, value=2)
15+
led = Pin(13, Pin.OUT)
16+
17+
18+
for i in range(2): # no infinite loop
19+
led.on()
20+
time.sleep_ms(250)
21+
led.off()
22+
time.sleep_ms(250)
23+
24+
25+
26+
27+
28+
29+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Timers
2+
3+
from machine import Timer
4+
5+
tim = Timer(period=5000, mode=Timer.ONE_SHOT, callback=lambda t: print(1))
6+
tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t: print(2))
7+
8+
9+
from machine import Timer
10+
11+
tim = Timer(period=5000, mode=Timer.ONE_SHOT, callback=lambda t: print(1))
12+
tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t: print(2))
13+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# UART
2+
from machine import UART, Pin
3+
4+
uart1 = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5))
5+
uart1.write("hello") # write 5 bytes
6+
uart1.read(5) # read up to 5 bytes
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# DS18S20 and DS18B20 devices:
2+
import time
3+
4+
import ds18x20
5+
6+
ds = ds18x20.DS18X20(ow)
7+
roms = ds.scan()
8+
ds.convert_temp()
9+
time.sleep_ms(750)
10+
for rom in roms:
11+
print(ds.read_temp(rom))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# NeoPixel
2+
3+
from machine import Pin
4+
from neopixel import NeoPixel
5+
6+
pin = Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels
7+
np = NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels
8+
9+
# if micropython.version >= (1, 20, 0):
10+
# np[0] = (255, 255, 255) # set the first pixel to white
11+
# np.write() # write data to all pixels
12+
# r, g, b = np[0] # get first pixel colour

0 commit comments

Comments
 (0)