>moria.de >Spectrometry: A low cost DIY spectrometer

A low cost DIY spectrometer

There are many cheap DIY spectrometers on the web, but little is written on why they are built the way they are and what to watch out for. Cheap means using a simple film transmission grating and lenses instead of mirrors, with overall material costs of about 20 USD or less (in 2025). That type only uses one half of the first diffraction order and wastes much light. The resolution is typically in the range of a few nm. That's the price to pay for low material costs. I will show 1 nm is possible.

Introduction

The theory of operation is quickly explained: Light enters through a slit and is collimated using a lens. Then a grating causes diffraction, which changes the angle of the slit image depending on the wavelength. Finally another lens focuses the image of the spread out slit at infinity again to project it onto a sensor.

Since you image a slit, the resolution is determined by how many slits fits on the sensor next to each other. That is called the R-number. A very popular slit size is 100 µm. If you image it 1:1 and the sensor is 10 mm wide, it can take 10000 µm / 100 µm = 100 slits. If you image the range of 400–700 nm, 100 slits give a resolution of 3 nm. It does not matter how many pixels the sensor has, as long as they suffice to sample the slit image well.

A shorter focal length at the focusing end yields a smaller image, e.g. for a smaller sensor with smaller pixels. The focal length is determined by the field of view needed to capture the angle of diffracted light.

The focal length of the collimating lens determines the scaling of the image of the slit. A smaller length enlarges the image and needs a more narrow slit, but also a smaller grating. A larger length shrinks the image of the slit and can accept a more wide slit, but also needs more grating area.

Should the light entering the slit already be collimated, e.g. not using a fiber, but directly from the Sun or a laser, it will not be spread out and only a very small portion of the grating will be used and the quality of the spectrogram may suffer a lot.

Configuration

Light is often input via a fiber and a common NA for fiber ends is 0.22, which needs to be converted to a focal ratio for the required lens. $n$ is 1 for air. What's the half angle?

$$\text"NA" = n · \text"sin"(a / 2)$$ $$\text"NA" / n = \text"sin"(a / 2)$$ $$\text"arcsin"(\text"NA" / n) = a / 2$$

The half angle of NA 0.22 is 12.71°. Now on to the focal ratio for that:

$$a / 2 = \text"arctan"(D/{2·f})$$ $$\text"tan"(a / 2) = D/{2·f}$$ $$2 · \text"tan"(a / 2) = D/f$$

The focal ratio of 12.71° is f/2.22, so that is required for the collimating lens, if no light should be wasted. While such lenses are available, they do cost some money. I tried an affordable 50 mm f/1.7 camera lens, but the resulting image was not pleasantly sharp after alignment when viewed with a finder scope focused at infinity and the adjustment was tough, because both the flange distance and the focus needs to be adjusted at once and the focal ratio leaves very little tolerance. Finally I accepted the need for a slower focal ratio, losing much light, and used the chance to increase the focal width at the same time for more resolution. A 100 mm f/4 (25 mm diameter) achromat is a cheap solution, adjustment is easy and it delivers a beautifully sharp image. The difference to f/2.22 is almost two f-stops or only a quarter of the light.

For amateurs, good optics (in particular used optics) are much cheaper and easier to obtain than a good grating. That means the optical performance should be as good as possible and the spectrometer should deliver an imaging PSF much better than the slit width, because the grating will be the weakest point.

Using a grating of 1000 lp/mm results in a focusing lens of 50 mm for a 22.1 mm APS-C size sensor or a quarter of the length for a 5 mm sensor. That flexibility allows to use many cameras.

The Spectrometer designer is a wonderful web application that does all the math. My inputs are:

350–750 nm
Resolution 1 nm
Deviation from Littrow 0° (using the grating at the diffraction angle)
Groove density 1000 lp/mm (good availability)
Magnification 0.5
Detector width 22.1 mm (APS-C)
Numerical aperture 0.124 (collimation f/4)

That results in a configuration using standard parts:

Slit width 110.5 µm
Collimator focal length 106.2 mm
Focusing focal length 53.1 mm
Grating angle of incidence 16°
Diffraction angle 4.3–28.4°, center angle 16°
Grating size 27.6 mm

That is a common slit width. I used an Auto Rikenon 50 mm f/1.7 M42 lens for focusing with a Canon EOS 350D, both used from Ebay. Note that the diffraction angles are relative to the grating, so the grating is at 16° to the optical axis and the focusing lens is at 16° to the grating.

Spectrometer

The use of a DSLR means two things: An UV/IR block filter and a bayer matrix, both not ideal. The filter will restrict the wavelength range some and the bayer matrix means an even more weird intensity curve and higher spatial quantization noise. Debayering is done with a 2x2 pixel window, horizontally with a step width of 1 (for minimum spatial quantization noise) and vertically with a step width of 2 (saves storage). The result is the sum of the pixels in that window.

The EOS 350D has pixels with a pitch of 6.41 µm. The bayer pattern means less than two pixels quantization noise. The resolution of 1 nm in pixels is:

$${ 100 \text"µm slit" · 0.5 \text"magnification" } / { 6.41 \text"µm/pixel" } = 7.8 \text"pixel"$$

The expected error of peaks should be less than 2/7.8 = 0.26 nm.

A 3D printed case is not as rigid as the massive metal casts of expensive gear. It also has worse temperature dependencies. The Canon EF bajonet is a joke when it comes to tolerances; agricultural machinery is more precise. It is probably a good idea to take calibration data before each measurement.

Case

It may be desirable to use different slit widths and different light connectors. The slit must be centered to the connector and should be located right before the fiber end. The solution is a small adapter containing slit and optical connector in one unit and that is what the pros use as well. That part is screwed to the case at a defined focus position.

The focusing lens is mounted to the camera and a swappable adapter mounts to its filter thread to allow swapping cameras.

The case could have been built to use the other first diffraction order, but doing it this way ends up with a spectrogram where shorter wavelengths are left, which is a common convention.

The case is 3D-printed and holds the slit adapter, the collimation focuser, the grating and the camera lens adapter. It is printed with a 0.4 mm nozzle and 0.2 mm layer height. It does not need support, but the long 45° overhang for the focuser must be printed with enabled part fan.

Case

The slit and camera adapter are fixed with M3 screws. There are M3 core holes in the case to cut threads into.

SMA905 light input and slit

It is convenient to input light using a fiber. Optical SMA 905 connectors are very popular, but also very expensive for hobbyists. The subtypes SMA 905 A/B use 4 mm for the ferrule, like the metal part of electrical SMA connectors. Most common is SMA 905 C using a ferrule of 3.175 mm, also called F-SMA I. Devices have female connectors and cables have male ones.

You can save money by buying a female electrical SMA connector and pushing out its inside. The result is a connector with the right thread and flange, but it is typically 9.4 mm long, which lets the 12.0 mm long ferrule look out by 2.6 mm. It is also too wide at the end. A precisely 3D-printed shim with a centering ring solves both problems. FDM with a 0.25 mm nozzle and 0.1 mm layer height worked fine for me. A 0.4 mm nozzle with an extrusion width of 0.3 mm may work. The SMA connector has 2.6 mm diameter holes and is made to be mounted with 2-56 UNC-2B screws. That is not recommended for M2.5 and M2.2 is rather uncommon, but M2 works as well. I designed a M2 core hole and simply cut a thread into the print. That is the stack of components:

Assembly

I cut 5x5 mm pieces from razor blades, set two small drops of nail polish in the adapter and adjusted the blade edges using a stereo microscope with a caliper set at 0.1 mm as visual reference. The adapter also has a 0.1 mm wide line printed inside as visual aid. Once the polish dried, I glued the shim on top and mounted the SMA connector with the M2 screws.

It is tough to adjust the edges perfectly upright in addition to being parallel. The screw holes of the adapter allow for some rotational adjustment. They are made for M3 cylinder head screws.

Input light collimation

The lens is a 99.6 mm focal length achromat with a diameter of 25.3 mm, available from Astromedia and various other shops. I blackened its edge with black synthetic resin varnish. It does not matter if it is shiny or matte. Resin has a similar refraction index to glass and the edge looks much darker than with black sharpie.

Many builds use a shift focuser, but we are talking about adjustment steps of less than 0.1 mm. Perfect collimation is required, because the slit image determines the resolution of the spectrometer.

The focuser uses a thread with 1.0 mm pitch. That is a compromise between ease of printing and adjustment. Like for the slit components, I used a 0.25 mm nozzle with 0.1 mm layer height. A 0.4 mm nozzle with 0.3 mm extrusion width may work as well. Slow printing and a well dialed in flow adjustment helps for threads. Once the lens in its threaded ring fits nicely into the focuser, the focuser is glued to the case wall. Watch out to mount it with the slits in the ring facing the front for easy adjustment. Should the thread be too tight, you only need to print a new focuser with a little bit more tolerance, which is a tiny part, but you can keep the case. The uncommon square outline allows printing without support, but you do need a part fan.

Focuser

Grating

High quality glass gratings are quite expensive. The alternative are cheap plastic films with good availability.

The more grating area is used, the more unevenness in the film grating reduces the resolution. Card board dia slides are a popular way to mount the film grating, but the grating slide I bought was wrinkled and bent. As it turned out, the film just loves to bend and the card board is not the main contributor to that. Small lenses and sensors use less area of the grating and suffer less from bending, but restrict the choice of cameras. Framing raw film in a dia slide with glass reduced the bending and resulted in a higher quality. Not perfect, but probably as good as possible given the material.

Adjustment

The adjustment contains multiple sequential steps.

Preliminary collimation: Mount the slit adapter to the case, but tighten the screws only so much that you can still adjust it rotationally. Do not yet mount the camera adapter and do not mount the grating. Use a small telescope that is focused at infinity (using celestial objects, far terrestrial objects are not enough) and adjust the collimation lens until the slit is in focus. It may be helpful to place a small light in front of the slit.

Slit adjustment: Swap the telescope for a camera focused at or near infinity and adjust the slit until it is perpendicular in the image. In case the edges are not perfectly parallel, aim for symmetry to both sides.

Final collimation: Tighten the screws and re-adjust the collimation lens using the telescope. If needed, secure the thread with a tiny bit of glue.

Camera focusing: Determine the exact infinity focus of the camera. It may not be exactly at the infinity setting. The image should show the expected slit width in pixels. Use a RAW image for verification.

Grating orientation: I have two different gratings from two vendors, and both are tilted. Many spectrograms on the net from devices using cheap film gratings are tilted. Perhaps a tilt error is common. There are two ways to check the grating orientation: Inside the spectrometer, white light shining through the slit and measuring the camera image angle of the wide dispersed slit, or outside the spectrometer with a laser pointer. Rotate the grating axially to see the effect. The dia holder has significant oversize to allow some rotation. Optically, it is sufficient if the slit is perfectly parallel to the grating, but an angle makes software way more complicated and will either lose resolution or SNR. Just keep everything square.

Data conversion

Images must be shot in RAW format. Libraw allows conversion of many RAW formats, but unsurprisingly none of the supplied tools match the needs of debayering for a spectrometer. The small program rawto2dspec performs the described debayering and generates a FITS file with what is commonly called an uncalibrated 2D spectrum, transfering as much meta data as possible into the FITS header and adding a histogram of the raw data as well to allow checking for pixel saturation.

Once data is in FITS format, it is suitable for pretty much all spectrometric software.

This is a part of the debayered and postprocessed visual 2D spectogram of a Neon/Xenon calibration lamp:

Neon/Xenon postprocessed spectrogram

That is a contrast enhanced part of the FITS file:

Neon/Xenon contrast enhanced FITS spectrogram

Spectral point spread function

When fitting a curve for peak detection, it should match the spectral PSF of the instrument. Some spectrometer designs may show abberations that create an asymmetric PSF. So, how does it look like? This is a scan through the FITS file of the major Neon peak near the center without background subtraction.

Spectral PSF

It is almost symmetrical and the FWHM is where it is to be expected. The flat head says the optical PSF is better than needed, just as desired. A Gauss fit should be usable for peak detection. A peak near the end has a slightly larger FWHM.

BOM

Lower and upper case shell, lens bracket, lens focuser, slit bracket, SMA905 shim, camera mount: 3D printed from OpenSCAD part-*.scad files
2 pieces 5x5 mm razor blade: Cut with strong scissors from a blade.
Achromat f=99.6 mm, diameter 25.3 mm: PGI Shop or AstroMedia
Grating 1000 lp/mm: AstroMedia grating to frame yourself
50x50 mm slide frame with glass: Card board is possible, but offers less resolution.
SMA connector, female: Reichelt but much cheaper from Ebay. Press the inside out with a pin.
APS-C camera with 50 mm lens and 52 mm filter thread: Or modify the 3D printed camera mount for other cameras
4x M3x10 mm cylinder head for slit
4x M2x6 mm cylinder head or slit head for SMA connector
4x M3x10 mm countersunk head for camera mount
2x M3x10 mm countersunk head for case
rawto2dspec as gzipped tar.