Academic Company Events NI Developer Zone Support Solutions Products & Services Contact NI MyNI

Using Counters and Digital I/O

Digital input and output are the foundation of computer technology. They are able to communicate a True or False or 1’s and 0’s. Digital outputs are often used to indicate if a threshold has been passed or to apply power to a circuit. Digital inputs are often used to trigger the acquisition of signals. Counters provide the functionality of outputting a square wave pulse train as well as count digital edges. This module introduces digital and counter I/O concepts using NI LabVIEW software.


Step-by-Step Procedure

Download the Step-by-Step Procedure PDF

The following procedure outlines how to count the number of digital pulses received on the counter pin of an NI Educational Laboratory Virtual Instrumentation Suite (NI ELVIS)  II device, and then how to output the digital value to the digital port of the device in binary format. This procedure requires a single-pole double-throw (SPDT) switch as well as eight 330 Ω resistors and eight LEDs. 

Note: If you have a jumper wire connected to your counter input pins that is toggled between DGND, the +5 V rail achieves the same results as an SPDT switch.

  1. Connect the USB DAQ device to your PC
    1. You can locate the device pinouts for your DAQ device by searching for your device online at ni.com
    2. The user guide and specifications contain this diagram
    3. Right-click your device in NI Measurement & Automation Explorer (MAX) and select Device Pinouts
  2. Configure the SPDT switch to output 5 V pulses
    1. Wire one throw to the +5 V terminal of your device
    2. Wire the second throw to the DGND terminal of your device
    3. Wire the output pole to PFI8 (ctr0 SOURCE) of your device
  3. Connect the resistor and LED indicator network – using a breadboard is recommended but not required
    1. Connect eight LEDs and a 330 Ω resistor in series – the resistor is used to regulate the current flow through the LED to prevent it from overheating
    2. Connect the other terminal of the LED to one of the port0/line0 terminals on your device
    3. Connect the other terminal of the resistor to the DGND of your device
    4. See the figure below for proper configuration of one LED
      /images/labview101/led_setup.gif
    5. Repeat for the remaining lines 1-7 of port0 and LEDs

Note: LEDs are diodes, so they allow current to flow in only one direction. If an LED does not light up when the 5 V voltage is applied, reverse the direction/polarity of the LED because this is most likely happening because the LED is reversed. If it still does not light up, the LED may no longer be working.

  1. Create a new VI in LabVIEW
    1. Open LabVIEW and select File»New VI
  2. Place a counter input DAQ Assistant on the block diagram
    1. Right-click on the block diagram and select Express»Input»DAQ Assistant
    2. Place the DAQ Assistant on the block diagram by left-clicking
  3. Configure DAQ Assistant type
    1. Select Acquire Signals
    2. Select Counter Input
    3. Select Edge Count
    4. Select Dev1 (or the name of your device if not Dev1)
    5. Select ctr0 for counter input channel 0
  4. Configure acquisition settings
    1. Ensure 1 Sample (On Demand) is selected for Acquisition Mode
    2. Select OK to apply configuration
    3. Select Yes when prompted to automatically create a While Loop
  5. Right-click the Data output terminal and select Create»Numeric Indicator
  6. Run the VI and toggle the SPDT switch
    1. Notice the edges being counted on the front panel indicator
  7. Place a digital output DAQ Assistant on the block diagram
    1. Right-click on the block diagram and select Express»Output»DAQ Assistant
    2. Place the DAQ Assistant on the block diagram to the right of the counter input DAQ Assistant by left-clicking
  8. Configure DAQ Assistant type
    1. Select Generate Signals
    2. Select Digital Input
    3. Select Port Output
    4. Select Dev1 (or the name of your device if not Dev1)
    5. Select port0 for port output channel 0
  9. Configure acquisition settings
    1. Ensure 1 Sample (On Demand) is selected for Acquisition Mode
    2. Select OK to apply configuration
  10. Wire the output from the counter input DAQ Assistant Data output terminal to the Data input terminal of the digital output DAQ Assistant
    1. Notice that a Convert from Dynamic Data VI is automatically inserted
    2. This is because the counter input’s data type is dynamic, and the digital output requires a 1D array of numeric
  11. Wire the Stopped output from the counter input DAQ Assistant to the Stop (T) input of the digital output DAQ Assistant
    1. This stops the digital generation if the Stop button is pressed
  12. Create the stop condition to include when the Stop button is pressed or an error is detected
    1. Unwire the Conditional Terminal from the Stopped output terminal of the counter input DAQ Assistant
    2. Right-click the block diagram and select Programming»Boolean»Or
    3. Wire the output from the Stop button control to the bottom input of the Or Boolean function
    4. Right-click the block diagram and select Programming»Cluster, Class, & Variant»Unbundle by Name and place this to the right of the digital output DAQ Assistant
    5. Wire the Error Out output terminal of the digital output DAQ Assistant to the input of the Unbundle by Name function and ensure Status is selected to be unbundled
    6. Wire the output of the Unbundle by Name function to the top input of the Or function
    7. Wire the output of the Or function to the input of the Conditional Terminal
  13. Run the VI and observe the results
    1. The Edge Count indicator should match the binary output of the LEDs

You can convert your DAQ Assistant Express VIs into low-level NI-DAQmx functions by right-clicking the DAQ Assistant and selecting Generate NI-DAQmx Code. The low-level NI-DAQmx API exposes more functionality and customization options for programming.

LabVIEW Example Code

Right-click on the above VI snippet and select Save Image As... Locate the file on your hard disk and click and drag the file icon onto your LabVIEW block diagram. LabVIEW automatically generates the code from the VI snippet. For more information, read VI Snippets.

For a low-level NI-DAQmx driver example for this concept, open the NI Example Finder and browse to Hardware Input and Output»DAQmx»Counter Measurements»Count Digital Events»Count Digital Events.vi to count edges, and Hardware Input and Output»DAQmx»Digital Generation»Cont Write Dig Port-Int Clk.vi to generate digital signals in the NI Example Finder. View detailed help on using the NI Example Finder and help within LabVIEW.

Don’t have NI-DAQmx installed on your PC?
Download the latest version of NI-DAQmx.

Module Exercise: Using Counters and Digital I/O

Download the Module Exercise PDF

Exercise A: Using Counters and Digital I/O

Exercise Goal
Use your NI myDAQ device to continuously count the number of digital edges received on PFI0 and display the value as a numeric and a digital Boolean array in LabVIEW.

Required Hardware
1- NI myDAQ
1- 3.5 mm audio cable with MP3 or CD player
or
1- computer microphone

Things to Consider

  • What is the audible frequency range of the human ear? Then what should the sampling rate be, according to the Nyquist Sampling Theorem, to generate all of the frequencies? What is the sampling frequency of a CD player? Therefore, what should you use for the sampling rate of your NI myDAQ device?
  • Samples to Write should be 1/5 to 1/10 of your sampling rate. For example, Fs=10 kHz, Samples to Write = 1k or 2k samples. This is a good benchmarking relationship for buffered generation.
  • Use a voltage generation to read output to speakers or headphones on audioOutputLeft and audioOutputRight for a stereo output, or just one channel for a mono output.
  • The output range for the audio output is ±2 V.
  • Display the signal to a graph before outputting to the NI myDAQ device.
  • Use a Merge Signals function to create two audio channels for both right and left.
  • Create a control for the Frequency to adjust this value on the fly. Should this control be placed inside or outside the While Loop if you want to update while running the VI?
  • Vary the frequency of the generated signal to hear the change in pitch.

Here is an example of what the final signal should look like on a graph indicator:

View the exercise solution (Don’t cheat!).

Exercise B: Using Counters and Digital I/O

Exercise Goal
Use your NI myDAQ device to continuously output a number in binary format on the digital port using LEDs with resistors.

Required Hardware
1- NI myDAQ
8 - LEDs
8 - 330 Ω resistors
16 - pieces of wire
1- breadboard (optional)

Things to Consider

  • Use a digital output in port format to output the value.
  • Use the Build Array function to convert the single scalar to an array inputting to the DAQ Assistant.
  • Connect a resistor and LED in series for each digital line to limit the current to each LED.

View the exercise solution (Don’t cheat!).

Cumulative Exam:
LabVIEW and DAQ

Evaluate your understanding of LabVIEW and the basics of DAQ. This exam is recommended after you complete all of the modules for LabVIEW Basic Concepts and LabVIEW Basic Tasks. There are 26 multiple choice questions on the exam.

Take the exam


Cumulative Project: LabVIEW and DAQ

Assess your working knowledge of building a complete system by acquiring, processing, and displaying data with LabVIEW and a DAQ device. This project is recommended after you complete all of the modules for LabVIEW Basic Concepts and LabVIEW Basic Tasks.

Start the project