技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

把 EEG 装进树莓派:PiEEG 的 C 库实现与工程代价

项目背景与痛点

脑电采集长期是一个“昂贵且封闭”的领域。商业设备如 g.tec、BioSemi 动辄数万美元,固件闭源、驱动私有,算法研究者拿到的往往只是一个封装好的 .dll。OpenBCI 用 ADS1299 把硬件成本压到千元美元级,但仍需要一块独立采集板 + USB Dongle + 上位机,链路里多了一整套协议栈。

pieeg-club/EEGwithRaspberryPI 的切入点很直接:把 ADS1299 前端做成一块直接叠在树莓派 40-pin 排线上的扩展板,让采集主机与计算主机合并为同一台设备。省掉 USB 传输、省掉独立采集盒、省掉一层驱动协议,成本被压到几百元人民币的量级。而这个 C 库的定位,是绕开 Python 生态的中间层,直接在用户态操作 SPI 与 GPIO——目的不是“好用”,而是“时序可控”。需要先说明的是,仓库已被作者标注 Not supported,这本身就是理解它技术选择的关键背景。

核心技术揭秘

硬件栈:核心是 TI 的 ADS1299,8 通道、24 位 ΔΣ ADC,片内集成可编程增益放大器(1~24 倍)、偏置驱动(Bias Drive)、导联脱落检测与内部方波测试信号。它通过 SPI 输出数据,并用一根 DRDY 引脚在每次转换完成时拉低,通知主机取数。

软件栈:库依赖 bcm2835,直接 mmap 访问 BCM2835 的寄存器来操作 SPI0 和 GPIO——这是刻意的取舍。走内核 spidev 意味着每次传输都有一次 ioctl 上下文切换,再叠加 CFS 调度抖动,实时性会被拉低一个量级。代价同样明显:必须 root 运行、与内核 SPI 驱动争抢外设、并且在树莓派 5 上彻底失效(外设桥换成了 RP1,寄存器模型不再兼容)。这大概率就是项目停更的技术原因之一。

数据流可以概括为下面这条链路:

graph TD A[头皮电极 8 通道] --> B[ADS1299 模拟前端] B --> C[PGA 放大与 24 位 ΔΣ 采样] C --> D[DRDY 下降沿] D --> E[bcm2835 直接操作 SPI 寄存器] E --> F[读取 3 字节状态加 8 乘 3 字节通道数据] F --> G[24 位补码符号扩展] G --> H[码值换算为微伏电压] H --> I{"下游用途"} I -->|"离线复盘"| J[写入 CSV 文件] I -->|"实时闭环"| K[环形缓冲队列] K --> L[FFT 与分类推理]

最值得注意的实现难点不在 SPI,而在“没有 FIFO”。ADS1299 片上不带采样缓冲,主机必须在下一次 DRDY 到来之前把上一次的数据读走,否则旧数据直接被覆盖。因此采样循环必须足够紧:不能在循环里做文件 I/O,不能在循环里做内存分配,更不能让 Python 的 GC 插手。真正的采样时刻由 ADS1299 内部振荡器决定,所以采样率本身是稳的,风险集中在“主机来不及取数”导致的丢帧。这也是为什么作者坚持用 C——不是为了性能数字好看,而是为了把这段临界区做得足够短。

另一个容易被忽视的细节是初始化序列:RESET → SDATAC → 配置 CONFIG1/2/3 → 逐通道写 CHnSET(增益与输入选择)→ 配置 BIAS_SENSP/BIAS_SENSN 选定偏置通道 → RDATAC 进入连续读取模式。这套寄存器序列是这个仓库真正有价值的部分,它把芯片手册里散落的时序要求固化成了可复现的代码。

功能亮点与差异

对比 BrainFlow 这类成熟框架,PiEEG 的 C 库几乎是“裸金属”路线:没有跨语言绑定、没有内置滤波与降采样、没有统一的板卡抽象,但换来了对原始寄存器与时序的完全掌控,以及在树莓派上尽可能低的采集延迟。

对比 OpenBCI Cyton,差异在于链路长度。Cyton 需要 USB Dongle 和一套串口协议,PiEEG 则是一根排线直连,物理层就是 SPI。对于“采集完立刻在同一块板子上跑推理”的边缘 BCI 场景,这种一体化是实打实的优势——Pi 4 上跑 FFT、CSP 甚至轻量 CNN 都够用,不需要第二台机器。

应用场景与落地建议

它适合实验室原型、教学演示、BCI 算法验证、神经反馈与注意力/睡眠的探索性研究。不适合任何需要临床诊断资质或 CE/FDA 认证的场景:板上没有医疗级隔离,供电来自树莓派的开关电源,安全性无从谈起。

生产化落地有几条硬建议:

  • 实时性:安装 PREEMPT_RT 内核,采集线程设 SCHED_FIFO 并绑定 CPU 核,关闭调频与省电策略;数据落盘用双缓冲 + 独立写线程,绝不阻塞采样循环。
  • 电源与噪声:树莓派的开关电源噪声会直接耦合进模拟前端。用电池供电或低噪声 LDO,并在软件侧准备 50 Hz 陷波。
  • 版本锁定:树莓派 5 与 bcm2835 不兼容,请锁定 Pi 3B+/4B 与较旧的 Raspberry Pi OS,不要盲目升级。
  • 数据格式:CSV 便于调试但体积膨胀快。长时间记录建议改用二进制帧 + 元数据头(采样率、增益、参考电极、时间戳),否则后期对齐事件标记会很痛苦。

综合评价

优点:成本极低;采集与计算同机,边缘闭环天然成立;C 实现对时序的掌控力强;寄存器初始化序列具有很高的参考价值;全栈开源,便于二次开发。

缺点:Not supported 意味着零维护,任何系统升级都可能直接让它挂掉;强依赖 root 与已过时的 bcm2835;无片上 FIFO,实时性上限受 Linux 调度制约;API 文档薄弱,几乎没有测试覆盖;缺少 Python 绑定,上层生态需要自己补。

一句话结论:它是一个优秀的参考实现,而不是一个可以放心依赖的生产组件。合理的用法是把它的 SPI 读取逻辑与寄存器配置序列当作蓝本,迁移到你自己维护的分支,或者直接为 BrainFlow 补一个 PiEEG 板卡后端,把生态与实时性两头都占住。

关于

GitHub 原始描述(英文)

Not supported. Measure 8 EEG channels with Shield PiEEG and RaspberryPi in C library

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

C
C++
Makefile
Python

README

PiEEG-8

Turn a Raspberry Pi into an 8-channel brain-computer interface.

Open-source Raspberry Pi shield for measuring EEG, EMG and ECG with the TI ADS1299.

📖 Documentation · 🚀 How to Start · 🛒 Buy · 💬 Discord

PiEEG-8 shield on a Raspberry Pi
Warning 警告

PiEEG-8 is not a medical device and has not been certified by any regulator for use with the human body. It is a tool for research, education and engineering. You use it at your own risk.

Caution 注意

Battery only. The Raspberry Pi, the shield and everything cabled to them (monitor, keyboard) must run from a 5 V power bank while electrodes are on a person. Never use a wall charger or a mains-powered monitor or computer. The board has no isolation. Read the Safety page and the liability notice.

At a glance

Channels 8 biosignal inputs, plus reference and bias
Converter TI ADS1299, 24-bit, simultaneous sampling
Sample rate 250 SPS (default)
Signals EEG, EMG, ECG
Host Raspberry Pi 4 or Raspberry Pi 5
Link to the Pi SPI0 on the 40-pin header, DRDY on GPIO 26
Electrodes Dry or wet Ag/AgCl, 2.54 mm header
Power 5 V power bank, 3 A, up to 10,000 mAh
Software PiEEG Server, Python SDK (this repo), BrainFlow

How it works

 Electrodes 1–8 ─┐
 Reference (ear) ┼─► PiEEG-8 ─SPI─► Raspberry Pi ─► PiEEG Server
 Bias (ear) ─────┘   (ADS1299)      4 or 5          ├ dashboard :1617
                                      ▲             ├ WebSocket :1616
             5 V power bank ──────────┘             └ CSV · LSL · OSC
PiEEG-8 setup: cap, shield on the Pi, power bank, monitor, keyboard and mouse

Quick start

  1. Mount the shield on the Raspberry Pi’s 40-pin header (Pi switched off).
  2. Connect 8 electrodes to channels 1–8, a reference ear clip and a bias ear clip.
  3. Power the Pi from a 5 V power bank. The LED on the shield lights up.
  4. Install and run PiEEG Server:
curl -sSL https://raw.githubusercontent.com/pieeg-club/PiEEG-server/main/install.sh | bash
sudo reboot                          # first time only: enables SPI
pieeg-server --device pieeg8
  1. Open http://raspberrypi.local:1617, click Connect, close your eyes and watch the alpha rhythm rise.
Note 注意

The installer’s background service starts in 16-channel mode. To make it start as PiEEG-8, see How to Start.

Full walkthrough: docs.pieeg.com → PiEEG-8 → How to Start

Wiring

Input Electrode Purpose
Channels 1–8 EEG electrodes in a cap (10-20 system) Signal
Reference Ear clip on one earlobe Common 0 µV point for all channels
Bias Ear clip on the other earlobe Cancels common-mode noise (mains hum)
Wiring of channels 1–8, reference and bias

Raspberry Pi pinout

Header pin GPIO Signal
19 GPIO 10 SPI0 MOSI
21 GPIO 9 SPI0 MISO
23 GPIO 11 SPI0 SCLK
24 GPIO 8 SPI0 CE0
37 GPIO 26 DRDY (data ready)
PiEEG-8 SPI and DRDY pins on the 40-pin header

Software

Best for
PiEEG Server (recommended) Browser dashboard, recording, WebSocket, LSL, VRChat OSC, webhooks
Python SDK (this repo, 1.GUI) Learning the ADS1299, raw data, custom register settings
BrainFlow BoardIds.PIEEG_BOARD (ID 56); build BrainFlow on the Pi with --build-periphery

Python SDK scripts

Script Pi What it does
1.8_ch_Pi5.py Pi 5 Live 8-channel graph, band-pass filtered (system python3-libgpiod)
1.8_ch_Pi5_gpiod_1_5_4.py Pi 5 Same, for pip install gpiod==1.5.4
1.8_ch_Pi4.py Pi 4 Live 8-channel graph, band-pass filtered
Raw_data_1Ch_Pi5.py Pi 5 Raw view of one channel
save_data_short_Pi5.py Pi 5 Save raw µV to Excel
save_data_gpiod_1_5_4.py Pi 5 Save raw µV to Excel, gpiod==1.5.4
save_data_short_Pi4.py Pi 4 Save raw µV to Excel

Install (recent Raspberry Pi OS needs a virtual environment):

python3 -m venv --system-site-packages ~/pieeg-env
source ~/pieeg-env/bin/activate
pip install spidev matplotlib scipy pandas openpyxl

# Raspberry Pi 5 — pick one:
sudo apt install -y python3-libgpiod      # for 1.8_ch_Pi5.py
pip install gpiod==1.5.4                  # for 1.8_ch_Pi5_gpiod_1_5_4.py

# Raspberry Pi 4:
pip install RPi.GPIO

Run:

cd 1.GUI
python 1.8_ch_Pi5.py          # or 1.8_ch_Pi4.py
Live 8-channel graph

Details, troubleshooting and the data format: Software docs · Specification

Example signals

EEG: alpha rhythm (eyes closed)

Alpha rhythm with eyes closed

EEG: chewing and blinking artifacts

Chewing and blinking artifacts

EMG and ECG

EMG ECG

What’s in this repository

Folder Content
1.GUI Python SDK: live graphs and saving data for Raspberry Pi 4 and 5
1.GUI/dataset Raw EEG with chewing and blinking (4-3-2-1 times), plus plotting scripts
Datasets Raw EEG with dry Ag/AgCl electrodes, 250 SPS
2.Software Legacy PiEEG desktop app and data-conversion scripts
ML_application Machine-learning example: record → alpha power → classifier
images Images used in the original README
Manual. Conenction and start - PiEEG1.pdf Printable quick-start manual
Wet EEG cap - How to use it.pdf Instructions for the wet-electrode cap

Documentation

Page
PiEEG-8 Intro Overview and specs at a glance
What’s in the Box What you need besides the shield
How to Start Step-by-step setup and first signal checks
Electrodes & Pinout Wiring, montage, GPIO pins
Software PiEEG Server, Python SDK, BrainFlow
Signals & Datasets Example recordings, datasets, ML example
Specification Data frame, µV conversion, ADS1299 registers
Troubleshooting Fixes for SPI, GPIO, software and noise
Safety Battery-only operation and responsible use

Citation

If you use PiEEG-8 in research, please cite:

I. Rakhmatulin et al. PIEEG: Turn a Raspberry Pi into a Brain-Computer-Interface
to measure biosignals. arXiv:2201.02228, 2022.

I. Rakhmatulin et al. Raspberry Pi Shield – for measure EEG (PIEEG). 2021 5th International
Conference on Electrical, Electronics, Communication, Computer Technologies and Optimization
Techniques (ICEECCOT), pp. 410–413. DOI: 10.1109/ICEECCOT52851.2021.9707969

License

Software and SDK: MIT. Hardware use is subject to the terms in license.txt and the PiEEG liability notice.

Contact

评论

暂无评论