StampP4X
Support the following products:
UiFlow2 Example
ADC Input
Open the stampp4x_adc_example.m5f2 project in UiFlow2.
This example reads the 16-bit ADC value from GPIO23 every 200 ms and prints the result to the serial console.
GPIO Output
Open the stampp4x_gpio_example.m5f2 project in UiFlow2.
This example toggles GPIO15 high and low once per second and prints the current output state to the serial console.
PWM Output
Open the stampp4x_pwm_example.m5f2 project in UiFlow2.
This example outputs a 20 kHz PWM signal on GPIO15. The duty value repeatedly ramps between 0 and 1023 and is printed to the serial console.
MicroPython Example
ADC Input
This example reads the 16-bit ADC value from GPIO23 every 200 ms and prints the result to the serial console.
1import os, sys, io
2import M5
3from M5 import *
4from hardware import ADC
5from hardware import Pin
6import time
7
8
9adc23 = None
10
11
12last_time = None
13
14
15def setup():
16 global adc23, last_time
17
18 M5.begin()
19 adc23 = ADC(Pin(23), atten=ADC.ATTN_11DB)
20 last_time = time.ticks_ms()
21
22
23def loop():
24 global adc23, last_time
25 M5.update()
26 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 200:
27 last_time = time.ticks_ms()
28 print((str("ADC(pin23): ") + str((adc23.read_u16()))))
29
30
31if __name__ == "__main__":
32 try:
33 setup()
34 while True:
35 loop()
36 except (Exception, KeyboardInterrupt) as e:
37 try:
38 from utility import print_error_msg
39
40 print_error_msg(e)
41 except ImportError:
42 print("please update to latest firmware")
GPIO Output
This example toggles GPIO15 high and low once per second and prints the current output state to the serial console.
1import os, sys, io
2import M5
3from M5 import *
4from hardware import Pin
5import time
6
7
8pin15 = None
9
10
11last_time = None
12enable = None
13
14
15def setup():
16 global pin15, last_time, enable
17
18 M5.begin()
19 pin15 = Pin(15, mode=Pin.OUT)
20 last_time = time.ticks_ms()
21
22
23def loop():
24 global pin15, last_time, enable
25 M5.update()
26 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 1000:
27 last_time = time.ticks_ms()
28 enable = not enable
29 if enable:
30 print("set pin15 high")
31 pin15.on()
32 else:
33 print("set pin15 low")
34 pin15.off()
35
36
37if __name__ == "__main__":
38 try:
39 setup()
40 while True:
41 loop()
42 except (Exception, KeyboardInterrupt) as e:
43 try:
44 from utility import print_error_msg
45
46 print_error_msg(e)
47 except ImportError:
48 print("please update to latest firmware")
PWM Output
This example outputs a 20 kHz PWM signal on GPIO15. The duty value repeatedly ramps between 0 and 1023 and is printed to the serial console.
1import os, sys, io
2import M5
3from M5 import *
4from hardware import PWM
5from hardware import Pin
6import time
7
8
9pwm15 = None
10
11
12duty = None
13last_time = None
14dir2 = None
15
16
17def setup():
18 global pwm15, duty, last_time, dir2
19
20 M5.begin()
21 pwm15 = PWM(Pin(15), freq=20000, duty=512)
22 duty = 0
23 dir2 = True
24 last_time = time.ticks_ms()
25
26
27def loop():
28 global pwm15, duty, last_time, dir2
29 M5.update()
30 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 10:
31 last_time = time.ticks_ms()
32 duty = (duty if isinstance(duty, (int, float)) else 0) + 1
33 if duty >= 1023:
34 duty = 0
35 dir2 = not dir2
36 if dir2:
37 pwm15.duty(duty)
38 print((str("duty: ") + str(duty)))
39 else:
40 pwm15.duty((1023 - duty))
41 print((str("duty: ") + str((1023 - duty))))
42
43
44if __name__ == "__main__":
45 try:
46 setup()
47 while True:
48 loop()
49 except (Exception, KeyboardInterrupt) as e:
50 try:
51 from utility import print_error_msg
52
53 print_error_msg(e)
54 except ImportError:
55 print("please update to latest firmware")
