Skip to content

A high-precision, low-cost controller for Earth's Field Nuclear Magnetic Resonance (EFNMR) spectroscopy, built on the Raspberry Pi Pico (RP2040/RP2350).

License

Notifications You must be signed in to change notification settings

sahmed0/spectrometer-control-firmware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NMR Spectrometer Controller (C++ & Python)

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.


🏛️ Technical Architecture

This system allows for sub-microsecond timing precision and gapless data acquisition, overcoming the traditional limitations of standard microcontrollers.

1. PIO (Programmable I/O) Pulse Generation

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.

2. DMA (Direct Memory Access) Acquisition

  • 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).

3. Firmware (C++)

  • 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.

4. Desktop GUI (Python)

  • Modern Interface: Built with CustomTkinter for 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 Matplotlib to render high-frequency ADC data and analysis results instantly.

📊 Data Analysis Features

The software provides real-time signal processing to extract physical properties from the raw NMR data.

T2 Relaxation Analysis (CPMG)

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).

Fast Fourier Transform (FFT)

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.

📸 System Gallery

1. Free Induction Decay (FID)

Raw signal from the probe showing the decaying Larmor precession. Free Induction Decay

2. Larmor Frequency

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

3. CPMG Pulse Sequence

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

3. T2 Relaxation Fit

Automated exponential fit of the echo amplitudes. T2 Analysis

4. Fourier Transform of the CPMG Echo Train

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


Licence & Usage

Copyright (c) 2026 Sajid Ahmed. All Rights Reserved.

This repository is strictly for portfolio review and recruitment purposes.

Use Constraints:

  • 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.

About

A high-precision, low-cost controller for Earth's Field Nuclear Magnetic Resonance (EFNMR) spectroscopy, built on the Raspberry Pi Pico (RP2040/RP2350).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published