A high-precision, low-cost controller for Earth's Field Nuclear Magnetic Resonance (EFNMR) spectroscopy, built on the Raspberry Pi Pico (RP2040).
This project demonstrates a full-stack engineering approach, combining bare-metal firmware, custom hardware interfaces (PIO/DMA), and a modern desktop application for scientific data analysis.
This system allows for sub-microsecond timing precision and gapless data acquisition, overcoming the traditional limitations of standard microcontrollers.
The RP2040's unique PIO subsystem is used to implement a custom assembly state machine (pio_pulses.pio).
- Cycle-Accurate Timing: Generates RF pulses (B1 field) with 125MHz clock precision, completely independent of CPU jitter or interrupts.
- Complex Sequences: Capable of executing multi-pulse sequences like CPMG (Carr-Purcell-Meiboom-Gill) for T2 relaxation measurement, handling microsecond-level spacing between thousands of echoes.
- Gapless Streaming: Uses DMA channels to stream ADC data directly into a ring buffer in RAM.
- Zero CPU Overhead: The CPU is free to process logic or communicate with the PC while data is being captured at full samplerate (up to 500 kS/s).
- Bare-Metal Performance: Written in C++ using the Pico SDK (Arduino Core) for maximum efficiency.
- State Machine: Manages the spectrometer states (Idle, Tuning, Pulse, Acquire) and handles high-speed Serial communication with the host.
- Modern Interface: Built with
CustomTkinterfor a professional, dark-themed user experience. - Multithreading: Handles Serial I/O in background threads to ensure the UI remains responsive during long experiments.
- Real-Time Visualization: Uses
Matplotlibto render high-frequency ADC data and analysis results instantly.
The software provides real-time signal processing to extract physical properties from the raw NMR data.
For spin-echo trains, the software performs automated peak detection and curve fitting.
- Algorithm: Extracts the peak amplitude of each echo in the train.
-
Fitting: Fits a mono-exponential decay
$$V(t) = A \cdot e^{-t/T_2}$$ to determine the Transverse Relaxation Time ($$T_2$$ ), a key indicator of molecular environment (e.g., distinguishing oil vs water).
To determine the exact Larmor frequency of the sample (typically ~2 kHz in Earth's field):
- Preprocessing: Applies DC offset removal, basic denoising, and Hanning windowing to the FID signal.
- Spectrum: Computes the FFT to display the frequency domain, with automatic peak detection to label the Larmor frequency.
Raw signal from the probe showing the decaying Larmor precession.

Fast Fourier Transform (FFT) of the FID, showing the Larmor frequency.

A train of spin echoes generated by the PIO engine, used for T2 measurement.

Automated exponential fit of the echo amplitudes.

Fast Fourier Transform (FFT) of the CPMG echo train, showing the NMR peaks.

Copyright (c) 2026 Sajid Ahmed. All Rights Reserved.
This repository is strictly for portfolio review and recruitment purposes.
- No Unauthorised Distribution: You may not redistribute, sublicense, or sell any part of this source code.
- No Commercial Use: This software may not be used for any commercial or production purposes.
- AI & Machine Learning Restriction: Use of this source code, in whole or in part, for the purpose of training, fine-tuning, or validating machine learning or artificial intelligence models (including but not limited to LLMs and generative AI) is strictly prohibited without express written permission from the author.
- Viewing & Forking: Permission is granted to view the source code and fork the repository on GitHub for the purpose of personal review only.
The software is provided " as is ", without warranty of any kind.
See the LICENSE file for the full legal text.