Close
0%
0%

Raspberry Pi Spectrometer

The PySpectrometer is a Python (OpenCV and Tkinter) implementation of an optical spectrometer for the Raspberry Pi.

Similar projects worth following
Commercial Spectrometers often cost thousands if not tens of thousands of Dollars, putting them far beyond the reach of the amateur, and poorly funded Schools and Colleges.

The PySpectrometer is a Python (OpenCV and Tkinter) implementation of an optical spectrometer. This uses inexpensive off the shelf components and can be built at a cost that is in reach of everyone. (Released Under Apache Licence)

The hardware is simple and widely available and so should be easily to duplicate without critical alignment or difficult construction. The hard work was developing the software. The total cost (minus the Raspberry Pi) is under 100 UK Pounds.

Resolution/accuracy seems to be +/- a couple of nm or so, pretty reasonable for the price of the hardware.

Quick Intro video:

https://www.youtube.com/watch?v=1MgtNsAx8xE

The Project is hosted at GiHub and all source code can be found here: https://github.com/leswright1977/PySpectrometer

The PySpectrometer is a Python (OpenCV and Tkinter) implementation of an optical spectrometer. The motivation beind this project was to build a tool that could measure the wavelength of home-made Dye Lasers and perform some fluorescence spectroscopy. Most importantly at a cost that is in reach of everyone!

The hardware is simple and widely avilable and so should be easily to duplicate without critical alignment or difficult construction. The hard work was developing the software.

Resolution/accuracy seems to be +/- a couple of nm or so, pretty reasonable for the price of the hardware, especially when you consider the price of commercial components such as the Hamamatsu C12880MA breakout boards which run north of 300 bucks, and has a resolution of 15nm. Of course, this build is physically much larger, but not enormous!

Visit my Youtube Channel at: https://www.youtube.com/leslaboratory

A video of this project specifically is available here:

An explanation of the new software features is here:

The Miniature build video is here:

For those who want a truly miniature Spectrometer, the software runs on the Raspberry Pi Zero 2W!



Hardware

Screenshot

The hardware consists of:

A commercial Diffraction grating Spectroscope https://www.patonhawksley.com/product-page/benchtop-spectroscope

A Raspberry Pi Camera (with an M12 Thread) https://thepihut.com/products/raspberry-pi-camera-adjustable-focus-5mp

A CCTV Lens with Zoom (M12 Thread) (Search eBay for F1.6 zoom lens)

Everything is assembled on an aluminium base (note the Camera is not cooled, the heatsink was a conveniently sized piece of aluminium.)

Screenshot

Screenshot

For the MINIATURE version the hardware is:

Screenshot

A commercial Diffraction grating Pocket Spectroscope: https://www.patonhawksley.com/product-page/pocket-spectroscope

A Raspberry Pi Camera (with an M12 Thread): https://thepihut.com/products/raspberry-pi-camera-adjustable-focus-5mp

M12x0.5 F2.0 Fixed 12mm Focal length Lens: https://www.ebay.co.uk/itm/114551239930

***Assembly***

For either version:

Mount the lens onto the picamera. Mount the Picamera and Lens in such a way that it "looks down the barrel" of the spectroscope.

Refer to the above photographs for example mounts.

Point the spectromter at a light source and preview with a utility such as  raspistill.

Use the command: raspistill -t0 for a live view.

Adjust the lens so the spectrum is in focus and rotate the sectroscope so the spectrum is aligned horizontally. The blue end should be on the left and the red on the right.

***Installation***

Developed and tested on: 2021-01-11-raspios-buster-armhf-full.img for anything else your milage may vary!

Rasberry pi 4 and PiCamera Recommended.

(Note the software uses the Linux Video Driver, not the Picam Python module. As a consequence it will work with some webcams on probably any Linux box (Tested on Debian with a random webcam))

First attach the Picam, and enable it with raspi-config

Install the dependencies:

sudo apt-get install python3-opencv

sudo apt-get install python-dev libatlas-base-dev

pip3 install scipy

pip3 install peakutils

Run the program with: python3 pyspectrometer-v1.py

To calibrate, shine 2 Lasers of known wavelength (He-Ne, Argon or DPSS recommended! (Diode Lasers can have wavelengths that can be +/- several nm!)) at a piece of card in front of the spectrometer.

Click the two peaks on the graph, and in each of the boxes enter the corresponding wavelength. Then hit 'Calibrate'. In this example I have Calibrated with 532nm (DPSS) and 633nm He-Ne. The Scale and lablels will then adjust to match your values.

For good accuracy make sure your wavelengths are quite far apart, ideally one at the red end and one at the blue end

Screenshot

Alternatively, you may use a Fluorescent tube (or any other...

Read more »

Zip Archive - 2.42 MB - 08/09/2021 at 20:30

Download

  • Pi Zero 2 W Spectrometer Video!

    LesWright10/31/2021 at 09:38 0 comments

  • Raspberry Pi Zero 2 W Good News!

    LesWright10/29/2021 at 20:55 0 comments

    I ordered a Pi zero 2 W, and installed the software.

    Due to limited RAM, some dependencies needed to be installed with : --no-cache-dir

    For example: pip3 install pip install scipy --no-cache-dir

    Other then that, I am happy to report the software is perfectly usable on the Pi Zero 2W!

    It runs a little toasty. Running the software over x11vnc, at a room Temp of 22.3C, I was seeing temps of 62C after 5 minutes of running. A heastsink and/or fan is probably a good idea.

  • Future work

    LesWright10/28/2021 at 10:54 0 comments

    Although the project as it stands is Finished in that it performs the requirements I originally set out, I have had some feedback on potential avenues for further work including:

    • Upgrading the camera system (Potential candidate already on order)
    • Ability to plot spectrum against time (spectrogram or waterfall display and export the data) I will have to think carefully about how best to do this. Feedback is welcome!

  • Going to test a new Camera

    LesWright10/20/2021 at 20:25 0 comments

    I just ordered a new camera to test: Arducam OV9281 1MP Mono Global Shutter MIPI Camera for the Raspberry Pi.

    If the tests with Debayered cameras are anything to go by, I am hoping for some really nice results with this, namely better spectral response and better resolving ability for things like Fraunhoffer lines.

    Of course,  this will drive up the cost of the project a little and may require some tinkering with the software, but the returns might well be worth it.

    i will post again in here once it arrives and I have done some tests.

  • Build instructions updated

    LesWright10/19/2021 at 18:43 0 comments

    The build instructions are updated, and a short 5 minute intro video added to the project.

    The build instructions also contain the three YouTube videos, if you prefer to watch something rather than read about it!

  • Update parts list for Miniature version!

    LesWright10/18/2021 at 15:32 0 comments

    Updated parts list has been added if you wish to build the miniature version.

    Links have been provided as a "Shopping list" for your convenience. Note, I am not affiliated with any suppliers!

    You can find the Links in the Project description, and on GitHub if you prefer: https://github.com/leswright1977/PySpectrometer

  • Quick update...

    LesWright10/09/2021 at 19:22 0 comments

    Glad to see people are interested in this project and doing their own things with it!

    mash.m designed 3D printed parts! Check it out:

    https://www.tinkercad.com/things/fz8NYdaUxby

    https://www.tinkercad.com/things/basXpPEWYCm

    mash.m also designed a 3D Print for the mini version, for those of you who own a 3D printer!

    https://www.tinkercad.com/things/fz8NYdaUxby

  • GitHub updated

    LesWright09/01/2021 at 11:49 0 comments

    The 3 youtube videos (Py Spectrometer, Software update, and Miniature spectrometer) have been added to GitHub, so they are easier to find for those who want to build this.

  • Debayering a picamera with Lasers!!!

    LesWright08/14/2021 at 20:32 0 comments

    If you are really, really interested, there is  spin-off project here, where I debayer a picam with a laser!!!!

  • Miniature PySpectrometer

    LesWright08/10/2021 at 20:51 0 comments

    A miniature version of this projet has been built Weighing in at just 51 g:

View all 14 project logs

  • 1
    Build instructions

    For a good overview for the project and a good place to start watch the videos.

    The physical build is simple. The heavy lifting is all done in Software, which is Open Source!

    A video of the original Py-Spectrometer is available here (if you want something smaller, scroll down to the 3rd video!)

    An explanation of the new software features is here:

    The Miniature build video is here:





    Hardware

    Screenshot

    The hardware consists of:

    A commercial Diffraction grating Spectroscope https://www.patonhawksley.com/product-page/benchtop-spectroscope

    A Raspberry Pi Camera (with an M12 Thread) https://thepihut.com/products/raspberry-pi-camera-adjustable-focus-5mp

    A CCTV Lens with Zoom (M12 Thread) (Search eBay for F1.6 zoom lens)

    Everything is assembled on an aluminium base (note the Camera is not cooled, the heatsink was a conveniently sized piece of aluminium.)

    Screenshot

    Screenshot

    For the MINIATURE version the hardware is:

    Screenshot

    A commercial Diffraction grating Pocket Spectroscope: https://www.patonhawksley.com/product-page/pocket-spectroscope

    A Raspberry Pi Camera (with an M12 Thread): https://thepihut.com/products/raspberry-pi-camera-adjustable-focus-5mp

    M12x0.5 F2.0 Fixed 12mm Focal length Lens: https://www.ebay.co.uk/itm/114551239930

    ***Assembly***

    For either version:

    Mount the lens onto the picamera. Mount the Picamera and Lens in such a way that it "looks down the barrel" of the spectroscope.

    Refer to the above photographs for example mounts.

    Point the spectromter at a light source and preview with a utility such as  raspistill.

    Use the command: raspistill -t0 for a live view.

    Adjust the lens so the spectrum is in focus and rotate the sectroscope so the spectrum is aligned horizontally. The blue end should be on the left and the red on the right.

    ***Installation***

    Developed and tested on: 2021-01-11-raspios-buster-armhf-full.img for anything else your milage may vary!

    Rasberry pi 4 and PiCamera Recommended.

    (Note the software uses the Linux Video Driver, not the Picam Python module. As a consequence it will work with some webcams on probably any Linux box (Tested on Debian with a random webcam))

    First attach the Picam, and enable it with raspi-config

    Install the dependencies:

    sudo apt-get install python3-opencv

    sudo apt-get install python-dev libatlas-base-dev

    pip3 install scipy

    pip3 install peakutils

    Run the program with: python3 pyspectrometer-v1.py

    To calibrate, shine 2 Lasers of known wavelength (He-Ne, Argon or DPSS recommended! (Diode Lasers can have wavelengths that can be +/- several nm!)) at a piece of card in front of the spectrometer.

    Click the two peaks on the graph, and in each of the boxes enter the corresponding wavelength. Then hit 'Calibrate'. In this example I have Calibrated with 532nm (DPSS) and 633nm He-Ne. The Scale and lablels will then adjust to match your values.

    For good accuracy make sure your wavelengths are quite far apart, ideally one at the red end and one at the blue end

    Screenshot

    Alternatively, you may use a Fluorescent tube (or any other gas discharge tube) in front of the Spectrometer, you will have to research the wavelengths of the emission lines (Mercury for Fluorescent tubes, Neon, Argon, Xenon for other types) That will be an excercise for you!

    Other settings

    "Label Peak width" and "Label threshold" set the width of a peak to label, and the level to consider it a peak respectively. The Defaults are fine, but if you find the graph too cluttered, you can play with these values.

    Snapshot, takes a snapshot of the graph section like this: Screenshot

View all instructions

Enjoy this project?

Share

Discussions

Térence wrote 12/30/2023 at 18:23 point

does it work with pico???

  Are you sure? yes | no

Pedro Pereira wrote 11/21/2023 at 12:07 point

First of all my congrats for this wonderful project - TOP. I would like to know if somehow, it would be possible to get a PAR or PPFD (RGB) reading from the spectrum analysed. It would be great for all of indoor plant home growing community, that like me (Bonsai lover) need to make sure our plants are getting the best light (mimic sunlight) indoor and check if the grow lights bought are really good or not. Best regards and congrats once again.

  Are you sure? yes | no

DalChrome wrote 07/21/2022 at 12:49 point

Great project. Can this be adapted as a spectrophotometer for analysing samples by reflection or is it really just for light sources?

  Are you sure? yes | no

M K wrote 11/21/2021 at 02:42 point

Congrats on your win today! 

  Are you sure? yes | no

roman keller wrote 11/14/2021 at 15:03 point

dear leswright, did you ever try with the raspi hq camera module? and would you know in what angle the spectrometers measure? thanks in advance! 

  Are you sure? yes | no

j wrote 09/01/2021 at 13:38 point

Nice Project! 

Do you have any identification routine with backend database, if not is it planed? Keep up the great work!

  Are you sure? yes | no

LesWright wrote 09/01/2021 at 20:51 point

Do you mean a database of known emission spectra against which to compare the spectra being viewed?

I suppose it could be done, though I suspect it would be a lot more complex than you might expect. It depends what you want to measure.

If you wanted to measure emission spectra for example, say from gas discharge tubes and the like, it could probably be done in quite a straightforward fashion, as they produce light in discrete lines.

The same should hold true for Fraunhofer lines. Being discrete bands, it should be possible to root out and identify those as well.

Curves on the other hand (absorption and emission), well that is going to be a real challenge! A quick google, leads me to believe there are Python libs that might well be suitable to compare curves...

I can certainly look into it (time permitting). As described above discrete lines would be the place to start

  Are you sure? yes | no

j wrote 09/01/2021 at 21:38 point

Yea, that's what i mean. 

Finding those maxias and compare them of all known discharge spectra sound like a good start. I see the complications with reflective measurements. 

I'm working on a cost effective reference sample for a baseline for proper comparable reflection coefficients measurements, made with barium sulfate and white paint - if it's done properly its as white as spectralon, witch it's quite high in price. 

How many Channels did you acquire with this setup? It looks like >500 with a FWHM of about 1 nm, as you mentioned.

Just for the kicks: Do you see chances to see the hyperfine D-lines of discharging sodium ? Would a full HD cam or 4k and maybe a tiny slit in front of the tube help to see those lines? The D-lines of sodium are at 589.59 and 588.99 nm, so the resolution for that should be <0.6 nm.

sorry for getting nerdy here (:

  Are you sure? yes | no

Comedicles wrote 08/31/2021 at 21:51 point

I gotta ask, why use Tkinter on a Pi? You can easily use PyQt and get a more versatile and better looking GUI.

  Are you sure? yes | no

LesWright wrote 09/01/2021 at 12:01 point

It's already included in Python, so one less thing to worry about and I have coded with it before. You are right though, Tk does have its limitations, though it was good enough to get the job done. I am sure there are better looking packages.

  Are you sure? yes | no

Lightning Phil wrote 08/22/2021 at 17:57 point

Just received a diffraction spectroscope.  Nice instrument!  Hope to find time to make a spectrometer.  In the mean time, looking at things through it is fun.

  Are you sure? yes | no

LesWright wrote 08/22/2021 at 18:23 point

Awesome! It's beautiful right. Nice spectra to be had, even around the home!

  Are you sure? yes | no

LesWright wrote 08/14/2021 at 20:34 point

You are welcome!

  Are you sure? yes | no

Kev wrote 08/14/2021 at 15:07 point

This is very cool, I like this. Thank you for sharing.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates