Friday, 4 August 2017

DIY Spectrometer

Have you ever wanted to see what the stars are made of, what is in a light source or even what is inside of your drinking water? Well, there is a tool for that in Star Trek it is called a tricorder but in the real world, we use a tool called a spectrometer. A spectrometer is a device that can analyse what is inside of an object based on the light that passes through that object.

How a Spectrometer Works

There are many different components that make up a spectrometer:
The sample/light source: This is what you are analysing

The slit: This allows only a sliver of light to pass through to be analysed.

The grating: It is similar to a prism where it splits the light into the different colours that make up that light.

The recorder: All this does is collect and capture the data provided from when the light is split by the grating.

When you put it all together it should work like this.


The setup:

Shine the light towards the spectrometer.
Place sample in between light and spectrometer (Optional)

What happens:

The light will pass through the slit allowing a narrow beam of light to pass through.
It will hit the grating at a 45-degree angle and be split into a spectrum.
The webcam will record that data and send it to the computer.


How to build a spectrometer:

The first step is to get an enclosure that is not too big but has the length width and height to fit your web camera.Next, you are going to need to completely fill it with black paper or cover every surface with black ink.

Then you will cut out a square where you can place the slit. To make the slit what you need to do is put two razor blades together then split them using a piece of paper. tape them down to a piece of black cardboard then once you've made the slit cut the piece of paper where the separation is to allow light to pass through. Go to 1:22 in the video to see how.
https://www.youtube.com/watch?v=fl42pnUbCCA

After that get a piece of grating. To make grating what you need to do is take an old CD and cut around the outer and inner edges of the CD. Then place tape all over the CD then peel it off. With the purple coloured disk cut that into a tiny rectangle that will fit on your webcam. Go to 3:03 in the video to see how.
https://www.youtube.com/watch?v=fl42pnUbCCA

Next, tape the grating to the webcam. Then place the webcam at a 45-degree angle to the slit and make sure the lens is perfectly lined up and that is a working spectrometer.

The Software

I have written two codes that work together to capture and analyze the data collected by the spectrometer.

What this code does is it captures the image. The image will pop up on the screen. In that image, we will take the Y value and input that into our next code

import numpy as np
import matplotlib.pyplot as plt
import cv2

cap = cv2.VideoCapture(1)
ret, frame = cap.read()

fig = plt.figure()
myax = plt.imshow(frame)
plt.show()

What this code will do is using the Y value from the image it will plot that row. It will plot it so that it is the intensity compared to the pixel position of that row.

import numpy as np
import matplotlib.pyplot as plt
import cv2

cap = cv2.VideoCapture(1)
ret, frame = cap.read()

spec = np.empty((0))
for x in range(1280):
    [r,g,b]=frame[305,x]
    #print x
    intensity = (int(r)+int(g)+int(b))/3.0
    spec = np.append(spec, intensity)

# Subtract minimum of spectrum
spec = spec - min(spec)

plt.plot(spec)
plt.show()