Pico Spectrometer
Firmware for a Raspberry Pi Pico that reads a Hamamatsu C12880MA micro-spectrometer and draws its spectrum live on a small TFT screen, with a knob for exposure. Built in 2023 as a self-contained instrument, so a spectrum could be seen without a computer attached.
This page describes firmware. There is nothing to run in the browser. It covers what the device does and how it was built, so the work is on the record with the rest.
What it does
The C12880MA is a fingertip-sized grating spectrometer with a 288-element detector. The Pico clocks data out of it, converts each of the 288 channels into a screen column, and redraws the whole display in a loop. Point the slit at a light source and its spectrum appears immediately.
Each bar is drawn in the color of its own wavelength, so the plot looks like a spectrum. That conversion runs once at startup. Every channel index becomes a wavelength, the wavelength becomes a CIE XYZ triple, and that becomes the 16-bit color the display takes.
The pieces
| Microcontroller | Raspberry Pi Pico, built against the pico-sdk with pico_stdlib,
hardware_adc and hardware_spi. |
|---|---|
| Detector | Hamamatsu C12880MA, 288 channels. Clock, start and video lines on GPIO 2, 3 and 26. |
| Display | ILI9341 TFT, 240×320, over SPI0 on GPIO 4 to 10. One full frame is pushed per reading. |
| Exposure control | A potentiometer on ADC channel 1, GPIO 27. |
| Wavelength range | Roughly 307 to 878 nm across the 288 channels, about 2 nm per channel. |
Calibration
Hamamatsu ships each C12880MA with its own calibration, a fifth-order polynomial mapping pixel index to
wavelength. The coefficients in the firmware are for the specific unit the lab had, serial
20L00801, and are hard-coded as SERIAL_20L00801. A different sensor needs its
own numbers; using these with another unit would shift every wavelength.
The color of a bar comes from the same calibration that places it, so the hues line up with where the lines fall.
The exposure knob
Integration time is set by hand because the useful range is very wide. A bright lamp and a dim reflectance reading are orders of magnitude apart. Turning the knob linearly across that range would waste most of its travel, so the reading is raised to the fourth power before it becomes a time. Most of the knob covers the short exposures, where small changes matter, and the long exposures sit near the top.
The knob is also sampled fifty times and smoothed into a running value rather than read once, which keeps a noisy ADC reading from making the plot flicker between frames.
Known limitations
These are marked in the source and were never resolved:
- The sensor timing is bit-banged. Clock pulses are driven in software with microsecond sleeps, and the integration time carries a hand-measured correction for the 48 clock pulses that follow the start pulse. Two comments in the source note that this should be moved to the Pico’s PIO blocks, and that the correction stands in for a real measurement.
- Nothing is printed to the screen but the plot. There is no axis, peak readout or scale, so the display shows the shape of a spectrum without showing where anything sits. A note in the source marks the peak value as the first thing to add.
- Readings are not recorded. The device displays a spectrum but cannot store or export one. Anything worth keeping had to be photographed off the screen.
Source
The firmware is eleven files of C. There is a sensor driver, a display driver, the color conversion and a
main loop. It builds with the standard pico-sdk toolchain and the README covers the
dependencies. It is not published in this repository and lives in the
pico-spectrometer project on its own.
The color conversion is based on John Walker’s specrend.c, as noted in
color.h. The main loop began from a Raspberry Pi example and keeps its BSD-3-Clause header.
The C12880MA datasheet and its per-unit calibration sheet come from Hamamatsu.