Homemade Optical Spectrometer

Designing a 3D-printed optical spectrometer using a diffraction grating and open-source software.

I recently worked on an interesting project where I needed to figure out how a certain material absorbs specific wavelengths of light. After a bit of research, I discovered an instrument called an optical spectrometer that is designed to measure the properties of light by separating it into its component colours. Seeking some guidance, I contacted Anglia Instruments, a UK-based supplier of spectrometers, who guided me through the process of selecting the appropriate equipment.

They lent me an Avantes UV/VIS/NIR Fiber Optic Spectrometer (sensitive to UV, visible, and near-infrared light) and a “Deuterium-Halogen Light source” to complete the absorbance part of the testing.

I found the instrument’s ability to reveal aspects of the world that are normally concealed from us fascinating, so I took a closer look at the instrument and realised that its key components were surprisingly simple. This realisation sparked the idea to have a go at creating my own.

Inside an Avantes optical spectrometer

What is optical spectroscopy and absorbance spectrum?

Consider that “light” is made up of different colours, each associated with a specific wavelength. When you see white light, like sunlight, it actually contains a combination of all these colours layered on top of one other.

A spectrometer uses something like a prism to spread out this light into a pattern we can interpret, much like a rainbow, ranging from violet with shorter wavelengths to red with longer wavelengths. A spectrometer can then look at this spread-out spectrum and measure the intensity of each wavelength.

In my tests, I was specifically interested in how a substance absorbs light rather than the light it emits/reflects. This is where the light source is required.

Deuterium/halogen light source

The deuterium-halogen light source offers a “reference” spectrum spanning a broad wavelength range, incorporating a deuterium lamp in the ultraviolet (UV) region and halogen light in the visible and near-infrared regions. By placing our sample material between the light source and the spectrometer, we can identify the absorbed wavelengths that appear as dark lines (absorption lines) absent from the reference spectrum, indicating the colours/wavelengths that were absorbed by the material.

Specific atoms and molecules emit and absorb specific wavelengths of light, just like each person’s fingerprint is distinct. It can be used for identification, the spectrum of light emitted or absorbed by a specific material or light source is also unique to that particular source and carries characteristic information about the source’s composition and properties.

Using a spectrometer to find a spectral “fingerprint” serves as a powerful tool for engineers and scientists to identify compounds/elements commonly found in the fields of chemistry, astronomy, and forensics.

Splitting Light - Diffraction or Refraction?

At the core of our spectrometer, we need a way to split the incoming light into constituent wavelengths (a monochromator).

There are a couple of options here; we could use something like a prism, which relies on the principle of refraction, as different wavelengths are bent by slightly different amounts as they pass from the air to a higher refractive index material (the glass prism). However, for this project, I opted for a much cheaper and more compact component, a Diffraction Grating which is is a surface with many closely spaced parallel slits.

My diffraction grating shown here has 1000 lines per mm (a pitch of just 1 micron). 

When light passes through these slits, each slit acts like a new tiny source of light; these resulting waves interfere with each other as they spread out. If you select a point on the opposite side of the grating and measure the distance from there to nearby adjacent slits, and then compare these distances to a multiple of the wavelength of incoming light, and they match, the light waves will overlap in such a way that the peaks and troughs line up (“in phase”) in this area. Constructive interference occurs as the waves reinforce each other. The amplitude waves add up, and the light appears brighter in these areas.

On the other hand, when the path lengths differ by half of a wavelength, the waves are “out of phase”, and destructive interference occurs. This complex interaction results in bright and dark spots emerging from the interference pattern at specific angles (maxima through constructive interference) and dark spots (minima through destructive interference). These bright spots are called orders, and the diffraction grating produces multiple orders at increasing angles, which get progressively dimmer.

The angle at which these orders appear depends on wavelength; this is fundamental to our spectrometer, as shorter wavelengths get diffracted at smaller angles.

I found this optical simulation video from Huygens Optics a useful insight into the interference nature of the diffraction grating.

You can see this effect as I pass the diffraction grating in front of a white light source. Bright first-order rainbows can be seen on either side with the shorter wavelength “blue light” diffracted by the smallest angle and “red” diffracted the most. When the grating is moved to the far right of the frame, the dimmer second-order spectrum is also briefly visible.

How do we measure the spectrum?

In the video, we can see that the spectrum is easily visible without any special tools, and the colors seem to spread out in a parallel direction to the slit of the grating. To measure the intensity of light across the spectrum, we just need a device that can measure the light intensities along a virtual line that runs through the diffracted spectrum in the “x-direction.”

Some spectrometers use high-resolution one-dimensional camera sensors that have a single row of many pixels for this task. However, we can achieve the same effect by using a USB HD webcam that also has many linear rows of pixels. We can easily mask off a 1D section of the image in software. As a result, this inexpensive webcam with 1024 pixels across the sensor, each measuring 0-255 intensity, can capture the spectrum intensities pretty well.

Detector angle calculation

For our spectrometer, it makes sense to place the camera sensor in the position of the brightest maxima where the spectra can be seen. These bright spots are labelled with an integer “n”, representing the order of the maximum at “n=1” being the brightest.
The angle at which this order appears depends on the wavelength of the incident light and the spacing between the slits on the grating (d). The angular position of the n-th order maximum can be calculated as below: The grating equation: sin(θ) = mλ/d, where θ is the angle of diffraction, λ is the wavelength of light, and d is the spacing between the rulings.

As we are interested in viewing the brightest full first-order spectrum of visible light, I calculated the diffraction angles we can expect for the shortest and longest wavelengths of the light in the visible range, a red 750 nm and purple 380 nm.
Then, I calculated the mid-point between these angles, which gives an angle from the diffraction grating on which to place the detector (a simple webcam) as a theoretical optimum location.

Benchtop test of detector locations

Next, I built a simple benchtop test, placing the light source, diffraction grating and webcam on my desk. After some experimenting it appeared that distance between the light source and diffraction grating serves to improve the formation of a clean image of the spectrum.

This (I think) is because a light source that is moved further away causes the rays to approach more parallel to one another and the “divergence” of a point light source is reduced. Reducing this dispersion appears to contribute to a uniform and clear spectral image. In many spectrometers, there is an optical component called a collimating lens that serves this purpose. 

However, with increased separation between the source and sensor, the brightness of the resulting spectrum reduces. So, after moving the sensor and grating around a few times and experimenting on my desk, I found a combination of positions that were a good compromise of clear spectral separation and overall brightness.

So, I took the photo you can see here of the final positions and loaded this into CAD.

Enclosure design

In CAD, I scaled the photo of the setup by using the ruler in the background. I then designed a simple enclosure around the diffraction grating and camera, and then added a few features that are explained below:

– I included a wall inside the enclosure on which to mount a pair of razor blades that form an adjustable slit. This helps isolate rays that are travelling close to parallel.

– A sample window to the enclosure where I can place a material to be analysed for its absorbance spectra.

– The walls of the casework interlock at the base, creating a light seal that prevents external light from hitting the sensor.

– There is an optical fibre transmission connection in the casework. This allows me to use nice cheap optical cables, like the ones usually found in the “optical audio” connection of DVD players, to efficiently collect and transmit light from a source to the spectrometer. This feature is especially useful when dealing with small or distant light sources.

I took the CAD and 3D printed the casework with my FDM printer, then carefully assembled the components onto the mounts and secured everything with plastic thread-forming screws. The next step was to figure out the software I needed to analyse images from the camera feed.

Analysis software

When I started the project, I was planning on writing some Python code to interpret the webcam video stream and plot a spectrum; however, I soon realised that this wasn’t the first DIY spectrometer, and a number of kind people have written some open source software to do just this.

The best open-source application I found was from the Theremino group “Theremino Spectrometer V.30″:

https://www.theremino.com/

The Software takes in the camera input and allows you to mask off areas of the feed to process into a spectrum.

Calibration

We know that shorter wavelengths (blue light) are diffracted by the smallest angle and longer wavelengths  (red) are diffracted the most, this appears on our image from left to right. The camera clearly shows a rainbow of the first order diffraction; however, there is no correspondence between the “X” pixel positions of each colour and the actual wavelengths displayed in the graph in the software. We need to calibrate the plot to establish a correspondence between the pixel positions and recorded wavelengths.

To do this, at least two “peaks” must appear on the image that corresponds to specific wavelengths of light we know. We can do this using a Fluorescent Lamp – yes, a simple household bulb that you can purchase at any shop.

We can use this as a reference because, unlike the Sun, which emits a nearly continuous spectrum (a complete rainbow), fluorescent bulbs emit some very specific colours of the rainbow and not others.

This occurs because fluorescent lamps commonly contain mercury vapor, which emits very sharp characteristic lines in the visible spectrum, like a fingerprint which can match up to our spectrometer plot for calibration. If you look at the image here, you can see a reference spectrum from Wikipedia for a fluorescent lamp, and if you compare this to the image from our spectrometer plot, in this animation you can see the characteristic fingerprint appear.

I chose two peaks at 436nm (blue) and 546nm (green) sufficiently spaced apart to scale the spectrum plot in the software, and there we have it: a fully calibrated spectrometer.

Testing - LED Characterization

To test the basic performance of the spectrometer, I attempted to characterise some light sources around the home; here, you can see the difference between a red LED and a red laser diode. The plot shows that centre wavelengths are very similar, but the shape of spectra differs greatly. The red LED emits light over a much broader range of wavelengths, producing a range of red hues. On the other hand, the laser is much more monochromatic, emitting light at a very narrow wavelength band, resulting in a more concentrated peak.

Laser Diode - Measured Spectrum
Red LED - Measured Spectrum

Next steps

There may be limitations regarding precision and sensitivity with this device compared to professional instruments, but considering the cost difference, I think ours provides pretty usable data.

I have several projects in mind that plan to build on this prototype. I’m hoping to use the absorbance window for an idea such as basic monitoring of water quality, where we can analyse the chemical content of samples using their absorption spectra.

Thanks for reading, if you have any questions or comments, let me know.