cart My Cart 0
☰ Menu

Our Blog

Get the latest

Push Button; Collect spectrum

Posted on: October 21st, 2020

In this installment, we’re going to looking at how to set up a simple push-button interface to collect spectral data on the Raspberry Pi. While we’re using a button, this tutorial can translate to any sort of analog input!

Setting Up

You’ll need an Avantes Spectrometer (I’ll be using the mini), a raspberry pi, and a trigger or button. You should also have installed the Avantes SDK and tested your spectrometer with a sample script. Check and make sure that Python and the gpiozero library are installed on your pi.

Saving Spectra

We’ll start by writing a function to save a spectrum as a file. For this, we’ll save a file in the save ascii format used by AvaSoft. This includes a header describing the acquisition parameters. So we will pass this method the wavelength and intensity values, as well as the acquisition configuration.

from datetime import datetime as dt
import csv

def save_spectrum_as_ascii(wavelengths,intensities,measureConfig,serialNumber=None, name=None):
    timestamp = dt.now().strftime("%Y-%m-%d--%H-%M-%S")
    filename = timestamp + ".txt"
    int_time = measureConfig.m_IntegrationTime
    averaging = measureConfig.m_NrAverages
    smoothing = measureConfig.m_Smoothing_m_SmoothPix
    header = [ "Integration time [ms]: {}".format(int_time),
               "Averaging Nr. [scans]: {}".format(averaging),
               "Smoothing Nr. [pixels]: {}".format(smoothing),
               "Data measured with spectrometer [{}]: {}".format(name,serial_number),
               ["Wave", "Sample"],
               ["[nm]", "[counts]"]
             ]
              
    with open(filename, 'w') as file:
        csvwriter = csv.writer(file, delimiter=';')
        csvwriter.writerows(header)
        csvwriter.writerows(zip(wavelengths,intensities))

 

Remember, you can find your device’s serial number in the AvsIdentityType and the user friendly name can be found in the DetectorConfigType as m_aUserFriendlyId[64].

Set the trigger

To set up the trigger, we need to assign it to a function. Since we’re trying to take and save a spectrum, let’s call our function take_and_save_spectrum

import gpiozero
import signal

button = gpiozero.Button(18)
button.when_pressed = take_and_save_spectrum

signal.pause()

There is one problem here – the button has no way of passing parameters (except the button itself) to the function. There are several ways to do this; the easiest is to group our sample script’s code into a take_spectrum function:

import time

def take_spectrum():
    dataready = False
    while not dataready:
        dataready = AVS_PollScan(handle)
        time.sleep(0.5)

    timestamp = 0 
    spectraldata = [] 
    ret = AVS_GetScopeData(handle, timestamp, spectraldata) 
    timestamp = ret[0] 
    for i,pix in enumerate(wavelengths): 
        spectraldata.append(ret[1][i])
    
    return spectraldata

As written, this method uses the handle variable without having been explicitly passed it as a parameter. This works in the scope of this script; however, it can create problems later on, is sloppy, and makes the code less reusable. In a future post, we’ll look at creating a spectrometer class that is much more usable.

 

 

Request the AvaSoft Developer's DLL Kit

Start developing your unique application interface now. Need help getting started? We'll be standing by.