Click here to Skip to main content
15,797,167 members
Articles / Programming Languages / C#

LED Example with Measurement Studio

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
28 Sep 2010CPOL3 min read 67K   2K   25   9
How-to article describing software, hardware setup


Figure 1. User interface


Figure 2. Photo of the hardware setup

(Click for larger photo).


In order to bring-up a small piece of hardware, I had to whip-up a quick utility for controlling an LED light source. My tools of choice for this type of task are a C#, National Instrument Measurement Studio, USB-6008 input/output card. At some point, I realized that a write-up like this one may be useful for those who are scratching the surface of Measurement Studio.


The setup is built around National Instruments USB-6008 multipurpose I/O device. I was connected to the laptop via USB. Constant current LED driver and LED itself were on a separate board (more detailed schematic).

LED output is proportional to the current. The driver circuit is design so that the LED current is proportional to the control voltage V_SET_I, which is generated by one of the analog outputs on USB-6008. Another digital control signal turns LED on and off.


Figure 3. Block diagram of the hardware setup.

(I've also uploaded the schematic of the constant current LED driver.)


The purpose of the software is to provide a simple GUI, which allows the user to control the LED intensity with a slider and to turn the LED on/off with a toggle button.

Class Diagram

Subclasses of DAQmx.Task are responsible for accessing the hardware channels. They expose public methods and properties while hiding the details such as physical designation of the channels involved. For our tasks, additional writer objects (AnalogSingleChannelWriter and DigitalSingleChannelWriter) are also created.



A tempting mistake is to try to create a task that contains more than one type of channel. For example, it could be analog output channel (AO) and digital output channel (DO).

	("Dev2/ao1", "LED brightness", 0, 5, AOVoltageUnits.Volts);
this.DOChannels.CreateChannel("Dev2/port0/line0", "LED on", 
	ChannelLineGrouping.OneChannelForEachLine);  // an attempt to create the 
				// 2nd type of a channel will cause an exception


Figure 5.

This is easy to address by making a separate subclass of DAQmx.Task for each of the channel types you want to use (see Figure 4).

About the Source Code

The code for this project was hand-written. At the same time, MStudio has a wizard that can generate code for tasks. If you’d like to learn more about the wizard, have a look at [4]. The code for the project is written in a bare bones style: has hard coded parameters, no sanity checking, no error handling. That makes it easier to see the calls to MStudio.

Points of Interest

Let’s take a look at one of the DAQmx.Task subclasses - the LEDOn. Its purpose is to toggle a digital output. The bare bones code is small:

class LEDOn : NationalInstruments.DAQmx.Task
    private DigitalSingleChannelWriter  m_dscwLEDon;

    public void Init()
        this.DOChannels.CreateChannel("Dev2/port0/line0", "LED on", 
	ChannelLineGrouping.OneChannelForEachLine); 	// create the channel, 
						// specify hardware
        this.Control(TaskAction.Verify);    	// transition task to the verified state
        m_dscwLEDon = new DigitalSingleChannelWriter(this.Stream);  // create the 
							// stream writer

    public bool On
        set { m_dscwLEDon.WriteSingleSampleSingleLine(true, value); }

The form class instantiates the LEDOn class (and other task classes), then it calls the initialization, then it can write to the channels.


Before using a channel, a small initialization has to take place:

  1. A channel is created within the task.
  2. Task is verified.
  3. Appropriate writer is created and connected to the task’s stream.

All this is done in LEDOn.Init():


Figure 6. State transition diagram for a DAQmx.Task object

Writing to the Channel

Writing to the channel is done by calling the WriteSingleSampleSingleLine() method of the channel writer object. Other types of channels have similar WriteX() methods. Notice that autoStart is set to true. The task briefly goes to the Running state, writes the sample, goes back to Committed state. There is a useful article in the MStudio online help “Using the Start Task function/VI”, which details when to call Start() and Stop(), and how autoStart works.

Closing Remarks

I’m planning to add a more elaborate version of this project that has sanity checking and error handling.

As always, bug notes, suggestions, insight, comments, requests, etc. are welcome!


[1] Official home page for National Instruments Measurement Studio

[2] .NET section of Measurement Studio forum

[3] USB-6008 multipurpose I/O device

[4] Walkthrough: Creating a MeasurementStudio NI-DAQmx Application

[5] Measurement Studio .NET class hierarchy chart

Acronyms and Abbreviations

NI National Instruments
MStudio Measurement Studio made by NI
DAQ Data Acquisition


  • Initial draft. September 26, 2010


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Systems Engineer Prolitech
United States United States
doing business as Prolitech
Redwood City, CA

blog (mostly technical)

Comments and Discussions

QuestionGive me some suggestions Pin
FanLJ17-Jul-14 22:17
FanLJ17-Jul-14 22:17 
GeneralGhosts of the past Pin
John Whitmire5-Oct-10 5:21
professionalJohn Whitmire5-Oct-10 5:21 
GeneralRe: Ghosts of the past Pin
Nick Alexeev5-Oct-10 21:16
professionalNick Alexeev5-Oct-10 21:16 
GeneralMy vote of 2 Pin
Joe Sonderegger4-Oct-10 21:15
Joe Sonderegger4-Oct-10 21:15 
GeneralRe: My vote of 2 Pin
Nick Alexeev4-Oct-10 21:35
professionalNick Alexeev4-Oct-10 21:35 
GeneralRe: My vote of 2 Pin
Joe Sonderegger5-Oct-10 4:47
Joe Sonderegger5-Oct-10 4:47 
GeneralMake vs. Buy Pin
Nick Alexeev10-Oct-10 12:03
professionalNick Alexeev10-Oct-10 12:03 
GeneralAbsent code Pin
Vitaly28-Sep-10 21:09
Vitaly28-Sep-10 21:09 
GeneralRe: Absent code Pin
Nick Alexeev28-Sep-10 22:14
professionalNick Alexeev28-Sep-10 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.