Raspberry Pi Zero GPIO 擴展板
3.3V VCC
GND GND
GPIO2 CH1
GPIO3 CH2
GPIO4 CH3
GPIO14 CLK
GPIO15 DAT
以下是示波器的程式碼:
python
import RPi.GPIO as GPIO
import spidev
import time
import numpy as np
import matplotlib.pyplot as plt
# Set up GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
GPIO.setup(3, GPIO.IN)
GPIO.setup(4, GPIO.IN)
# Set up SPI
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 500000
# Set up variables
samples = 1000
interval = 0.001
times = np.arange(0, samples * interval, interval)
# Read data from SPI
def read_spi(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
# Read data from channels
def read_channel(channel):
values = []
for i in range(samples):
value = read_spi(channel)
values.append(value)
time.sleep(interval)
return values
# Plot data from channels
def plot_channels():
ch1_values = read_channel(0)
ch2_values = read_channel(1)
ch3_values = read_channel(2)