← Projects
3d-printing beginner 16 days ago ◑ 24 views ◯ 5 min read

Build a DIY Spectrometer: CD Diffraction Grating, 3D-Printed Enclosure, and Spectral Analysis Software

Build time: 1 weekend
Tools needed: 3D printer, a utility knife for cutting the diffraction grating piece, black spray paint or matte black filament for light baffling, a webcam or Raspberry Pi camera module, a laptop or Raspberry Pi for the analysis software, superglue or hot glue for fina
spectrometerspectroscopydiffraction grating3d printingopticsraspberry piwebcamlight analysis
Parts List

A spectrometer splits light into its component wavelengths and measures the intensity at each one — the same basic principle used to identify elements in starlight, check LED color temperature, verify a laser's wavelength, or tell a CFL bulb's spectral fingerprint apart from an LED's. The core optical trick is decades old and genuinely simple: a diffraction grating bends different wavelengths of light by different amounts, spreading white light out into a rainbow the same way a prism does but with much finer, more measurable precision — and a working grating is sitting in your recycling bin right now in the form of any blank recordable CD or DVD.

This build pairs a CD-grating optical path with a 3D-printed light-tight enclosure and a basic webcam to capture the spread spectrum, then uses free spectral analysis software to turn the captured image into an actual intensity-vs-wavelength graph. It's a genuinely approachable weekend project, and the resulting instrument is sensitive enough to tell apart different light sources and, once calibrated against known reference lines, to make real wavelength measurements.

How a CD Works as a Diffraction Grating

A CD's data surface is etched with a spiral track of microscopic pits spaced at a very regular, very fine interval — about 1.6 micrometers between tracks on a standard CD, closer on a DVD. That regular spacing is, functionally, a diffraction grating: light hitting it at a shallow angle reflects back at an angle that depends on wavelength, spreading white light into a visible rainbow. It's not laboratory-grade (a purpose-built grating has a much higher line density and far better efficiency), but it's more than good enough to resolve distinct emission lines from fluorescent and LED sources, and it costs nothing.

Optical Layout

The design is a simple straight-line optical path: a narrow slit lets in a controlled sliver of light, that light travels down a light-tight tube to strike the CD grating at a shallow angle, and the diffracted spectrum falls onto the webcam sensor at the far end.

  1. Entrance slit: cut a narrow (0.2-0.5mm) slit in a piece of black card or 3D print one directly into the housing — the narrower the slit, the sharper the resulting spectral lines, at the cost of less light getting through.
  2. Light-tight tube: print this in matte black filament, and paint the interior with additional matte black paint if the print still shows any sheen — any stray reflected light inside the tube shows up as noise or ghost lines in the captured spectrum.
  3. Grating mount: cut a flat piece from the data side of a CD (avoid the label side) and mount it at roughly 30-45 degrees to the incoming light path — this angle is adjustable in practice, so build in a way to fine-tune it (a simple pivoting mount) during initial testing.
  4. Camera position: position the webcam to capture the diffracted spectrum fan where it falls, with its own lens focus set manually to the distance of the grating — autofocus will hunt and refocus unpredictably on a dim, mostly-black scene.

3D Printing the Housing

Design choiceReasoning
Matte black filament throughout, not just paintPrinted layer lines on the interior walls scatter light less predictably than a smooth painted surface; matte black filament plus a light paint pass on top gives the best result
Modular, slip-fit sections rather than one printed tubeLets you adjust slit width, grating angle, and camera distance during calibration before committing to a glued final assembly
Light baffles (internal ridges) inside the tubeKnock down any light that reflects off the tube walls before it reaches the grating or camera, the same principle used inside telescope tubes
A hood or hinged cap over the slitLets you control exactly how much ambient light enters, and seals out stray room light during a measurement

Capturing and Calibrating a Spectrum

Point the slit at a light source, adjust the grating angle and camera position until a clean, sharp spectral band appears in the webcam's live view, and capture a still image. From there, free tools built specifically for amateur spectroscopy — Theremino Spectrometer and RSpec are the two most commonly used by the hobbyist and citizen-science spectroscopy community — take that image and convert the pixel positions across the spectral band into an intensity graph.

Calibration is the step that turns "a pretty rainbow photo" into "a real measurement": you need at least two known reference wavelengths to map pixel position to actual nanometers. A compact fluorescent bulb is the standard first calibration source, since it has several well-documented, sharp mercury emission lines (notably around 405nm, 436nm, 546nm, and 611nm) that show up clearly even on a modest webcam sensor. Mark those known lines in the analysis software against their pixel positions, and the software interpolates a wavelength scale across the rest of the spectrum.

What You Can Actually Measure

Limitations and Safety

A CD grating has real limits: lower spectral resolution than a purpose-built grating, and some light loss to higher diffraction orders that a proper grating minimizes. Never point this instrument's slit directly at the sun or at any high-power laser without a diffusing step in between — the whole optical path is designed to concentrate light, which is exactly what you don't want aimed into your eye or your camera sensor at full intensity.

Once the basic unit works, the most useful upgrade is usually a better camera — swapping the webcam for a Raspberry Pi camera module with manual gain and exposure control gives much more consistent, repeatable captures than a typical consumer webcam's auto-exposure, which is constantly fighting you to "correctly expose" what is, to its auto-exposure algorithm, just a mostly-dark image with one bright thin line in it.