Skip to main content

Programming NI DAQmx board in Python: easier than you think!

For my DIY microscope I had a task - generate a train of digital pulses which simulate camera trigger, so that other devices (galvo and laser) are synched. I wanted to do it in Python, so that it seamlessly integrates in my data acquisition and analysis Jupyter notebook.

After some quick search I found a PyDAQmx library which seemed mature and had good examples to begin with.

Installation was smooth: download, unzip, open Anaconda prompt,
python setup.py install

After only 30 min fiddling, I was able to solve my problem in just a few lines of code:

Holy crap, it just works, out of the box. Oscilloscope shows nice digital pulses every 100 ms, each 1 ms long. The code is much shorter and cleaner than would be in C, C#, or LabView.

PyDAQmx appears to be a full-power wrapper around native NI DAQmx drivers (yes, they need to be installed), so presumably it can do all that can be done in C or even LabView (this statement needs to be tested).

One can use PyDAQmx to control galvos with fast analog output waveforms, as shown by @kyleellefsen in his code.

Many thanks to @clade for writing PyDAQmx! Really made my day..

My system was: Windows 10, Python 2.7 (x32), NI PCIe-6321 board.

Comments

Popular posts from this blog

Machine shopping for a microscopy lab

Disclaimer: I believe that everyone who can hang a picture on the wall can work in a machine shop. However, if you are sloppy, forgetful, or messy, don't do it. Or at least read the manuals and learn safety instructions before you go.

If you are still reading this, you are not easily scared! Welcome to the world of DIY fun and creativity which a machine shop provides. Let's start with the most common myths.

Myth 1. Machine shop is for old-school dudes who like to fix their motorcycles - today one can buy online everything needed for science.
If you can buy everything - you follow mainstream, because your tools are old and popular enough that a company makes money making and selling them. If you hit an unbeaten path, or even make adjustments, you need to invent and make new tools. Of course, you can hire engineers - but research labs are rarely that rich.

Myth 2. Machine shop is a big and expensive enterprise, only big institutes can afford it. 
MS can be as big or small as you m…

3D modeling in a lab

About once a week I am asked by my colleagues which 3D modeling software I am using - usually when I am staring at the new part being 3D printed.

I am using Autodesk Inventor for a few reasons:
it is a professional software for engineers and has huge community around itit provides freeacademic licensethere are thousands of youtube videos with detailed tutorials by enthusiastseasy to learn at a basic level, but there is always a lot of room for growth In a lab, there are two main workflows where Inventor is necessary: 3D modeling of complex assemblies (like custom-built microscope) and 3D printing. There are many youtube tutorials for beginners, so I here only review some things that Inventor can do, without any specific instructions.  3D modeling of parts and assemblies Before building a new microscope, you can create its virtual model and check dimensions, required adapters, and whether things will fit together. Luckily, Thorlabs has 3D model of nearly all its parts available for fre…

Programming of DIY microscopes: MicroManager vs LabVIEW

In the flourishing field of DIY light microscopy, a decision of choosing the programming language to control the microscope is critically important. Modern microscopes are becoming increasingly intelligent. They orchestrate multiple devices (lasers, cameras, shutters, pockel cells) with ever increasing temporal precision, collect data semi-automatically following user-defined scenarios, adjust focus and illumination to follow the motion (or development) of a living organism.
So, the programming language must seamlessly communicate with hardware, allow devices be easily added or removed, have rich libraries for device drivers and image processing, and allow coding of good-looking and smooth GUIs for end users. This is a long list of requirements! So, what are the  options for DIY microscope programming?

There are currently two large schools of microscope programming - Labviewers and Micromanagers. (update: Matlab for microscope control also has a strong community, comparable to labview…