Control an Ocean Optics Spectrometer in Python

Control an Ocean Optics Spectrometer in Python

View the app user interface

Here at Plotly, we wrote a Dash DAQ application to interface with an Ocean Optics spectrometer. With interactive UI components written in Python, you can read and control the instrument in real-time.

Try out the app, and keep reading to see how we built it!

First, we need to import all of the Dash packages that allow the app to work. Additionally, since we are displaying the spectra, we need to import Plotly’s graph objects:

We create separate classes that abstract the functionality of the spectrometer and of its controls, and import those as well. This lets us keep things modular. This is helpful if you are working with multiple spectrometers from different manufacturers, which may have similar functions but different protocols for communication.

To interface with an Ocean Optics spectrometer, we need the python-seabreeze library. We import it in the same file that we are using to hold our abstract definition of a spectrometer.Regardless of the model of spectrometer, we need to ensure that only one communication occurs at any given time, and that at most, one process can modify its properties. Consequently, when declaring a new spectrometer, we assign to it two locks:

The power button activates the application. We add a Dash DAQ PowerButton component to the app’s layout:

Upon being activated, the application displays live spectra on an interactive graph. We add the graph to the app’s layout, and associate it with an interval of 1,000 ms (the refresh rate):

Then we attach a callback to this interval that updates the plot, but only if the application is active.

The other main feature of this application is its ability to adjust the settings  of the spectrometer or its peripherals. For instance, a Dash DAQ Knob component is used to set the intensity of the connected light source, if it exists. We create a component and wrap it in a div:

Then we create a callback which we associate with the value of the knob, the power button, and the selected light source. The knob is only enabled if the power button is on and a light source has been selected.

The light intensity updates immediately, but the values of the other controls are sent to the spectrometer only after pressing the “Update” button on the interface. For each of these settings, a Control object is created that abstracts its functions and properties. Dash DAQ components display the controls in a user-friendly way.

The status of each command (i.e., whether or not it failed) is displayed in the box immediately under the button. If the power button is off, the application does not provide any information about the status. Consequently, we need to make a callback that associates all of the controls with the power button, the submit button, and the submit status box:

You can check out this demo app online. Purchase a Dash DAQ license to access the full code for this application, and many others, and start creating interactive, intuitive, graphical user interfaces for your lab instruments!