From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

Output Light Using a Basic 5mm LED, NI myDAQ, and LabVIEW

Code and Documents

Attachment

Download All

Overview


This document explains using a basic, low-cost 5mm LED to output light with your National Instruments myDAQ in LabVIEW. The digital output will be generated on the myDAQ digital line using the DAQ Assistant that is installed into LabVIEW with the NI DAQmx driver.

Objective: 

Use a basic 5mm LED (Light Emitting Diode) to output light using digital line output on your myDAQ device.

 

Background:

A basic 5mm LED is a low-cost solution for emitting light for various applications.  LEDs are often used to indicate if something is turned on, what power level a machine is running at, and for low-cost lighting solutions in remote locations.  It is important to know that a LED is a diode, which only allows current to flow in one direction.  This means that we have to connect the positive input to the positive lead of the lead, and the negative input to the negative lead.  Also, a LED can only handle a certain amount of current, so it is necessary to connect a resistor in series to regulate the current flow. 

 

Figure 1: Basic Red 5mm LED and 330Ω Resistor

 

What You Need:

  1. NI myDAQ
  2. LabVIEW
  3. Basic 5mm LED
  4. 330Ω 1/6th Watt Resistor
  5. Wire
  6. Breadboard

 

Wiring Instructions:

The LED is wired in series with a 330Ω resistor.  Assuming the LED has negligible resistance, the total resistance of the circuit is 330Ω.  According to Ohm’s Law, the current drive with a 5VDC power supply from the digital line would result in 5V÷330Ω= 15.15mA.  As stated in the specification sheet for the LED that is referenced at the bottom of the document, the Led can handle a current up to 20mA, and is recommended to be used with 16 to 18mA; so at 15.15mA we will be safe.  The LED requires a positive input on the longer lead side and a negative input on the shorter side, as seen in Figure 1 above.  We will supply power to this circuit using Digital Line 0 of the myDAQ, and ground the circuit to Digital Ground of the myDAQ.

 

Figure 2: Wiring Diagram

 

LabVIEW User Interface:

The user interface we created has a Boolean push button switch, Light Control, to turn the LED light on or off.

Figure 3: LabVIEW Front Panel

 

Coding Strategy:

In LabVIEW we need to create a front panel Boolean array control to output a 5V digital signal on Line0 to our basic 5mm LED.   This Boolean value is input into our DAQ Assistant which will output the corresponding value to the NI myDAQ digital port.  In digital Boolean logic, a True corresponds to a 5V signal, and a False corresponds to a 0V signal.  Therefore, a True will turn on the LED.

 

Figure 4: Coding Block Diagram

 

The LabVIEW diagram looks very similar to the coding block diagram, except the LED is not included.

Figure 5: LabVIEW 2009 Block Diagram

(The attached LabVIEW code snippet can be dragged-and-dropped to a LabVIEW block diagram, use attached PNG file.  After locating the PNG file, just drag the file icon onto a blank block diagram, as if you were dragging the file onto your desktop.)

 

How It Works:

Inside the while loop on the left is the Boolean Array Control used to send a True or False to the LED. The control is on the front panel, and can be seen to have only one element in the array.  This is because the DAQ Assistant is setup to only output to one line.  On the right is the DAQ Assistant.  It’s configured to output a single value from the myDAQ Digital Port0/Line0 each time it executes.  All of the code inside the While Loop continues to run until the Stop button is pressed on the front panel.  The Wait VI (top left) delays execution of the while loop to every 100ms.  Therefore the output rate is 10 samples per second, or 10 Hz.

In this VI the DAQ Assistant is configured for on-demand output on the digital channel.  The following steps walk through the configuration of the DAQ Assistant from scratch:

  1. Be sure your myDAQ is plugged in
  2. Press Ctrl-Space to bring up the Quick Drop Window (takes a full minute to load on the first use)
  3. Search for DAQ Assistant and double click on it when it appears in the list
  4. Drop it on the Block diagram (white window)
  5. When the Create a New Express Task configuration pane appears, select
    1. Generate Signals
    2. Digital Output
    3. Line Output
    4. Dev 1 (NI myDAQ) *Note: If you have other NI hardware installed, the myDAQ will not be Dev1.
    5. port0/line0
    6. Finish
  6. Leave Timing Settings to
    1. 1 Sample (On Demand)
  7. Do not Invert Line
  8. Press OK


Figure 6: DAQ Assistance Resistance Configuration

*Note that sample time is set by the Wait VI and is set to sample 10 times per second (every 100ms) in this VI

 

Tips and Tricks

  • Use this document in conjunction with the Measure Temperature using 10kΩ Thermistor, NI myDAQ, and LabVIEW document to display a light when a certain temperature threshold is met.  You can use a comparison to output a true or false if a value is greater than another number.
  • Expand your application by adding more than one channel to your DAQ Assistant and turn on multiple LEDs in with your VI in LabVIEW.
  • Further expand your application by integrating this logic to control a 7-segment LED to display integers.

 

Related Links

» Basic 5mm LED Datasheet

» NI myDAQ Getting Started and Support

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors