Click here to Skip to main content
Click here to Skip to main content

LED Example with Measurement Studio

By , 28 Sep 2010
Rate this:
Please Sign up or sign in to vote.

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

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:

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)

About the Author

Nick Alexeev
Systems Engineer Prolitech
United States United States
doing business as Prolitech
Redwood City, CA
http://www.prolifictec.com

Comments and Discussions

 
GeneralGhosts of the past PinmemberJohn Whitmire5-Oct-10 4:21 
GeneralRe: Ghosts of the past PinmemberNick Alexeev5-Oct-10 20:16 
Initially, I’ve started to use Measurement Studio because I needed chart controls that can plot a lot of real time data - about 1000 points per second on 10 or so separate charts. The data would normally come from a serial port.
 
Last Spring I’ve done a sizable project for one of my clients where Measurement Studio was used to control hardware more directly (through DAQmx). Although the best practices were used, it was a fast paced project with some “code first, think later” attitude.
 
Fortunately, I’ve got time to think now at the beginning of the next one. I went through the Spring’s project and compiled the class diagrams. Then I realized that putting them on the web could be useful. The rest of the article provides context for the diagrams.
GeneralMy vote of 2 PinmemberJoe Sonderegger4-Oct-10 20:15 
GeneralRe: My vote of 2 PinmemberNick Alexeev4-Oct-10 20:35 
GeneralRe: My vote of 2 PinmemberJoe Sonderegger5-Oct-10 3:47 
GeneralMake vs. Buy PinmemberNick Alexeev10-Oct-10 11:03 
GeneralAbsent code PinmemberVitaly28-Sep-10 20:09 
GeneralRe: Absent code PinmemberNick Alexeev28-Sep-10 21:14 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 29 Sep 2010
Article Copyright 2010 by Nick Alexeev
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid