🗺️ Schémata vývodů (Pinouty)
Referenční mapy pinů (GPIO, ADC, I2C, SPI, UART, PWM, napájení) pro laboratorní vývojové desky.
Raspberry Pi Pico Pinout
Oficiální přehled všech 40 pinů mikrokontroléru RP2040: napájecí větve (3V3, VSYS, VBUS, GND), 26 multifunkčních GPIO, 3 analogové kanály ADC0–ADC2 a sběrnice I2C/SPI/UART.
Zobrazit SVG schéma ↗YD-RP2040 Type-C Pinout
Barevné schéma vývodů alternativní laboratorní vývojové desky YD-RP2040 vybavené moderním USB-C konektorem, uživatelským tlačítkem (GP24) a vestavěnou adresovatelnou RGB LED WS2812B (GP23). Plně pinově kompatibilní s Raspberry Pi Pico.
Zobrazit PNG schéma ↗🐍 Komentované MicroPython skripty
Komentované zdrojové kódy pro Thonny IDE. Kód můžete zkopírovat přímo jedním kliknutím
nebo stáhnout jako soubor .py.
inbuiltLedToggle.py
Základní blikání vestavěné LED diody (GPIO 25)
# Toggles the onboard LED on Raspberry Pi Pico / RP2040 (GP25).
from machine import Pin
from time import sleep
# Initialize the onboard LED pin as digital output
led = Pin(25, Pin.OUT)
# Infinite loop toggling the LED state every 0.5 seconds (1 Hz blink rate)
while True:
led.toggle() # Invert current pin output state
sleep(0.5) # Delay for 500 ms
inbuiltLedSlowIntensityChange.py
Plynulá regulace jasu LED pomocí pulzně-šířkové modulace (PWM)
# Gradually changes the brightness of the onboard LED using PWM (Pulse Width Modulation) on GP25.
import time
from machine import Pin, PWM
# Construct PWM object attached to onboard LED pin (GPIO 25)
pwm = PWM(Pin(25))
# Set the PWM frequency (50 Hz provides smooth dimming without visible flicker)
pwm.freq(50)
# Variables to control smooth brightness fading
duty = 0 # Initial duty cycle value (0 = completely off, 65535 = full brightness)
step = 500 # Increment step per loop iteration
direction = 1 # 1 for brightening (fade in), -1 for dimming (fade out)
while True:
# Increment or decrement the duty cycle
duty += direction * step
# Check upper boundary (16-bit max is 65535) and reverse direction
if duty > 65535:
duty = 65535
direction = -1
# Check lower boundary (minimum 0) and reverse direction
elif duty < 0:
duty = 0
direction = 1
# Write the 16-bit duty cycle value to PWM hardware
pwm.duty_u16(duty)
# Delay between duty cycle updates
time.sleep(0.05)
# Output relative duty cycle percentage (0.0 to 1.0) to console
print("Duty cycle: ", duty / 65535)
ADCbasics.py
Základní vzorkování analogového vstupu na GP28 (ADC2) na frekvenci 50 Hz
# Basic Analog-to-Digital Converter (ADC) reading example for Raspberry Pi Pico.
# Reads analog voltage from ADC pin GP28 (ADC2) at a 50 Hz sampling rate.
from machine import ADC, Pin
import time
# Initialize ADC channel on GPIO 28 (Pico pin 34 / ADC2)
adc = ADC(Pin(28))
# Main loop continuously sampling the analog input
while True:
# Read raw 16-bit unsigned integer value (0 to 65535, where 65535 corresponds to ~3.3V)
adc_raw = adc.read_u16()
# Print the raw ADC value to serial console (compatible with UBI Serial Plotter)
print(adc_raw)
# 20 ms delay corresponds to a 50 Hz sampling frequency
time.sleep(0.02)
usrButton.py
Čtení uživatelského tlačítka s ošetřením zámitů kontaktů (Software Debounce)
# Reads the onboard user push button with software debounce on Raspberry Pi Pico / YD-RP2040 (GP24).
from machine import Pin
import time
# Button pin configuration (active LOW with internal pull-up resistor on GP24)
BUTTON_PIN = 24
# Contact debounce stabilization time in milliseconds
DEBOUNCE_TIME = 20 # ms
# Initialize GPIO pin as digital input with internal pull-up resistor
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_UP)
# Variables for tracking button state and debounce timing
last_steady_state = button.value() # Confirmed steady button state
last_flickerable_state = button.value() # Instantaneous state from previous iteration
last_debounce_time = time.ticks_ms() # Timestamp of the last input transition
print("Button monitoring started. Press the USR button on board...")
while True:
current_state = button.value()
current_time = time.ticks_ms()
# Detect any signal transition (potential contact bounce or valid press)
if current_state != last_flickerable_state:
last_debounce_time = current_time
last_flickerable_state = current_state
# Check if the signal has remained stable longer than DEBOUNCE_TIME
if time.ticks_diff(current_time, last_debounce_time) > DEBOUNCE_TIME:
# Detect confirmed edge transition
if current_state != last_steady_state:
if last_steady_state == 1 and current_state == 0:
print("-> Button PRESSED (Falling edge, Active LOW)")
elif last_steady_state == 0 and current_state == 1:
print("<- Button RELEASED (Rising edge)")
last_steady_state = current_state
neopixelRGBled.py
Řízení adresovatelné RGB LED WS2812B (NeoPixel) na GP23
# Controls an addressable WS2812B (NeoPixel) RGB LED on Raspberry Pi Pico / YD-RP2040 (GP23).
import machine
import neopixel
import time
# Pin configuration and pixel count
PIXEL_PIN = 23
NUM_PIXELS = 1
# Initialize GPIO pin for digital output and instantiate NeoPixel driver
pin = machine.Pin(PIXEL_PIN, machine.Pin.OUT)
np = neopixel.NeoPixel(pin, NUM_PIXELS)
# Define color sequence in (Red, Green, Blue) format with values 0 to 255
# Using low brightness values to avoid eye strain
colorsToShow = [
(20, 0, 0), # Red (e.g., alarm / tachyarrhythmia indication)
( 0, 20, 0), # Green (e.g., normal status / detected QRS complex)
( 0, 0, 20), # Blue (e.g., active Bluetooth / recording)
(20, 20, 0), # Yellow (e.g., warning / lead-off contact instability)
(20, 0, 20), # Purple
( 0, 20, 20), # Cyan
( 0, 0, 0) # Off
]
print("Starting WS2812B NeoPixel RGB LED color demonstration...")
while True:
for color in colorsToShow:
# Set color for the first pixel (index 0)
np[0] = color
# Transmit color data packet to LED over single-wire protocol
np.write()
print(f"Active color (R, G, B): {color}")
time.sleep(1) # 1-second delay between colors
I2Cscanner.py
Diagnostický skener sběrnice I2C (detekce připojených senzorů a adres)
# Scans the I2C bus to discover connected sensor devices and their 7-bit bus addresses.
from machine import Pin, SoftI2C
# I2C bus pin configuration (SoftI2C allows using any GPIO pins on RP2040)
I2C_SDA_PIN = 16 # GP16 (SDA data line)
I2C_SCL_PIN = 17 # GP17 (SCL clock line)
# Initialize SoftI2C bus at standard 100 kHz clock frequency
i2c = SoftI2C(sda=Pin(I2C_SDA_PIN), scl=Pin(I2C_SCL_PIN), freq=100000)
print("=" * 55)
print(" UBI - I2C SENSOR BUS SCANNER")
print(f" Pins: SDA = GP{I2C_SDA_PIN}, SCL = GP{I2C_SCL_PIN}")
print("=" * 55)
# Scan all 7-bit I2C addresses in the valid range 0x08 to 0x77 (8 to 119)
devices = i2c.scan()
# Known laboratory components used in UBI coursework
KNOWN_DEVICES = {
0x3C: "OLED Display 0.91'' (SSD1306, 128x32)",
0x3D: "OLED Display (SSD1306 alternative address)",
0x23: "BH1750 Ambient Light Sensor / Luxmeter",
0x10: "VEML7700 Precision Light Sensor",
0x57: "MAX30102 / MAX30100 PPG Pulse Oximeter",
0x38: "AHT20 Temperature and Humidity Sensor",
0x76: "BMP280 Barometric Pressure Sensor (SDO -> GND)",
0x77: "BMP280 Barometric Pressure Sensor (SDO -> 3V3)",
0x68: "BMI160 6-Axis IMU (Accelerometer + Gyro)",
0x0D: "QMC5883L 3-Axis Magnetometer",
0x29: "VL53L0X Laser ToF Distance Sensor",
0x5A: "MLX90614 Non-Contact Infrared Thermometer"
}
if len(devices) == 0:
print("\n[!] No I2C devices found.")
print(" Troubleshooting:")
print(" 1. Verify power supply connections (3.3V and GND).")
print(" 2. Verify SDA and SCL wiring (do not cross lines).")
print(" 3. Check for external pull-up resistors on the bus.")
else:
print(f"\n[OK] I2C devices found: {len(devices)}")
for device in devices:
hex_addr = hex(device)
hint = KNOWN_DEVICES.get(device, "Unknown device / custom module")
print(f" -> Address: {hex_addr} (dec: {device:3d}) | {hint}")
print("=" * 55)
measureECG.py
Periodické vzorkování analogového EKG signálu a detekce svodů (AD8232)
# Periodically samples an analog ECG signal from AD8232 front-end and monitors lead-off status.
from machine import Pin, ADC, Timer
import time
# 1. Analog ECG input and sampling configuration
# Analog output of AD8232 connected to ADC channel 2 (GP28)
ECG = ADC(28)
# Sampling period in milliseconds (20 ms corresponds to 50 Hz sampling frequency)
fs = 20 # ms
# Lead-off detection pins (AD8232 sets HIGH when an electrode is disconnected)
LOminus = Pin(17, Pin.IN, Pin.PULL_DOWN)
LOplus = Pin(18, Pin.IN, Pin.PULL_DOWN)
# Lead attachment status (True = electrodes properly connected)
leadsStateOK = True
# Conversion factor from raw 16-bit integer (0-65535) to voltage (0-3.3V)
conversionRate = 3.3 / 65535
# 2. Hardware timer callback function for periodic sampling
def sampleECG(timer):
"""
Called periodically by hardware timer with exact period fs (20 ms).
Reads instantaneous analog voltage and outputs formatted data string
compatible with UBI Serial Plotter: /*voltage,leadsState*/
"""
value = ECG.read_u16()
voltage = value * conversionRate
print(f"/*{voltage:.4f},{1 if leadsStateOK else 0}*/")
# 3. Initialize and start periodic timer
ECGtimer = Timer(mode=Timer.PERIODIC, period=fs, callback=sampleECG)
print("ECG sampling started (fs = 50 Hz)...")
# 4. Main loop monitoring electrode lead-off status
while True:
if LOminus.value() == 1 or LOplus.value() == 1:
if leadsStateOK:
leadsStateOK = False
print("# WARNING: Electrodes disconnected or poor skin contact!")
elif LOminus.value() == 0 and LOplus.value() == 0:
if not leadsStateOK:
leadsStateOK = True
print("# Electrodes properly connected.")
time.sleep(0.05)
MAX30102_I2C.py
Měření fotopletysmografie (PPG) a pulzní oxymetrie (SpO2) přes I2C (MAX30102)
# Measures photoplethysmography (PPG) and pulse oximetry (SpO2) using optical sensor MAX30102 via I2C at a 50 Hz sampling rate.
from machine import SoftI2C, Pin
from max30102 import MAX30102
# I2C Pin Configuration for Raspberry Pi Pico / RP2040
my_SDA_pin = 16 # GP16 (I2C SDA data line)
my_SCL_pin = 17 # GP17 (I2C SCL clock line)
my_i2c_freq = 400000 # I2C Fast-mode frequency (400 kHz)
# Initialize software I2C interface on specified pins
i2c = SoftI2C(sda=Pin(my_SDA_pin), scl=Pin(my_SCL_pin), freq=my_i2c_freq)
sensor = MAX30102(i2c=i2c)
# Scan I2C bus to verify sensor connection
if sensor.i2c_address not in i2c.scan():
print("Sensor MAX30102 not found on I2C bus. Check wiring and power.")
elif not (sensor.check_part_id()):
print("I2C device ID does not match MAX30102.")
else:
print("MAX30102 sensor connected and recognized (I2C address: 0x57).")
# Configure sensor with default settings
sensor.setup_sensor()
# Set hardware FIFO averaging
SAMPLE_AVG = 8 # Options: 1, 2, 4, 8, 16, 32
sensor.set_fifo_average(SAMPLE_AVG)
# Set the ADC range
ADC_RANGE = 16384 # Options: 2048, 4096, 8192, 16384
sensor.set_adc_range(ADC_RANGE)
# Set the sample rate
SAMPLE_RATE = 50 # Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
sensor.set_sample_rate(SAMPLE_RATE)
# Set the LED mode
LED_MODE = 2 # Options: 1 (red), 2 (red + IR), 3 (red + IR + g - MAX30105 only)
sensor.set_led_mode(LED_MODE)
print("Starting PPG acquisition. Place finger gently on sensor...")
while True:
# Continuously poll sensor FIFO queue and store incoming samples into buffer
sensor.check()
# Check if storage buffer contains new PPG samples
if sensor.available():
# Retrieve raw 18-bit optical absorption readings
red_sample = sensor.pop_red_from_storage()
ir_sample = sensor.pop_ir_from_storage()
# temperature = sensor.read_temperature()
# Output raw readings in comma-separated format for UBI Serial Plotter: RED,IR
print(red_sample, ",", ir_sample, sep="")