Cytron Maker Pi RP2040 Demo¶
The Cytron Maker Pi RP2040 board comes with the following demo script installed. This file can be found on this site in cy-mpi-rp2040-demo.
The manufacturer publishes this code on github: https://github.com/CytronTechnologies/MAKER-PI-RP2040/tree/main/Examples/CircuitPython/Out-of-the-box%20Demo%20Code
1# *************************************************
2# Out-of-the-box Demo for Cytron Maker Pi RP2040
3#
4# This demo code is written in CircuitPython and it serves
5# as an easy quality check when you first receive the board.
6#
7# It plays a melody upon power up (slide power switch to ON)
8# and shows running lights (blue LEDs) at the same time.
9# Then the two RGB LEDs will animate the colors, while the
10# program checking push buttons' state, repeatedly.
11#
12# Press GP20 button to play a short melody, lights up all
13# blue LEDs, move servo motors to 0 degree and run DC motors
14# at 50% and -50% speeds.
15# Press GP21 button to play another melody, turn off all blue
16# LEDs, move servo motors to 180 degree & brake DC motors.
17#
18# Maker Pi RP2040 also has four DC motors quick test buttons
19# built-in. You may press the onboard M1A, M1B, M2A or M2B
20# push buttons to run your motors without writing any code.
21#
22# More info:
23# http://www.cytron.io/p-maker-pi-rp2040
24# https://circuitpython.org/board/raspberry_pi_pico
25#
26# Email: support@cytron.io
27# *************************************************
28import board
29import digitalio
30import neopixel
31import simpleio
32import time
33import pwmio
34from adafruit_motor import servo, motor
35
36# Initialize LEDs
37# LEDs placement on Maker Pi RP2040
38LED_PINS = [board.GP0,
39 board.GP1,
40 board.GP2,
41 board.GP3,
42 board.GP4,
43 board.GP5,
44 board.GP6,
45 board.GP7,
46 board.GP16,
47 board.GP17,
48 board.GP26,
49 board.GP27,
50 board.GP28]
51
52LEDS = []
53for pin in LED_PINS:
54 # Set pins as digital output
55 digout = digitalio.DigitalInOut(pin)
56 digout.direction = digitalio.Direction.OUTPUT
57 LEDS.append(digout)
58
59# Initialize Neopixel RGB LEDs
60pixels = neopixel.NeoPixel(board.GP18, 2)
61pixels.fill(0)
62
63# Melody
64MELODY_NOTE = [659, 659, 0, 659, 0, 523, 659, 0, 784]
65MELODY_DURATION = [0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.15, 0.2]
66
67# Define pin connected to piezo buzzer
68PIEZO_PIN = board.GP22
69
70# Initialize buttons
71btn1 = digitalio.DigitalInOut(board.GP20)
72btn2 = digitalio.DigitalInOut(board.GP21)
73btn1.direction = digitalio.Direction.INPUT
74btn2.direction = digitalio.Direction.INPUT
75btn1.pull = digitalio.Pull.UP
76btn2.pull = digitalio.Pull.UP
77
78# Initialize servos
79# 50% duty cycle: 2**15 = 32768 = 1/2 of 65536 (16-bit)
80servo_motors = [] # create an array and add servo objects.
81servo_motors.append(servo.Servo(pwmio.PWMOut(board.GP12, duty_cycle=2**15, frequency=50)))
82servo_motors.append(servo.Servo(pwmio.PWMOut(board.GP13, duty_cycle=2**15, frequency=50)))
83servo_motors.append(servo.Servo(pwmio.PWMOut(board.GP14, duty_cycle=2**15, frequency=50)))
84servo_motors.append(servo.Servo(pwmio.PWMOut(board.GP15, duty_cycle=2**15, frequency=50)))
85
86# Initialize DC motors
87m1a = pwmio.PWMOut(board.GP8, frequency=50)
88m1b = pwmio.PWMOut(board.GP9, frequency=50)
89motor1 = motor.DCMotor(m1a, m1b)
90m2a = pwmio.PWMOut(board.GP10, frequency=50)
91m2b = pwmio.PWMOut(board.GP11, frequency=50)
92motor2 = motor.DCMotor(m2a, m2b)
93
94# -------------------------------------------------
95# ON START: Show running light and play melody
96# -------------------------------------------------
97for i in range(len(LEDS)):
98 LEDS[i].value = True
99
100 if i < len(MELODY_NOTE):
101 # Play melody tones
102 simpleio.tone(PIEZO_PIN, MELODY_NOTE[i], duration=MELODY_DURATION[i])
103 else:
104 # Light up the remainding LEDs
105 time.sleep(0.15)
106
107# Turn off LEDs one-by-one very quickly
108for i in range(len(LEDS)):
109 LEDS[i].value = False
110 time.sleep(0.02)
111
112
113color = 0
114state = 0
115
116# -------------------------------------------------
117# FOREVER LOOP: Check buttons & animate RGB LEDs
118# -------------------------------------------------
119while True:
120
121 # Check button 1 (GP20)
122 if not btn1.value: # button 1 pressed
123 # Light up all LEDs
124 for i in range(len(LEDS)):
125 LEDS[i].value = True
126
127 # Move servos to 0 degree
128 for i in range(len(servo_motors)):
129 servo_motors[i].angle = 0
130
131 # Move motors at 50% speed
132 motor1.throttle = 0.5 # motor1.throttle = 1 or -1 for full speed
133 motor2.throttle = -0.5
134
135 # Play tones
136 simpleio.tone(PIEZO_PIN, 262, duration=0.1)
137 simpleio.tone(PIEZO_PIN, 659, duration=0.15)
138 simpleio.tone(PIEZO_PIN, 784, duration=0.2)
139
140 # Check button 2 (GP21)
141 elif not btn2.value: # button 2 pressed
142 # Turn off all LEDs
143 for i in range(len(LEDS)):
144 LEDS[i].value = False
145
146 # Move servos to 180 degree
147 for i in range(len(servo_motors)):
148 servo_motors[i].angle = 180
149
150 # Brake motors
151 motor1.throttle = 0 # motor1.throttle = None to spin freely
152 motor2.throttle = 0
153
154 # Play tones
155 simpleio.tone(PIEZO_PIN, 784, duration=0.1)
156 simpleio.tone(PIEZO_PIN, 659, duration=0.15)
157 simpleio.tone(PIEZO_PIN, 262, duration=0.2)
158
159
160 # Animate RGB LEDs
161 if state == 0:
162 if color < 0x101010:
163 color += 0x010101 # increase rgb colors to 0x10 each
164 else:
165 state += 1
166 elif state == 1:
167 if (color & 0x00FF00) > 0:
168 color -= 0x000100 # decrease green to zero
169 else:
170 state += 1
171 elif state == 2:
172 if (color & 0xFF0000) > 0:
173 color -= 0x010000 # decrease red to zero
174 else:
175 state += 1
176 elif state == 3:
177 if (color & 0x00FF00) < 0x1000:
178 color += 0x000100 # increase green to 0x10
179 else:
180 state += 1
181 elif state == 4:
182 if (color & 0x0000FF) > 0:
183 color -= 1 # decrease blue to zero
184 else:
185 state += 1
186 elif state == 5:
187 if (color & 0xFF0000) < 0x100000:
188 color += 0x010000 # increase red to 0x10
189 else:
190 state += 1
191 elif state == 6:
192 if (color & 0x00FF00) > 0:
193 color -= 0x000100 # decrease green to zero
194 else:
195 state += 1
196 elif state == 7:
197 if (color & 0x00FFFF) < 0x001010:
198 color += 0x000101 # increase gb to 0x10
199 else:
200 state = 1
201 pixels.fill(color) # fill the color on both RGB LEDs
202
203
204 # Sleep to debounce buttons & change the speed of RGB color swipe
205 time.sleep(0.05)