Click here to Skip to main content
15,886,676 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 67.8K   2K   25   9
How-to article describing software, hardware setup

form.PNG

Figure 1. User interface

hardware_setup_photo_small.PNG

Figure 2. Photo of the hardware setup

(Click for larger photo).

Introduction

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.

Hardware

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.

hardware_setup_block_diagram.PNG

Figure 3. Block diagram of the hardware setup.

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

Software

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.

class_diagram__correct.PNG

legend.PNG

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).

C#
this.AOChannels.CreateVoltageChannel
	("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

class_diagram__incorrect.PNG

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:

C#
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.

Initialization

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():

task_state_machine.PNG

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!

References

[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

History

  • Initial draft. September 26, 2010

License

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)
http://prolifictec.com/blog/index.html

Comments and Discussions

 
QuestionGive me some suggestions Pin
FanLJ17-Jul-14 21:17
FanLJ17-Jul-14 21:17 
GeneralGhosts of the past Pin
John Whitmire5-Oct-10 4:21
professionalJohn Whitmire5-Oct-10 4:21 
GeneralRe: Ghosts of the past Pin
Nick Alexeev5-Oct-10 20:16
professionalNick Alexeev5-Oct-10 20:16 
GeneralMy vote of 2 Pin
Joe Sonderegger4-Oct-10 20:15
Joe Sonderegger4-Oct-10 20:15 
poor
GeneralRe: My vote of 2 Pin
Nick Alexeev4-Oct-10 20:35
professionalNick Alexeev4-Oct-10 20:35 
GeneralRe: My vote of 2 Pin
Joe Sonderegger5-Oct-10 3:47
Joe Sonderegger5-Oct-10 3:47 
GeneralMake vs. Buy Pin
Nick Alexeev10-Oct-10 11:03
professionalNick Alexeev10-Oct-10 11:03 
GeneralAbsent code Pin
Vitaly28-Sep-10 20:09
Vitaly28-Sep-10 20:09 
GeneralRe: Absent code Pin
Nick Alexeev28-Sep-10 21:14
professionalNick Alexeev28-Sep-10 21: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.