Raspberry Pi Spectrometer

Overview

PySpectrometer 2021-03-05

Raspberry Pi Spectrometer

Screenshot

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.

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: https://www.youtube.com/watch?v=T_goVwwxKE4

This program, hardware design, and associated information is Open Source (see Licence), but if you are a commercial entity or have just gotten value from these kinds of projects and think they are worth something, please consider donating: https://paypal.me/leslaboratory?locale.x=en_GB

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

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!

TODO Add in a 3 wavelength Calibration functionality to conteract any nonlinearity caused by misalignment of the camera and 'scope. Non Linearity can be solved by rotating the camera on its axis, but it would be nice to just fire and forget.

Also, in a future version add 'Peak Hold' button, so that tuning curves for Laser dyes can be recorded.

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

Example Spectra

Here is an example of the spectrum of a fluorescent bulb. The peaks at 405,435,545,650 are Mercury, Europium (one of the lamp phosphors) is visible at ~610nm.

Screenshot

Measuring the wavelength of a cheap red laser pointer (661nm)

Screenshot

Measuring the wavelength of a cheap violet Laser pointer, note the strong fluorescence from the paper! Paper is optically brightened with a fluorescent dyes, most likely Coumarin.

Screenshot

The spectrum of Daylight (pointed out of the window at a blue sky)

Screenshot

The spectrum of of a Helium-Neon Discharge.

Screenshot

TODO

Done! Add a slider to control smoothing. See pyspectrometer-v2.py This changes the properties of the Savgol filter. By default is is set at 7.

Minimum smoothing applied:

Screenshot

Maximum smoothing applied:

Screenshot

Comments
  • 2021-01-11-raspios-buster-armhf too old for Rpi 4

    2021-01-11-raspios-buster-armhf too old for Rpi 4

    I just bought the Rpi 4 for wonderful pyspectrometer project.

    Unfortunately my Rpi4 cannot boot itself with recommended 2021-01-11-raspios-buster-armhf. The Legacy and 2021-03-04-raspios-buster does not allow to install scipy. The computer gets unrensponsive so I have to make a hard reset.

    Any help would be very appreciated!

    Regards Andrzej

    Screenshot 3

    opened by 68artik 8
  • Feature request: long time exposure

    Feature request: long time exposure

    Dear Les,

    this is a great project. Thank you for sharing! I found it via hackaday.

    I am thinking about if I could use it for astronomy. Because the light sources (stars) are very dim, a long time exposure would be needed. Is there an advantage if the HQ Pi Cam is used instead of the Standard Pi cam for this case?

    I just leave my thought for a long time exposure here for discussion. I more people are interested, maybe someone could implement it. Otherwise just close this ticket.

    Keep up the great work :)

    opened by gon0 7
  • Averaging to remove sensor noise

    Averaging to remove sensor noise

    I suggest adding some sort of averaging from several frames, so that sensor noise doesn't get captured as legitimate spectrum data. Ideally, user should be able to toggle this function on/off as necessary.

    I have not looked at the code too much yet, only at a glance. Besides python is not my number one language, but I am willing to help you add something that does the averaging.

    Also, if you accept it, I saw some room for improvement with the code.

    Me: US based software engineer by trade.

    ps: averaging can be done by grabbing data from several rows above and below the "main scan line", instead of waiting for several frames to accumulate.

    opened by antonzuykov 2
  • Project can't be cloned on Windows

    Project can't be cloned on Windows

    This project currently can't be cloned onto a Windows machine due to the colons in the filename of this image: media/spectrum-09-04-2021-15:19:27.jpg

    Renaming the file to something like spectrum-09-04-2021-15-19-27.jpg will solve this issue.

    opened by astuder 2
  • Add license information

    Add license information

    Hello!

    I found link to this project from https://hackaday.com/2021/04/23/pi-based-spectrometer-puts-the-complexity-in-the-software/

    Could you please add information about license of this repository — is it allowed to copy, change and use the code and images in any purpose? It would be great to see any popular open source license.

    opened by vazhnov 2
  • Near infrared spectrometer ranges from 400nm to 1100nm

    Near infrared spectrometer ranges from 400nm to 1100nm

    Hello leswright 1977, I am an engineer from China; I currently have a laboratory project that needs to use a spectrometer to measure the near infrared spectrum. The spectral range is 400 to 1100nm;; Check the commercial spectrometer, it is too expensive; One is about 10000 RMB; So I want to DIY myself;

    I watched your YouTube video, which only supports the spectrum of 400 to 700nm; I found a spectroscope on Taobao, and found that there was only a spectroscope with the wavelength of 400 to 700nm used to identify gemstones; This can not meet my needs, so I contacted you to inquire if there is a spectrometer that can measure 400nm to 1100nm to meet my project;

    My email address is [email protected] , looking forward to your reply! A friend from China

    opened by zhuziwen 1
  • Allow the video source to be specified on the command line

    Allow the video source to be specified on the command line

    So that I can easily use my spectrometer with different computers, some of which already have other video sources.

    Is the preferred way for people to submit pull requests to just change the latest "version" in the src/ directory? Having to ask you to make a new revision every time someone submits a change does seem kind of inconvenient for you (and potentially others that maintain their own customisations (for example me)). Would you perhaps be open to a pull request that unified the project history into a single src/pyspectrometer.py and instead used git's "tag" feature to represent revisions and allow folks to easily run any revision they chose?

    Thanks!

    opened by rafl 1
  • How much memory is needed?

    How much memory is needed?

    Hi, great project, congratulations, could you please let me know which Raspberry Pi 4 version did you use for the project? How much memory? What is the average memory utilization when running the software?

    opened by tytusd 1
  • dependency PIL.ImageTk missing

    dependency PIL.ImageTk missing

    I start with fresh image "Raspberry Pi Os with Desktop" (from 2021-03-04) and got:

    pi@raspberrypi:~/PySpectrometer/src $ python3 pyspectrometer-v3.py
    Traceback (most recent call last):
      File "pyspectrometer-v3.py", line 46, in <module>
        import PIL.Image, PIL.ImageTk
    ModuleNotFoundError: No module named 'PIL.ImageTk'
    

    It seems, that one dependency is missing - I had to add: sudo apt-get install python3-pil.imagetk

    opened by sigmaeo 1
  • RPI Zero2 W scipy dependency fails to install

    RPI Zero2 W scipy dependency fails to install

    When installing on a zero 2 W, the scipy install dies on numpy. It's a memory issue, since you only have 300 some MB. You'll need to increase the swapfile size, at least temporarily. Alternatively install the dependencies on a full size Pi4 and transfer the folders.

    also don't forget to run apt get update.

    I followed the instructions at: https://pimylifeup.com/raspberry-pi-swap-file/

    to save some clicks, here's the terminal commands

    sudo dphys-swapfile swapoff sudo nano /etc/dphys-swapfile ctrl-w CONF_SWAPSIZE=100 ---change 100 to 1024 or 2048. I wasted enough time last night. I changed it to 2048 and it finished in about an hour. ctrl-x y sudo dphys-swapfile setup sudo dphys-swapfile swapon ----then reboot sudo reboot

    opened by VaporwareII 0
  • GStreamer: pipeline have not been created

    GStreamer: pipeline have not been created

    Hello!

    Great project! I am just having a bit of an issue with openCV and GStreamer. Any suggestions would be much appreciated

    [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1824) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module v4l2src0 reported: Failed to allocate required memory. [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (914) open OpenCV | GStreamer warning: unable to start pipeline [ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (501) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created

    opened by jresch 11
  • Add 3D printed models to use regular raspberry pi camera

    Add 3D printed models to use regular raspberry pi camera

    Hello! this project is awasome!

    I just found a c-mount CCTV lens on my local store so I made this 3D models to print the camera/spectrometer support and lens thread to use a regular (oficial) v2 raspberry pi camera.

    Also I made some modifications in the README.md to acomodate some things and structure it better. I hope you don't mind. Also added an example testing a bandpass filter in front of the spectrometer.

    thank you!

    opened by koalazak 0
  • API feature request

    API feature request

    I have to do a lot of color measurements on different RGBW led strips. It would be awesome to automate this process. It would be great if I could trigger a measurement externally with maybe a TCP of HTTP(S) command and get the measurements back as JSON (or some other format like comma delimited CSV like). Would you please consider this?

    opened by osenbruggen 4
  • Allow video_index, resolution and fps to be specified on commandline

    Allow video_index, resolution and fps to be specified on commandline

    This will allow for use on different computers and with weird USB cameras.

    My USB camera is aliexpress breed, which is quite picky about resolution+fps combinations. Also as I am using notebook as a device for doing analysis, the video0 is typically internal camera.

    I came across https://github.com/leswright1977/PySpectrometer/pull/13 only after introducing these changes, but approach seems to be quite compatible. I would also like to second @rafl question about code organization and suggest having just one file pyspectrometer.py and use git tags for version tracking.

    opened by bobek 0
  • question about spectroscope holder

    question about spectroscope holder

    Where did you buy the black piece that holds the spectroscope (its rectangular with a hole the right size for the spectroscope and with what looks like a brass screw for holding it in place)? It doesn't show it listed in the parts on the github page.

    opened by jr751 1
Owner
Les Wright
Hobby Programmer with interests in Electronics, Optics, Assembler, C, Python, AI, Robotics and sensing technologies.
Les Wright
Micro Displays for Raspberry Pi

micro-displays Micro Displays for Raspberry Pi Why? I'm super bored in lockdown. Add a Raspberry Pi 400 and a few tiny displays... The top half of the

ig 291 Jul 6, 2022
A Python class for controlling the Pimoroni RGB Keypad for Raspberry Pi Pico

rgbkeypad A Python class for controlling the Pimoroni RGB Keypad for the Raspberry Pi Pico. Compatible with MicroPython and CircuitPython. keypad = RG

Martin O'Hanlon 43 Nov 11, 2022
Create a low powered, renewable generation forecast display with a Raspberry Pi Zero & Inky wHAT.

GB Renewable Forecast Display This Raspberry Pi powered eInk display aims to give you a quick way to time your home energy usage to help balance the g

Andy Brace 32 Jul 2, 2022
Raspberry Pi Pico development platform for PlatformIO

Raspberry Pi Pico development platform for PlatformIO A few words in the beginning Before experimental please Reinstall the platform Version: 1.0.0 Th

Georgi Angelov 160 Dec 23, 2022
Raspberry Pi Pico support for VS Code

Pico-Go VS Code Extension Pico-Go provides code auto-completion and allows you to communicate with your Raspberry Pi Pico board using the built-in REP

Chris Wood 114 Dec 28, 2022
Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi

Clean Dashboard for Pi-Hole Minimal and clean dashboard to visualize some stats of Pi-Hole with an E-Ink display attached to your Raspberry Pi.

Alessio Santoru 104 Dec 14, 2022
A global contest to grow and monitor your own food with Raspberry Pi

growlab A global contest to grow and monitor your own food with Raspberry Pi A capture from phototimer of my seed tray with a wide-angle camera positi

Alex Ellis 442 Dec 23, 2022
A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler.

Env-MCRO A Raspberry Pi Pico powered Macro board, like a Streamdeck but cheaper and simpler. (btw this image is a bit outdated, some of the silkscreen

EnviousData 68 Oct 14, 2022
A Macropad using the Raspberry Pi Pico, programmed with CircuitPython.

A Macropad using the Raspberry Pi Pico, programmed with CircuitPython.

null 15 Oct 14, 2022
Raspberry Pi Pico and LoRaWAN from CircuitPython

Raspberry Pi Pico and LoRaWAN from CircuitPython Enable LoRaWAN communications on your Raspberry Pi Pico or any RP2040-based board using CircuitPython

Alasdair Allan 15 Oct 8, 2022
a weather application for the raspberry pi and the Pimorioni Inky pHAT.

raspi-weather a weather application for the raspberry pi and the Inky pHAT

Derek Caelin 59 Oct 24, 2022
A versatile program that uses the raspberry pi camera and provides it as a service

PiCameleon Is a daemon program meant to provide the RaspberryPi Camera as a service while running according to a configuration.

André Esser 52 Oct 16, 2022
Monorepo for my Raspberry Pi dashboard and GPS satellite listener.

?? pi dashboard My blog post: Listening to Satellites with my Raspberry Pi This is the monorepo for my Raspberry Pi dashboard!

Andrew Healey 27 Jun 8, 2022
A low power 1U Raspberry Pi cluster server for inexpensive colocation.

Raspberry Pi 1U Server There are server colocation providers that allow hosting a 1U server for as low as $30/month, but there's a catch: There are re

Paul Brown 627 Dec 31, 2022
A Raspberry Pi Pico plant sensor hub coded in Micropython

plantsensor A Raspberry Pi Pico plant sensor hub coded in Micropython I used: 1x Raspberry Pi Pico - microcontroller 1x Waveshare Pico OLED 1.3 - scre

null 78 Sep 20, 2022
KIRI - Keyboard Interception, Remapping, and Injection using Raspberry Pi as an HID Proxy.

KIRI - Keyboard Interception, Remapping and Injection using Raspberry Pi as a HID Proxy. Near limitless abilities for a keyboard warrior. Features Sim

Viggo Falster 10 Dec 23, 2022
Using a raspberry pi, we listen to the coffee machine and count the number of coffee consumption

A typical datarootsian consumes high-quality fresh coffee in their office environment. The board of dataroots had a very critical decision by the end of 2021-Q2 regarding coffee consumption.

dataroots 51 Nov 21, 2022
Raspberry Pi Pico Escape Room game.

Pico Escape Room Raspberry Pi Pico Escape Room game. Parts Raspberry Pi Pico Set of 2 x 20-pin Headers for Raspberry Pi Pico 4PCS Breadboards Kit Incl

Kevin Thomas 5 Feb 2, 2022
E-Ink Magic Calendar that automatically syncs to Google Calendar and runs off a battery powered Raspberry Pi Zero

E-Ink Magic Calendar that automatically syncs to Google Calendar and runs off a battery powered Raspberry Pi Zero

null 2.8k Dec 30, 2022