Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

Python for Windows program returns incorrect value.

Expand / Collapse

Python for Windows program returns incorrect value.

Recently we had a customer that was having a problem with the and examples. They attached a single-ended 5 volt signal to channel 0 on a USB-1408FS-PLUS. The example program returned an incorrect value of ~3.6 volts. The customer verified that InstaCal returned the correct result. So what gives?

The reason why the and programs are not reading the input correctly, is because the inputs are set to differential mode, which means one side of the input was left open. How could this be? It's because the example uses the discovery interface to connect to the device, which ignores how you have it set in InstaCal. Therefore, when the board was discovered by default it was in differential mode. 
Below is the modified example that sets the inputs to single-ended. Also added was the ULRange enum to specify a voltage range. Below is the program listing and the changes have been highlighted.

Library Call Demonstrated:  mcculw.ul.a_in() or mcculw.ul.a_in_32()
Purpose:                    Reads an A/D Input Channel.
Demonstration:              Displays the analog input on a user-specified
Other Library Calls:        mcculw.ul.to_eng_units()
                                or mcculw.ul.to_eng_units_32()
Special Requirements:       Device must have an A/D converter.
                            Analog signal on an input channel.
from __future__ import absolute_import, division, print_function
from builtins import *  # @UnusedWildImport
from mcculw import ul
from mcculw.device_info import DaqDeviceInfo
# added the following imports
from mcculw.enums import ULRange
from mcculw.enums import AnalogInputMode
from mcculw.ul import ULError, a_input_mode
    from console_examples_util import config_first_detected_device
except ImportError:
    from .console_examples_util import config_first_detected_device
def run_example():
    # By default, the example detects and displays all available devices and
    # selects the first device listed. Use the dev_id_list variable to filter
    # detected devices by device ID (see UL documentation for device IDs).
    # If use_device_detection is set to False, the board_num variable needs to
    # match the desired board number configured with Instacal.
    use_device_detection = True
    dev_id_list = []
    board_num = 0
        if use_device_detection:
            config_first_detected_device(board_num, dev_id_list)
        daq_dev_info = DaqDeviceInfo(board_num)
        if not daq_dev_info.supports_analog_input:
            raise Exception('Error: The DAQ device does not support '
                            'analog input')
        print('\nActive DAQ device: ', daq_dev_info.product_name, ' (',
              daq_dev_info.unique_id, ')\n', sep='')
        ai_info = daq_dev_info.get_ai_info()
        ai_range = ai_info.supported_ranges[0]
        channel = 0
        # ********* add this function
        a_input_mode(board_num, AnalogInputMode.SINGLE_ENDED)
        # Get a value from the device
        if ai_info.resolution <= 16:
            # Use the a_in method for devices with a resolution <= 16
            # *************** added ULRange.BIP10VOLTS **********
            value = ul.a_in(board_num, channel, ULRange.BIP10VOLTS)
            # Convert the raw value to engineering units
            # *************** added ULRange.BIP10VOLTS **********
            eng_units_value = ul.to_eng_units(board_num, ULRange.BIP10VOLTS, value)
            # Use the a_in_32 method for devices with a resolution > 16
            # (optional parameter omitted)
            value = ul.a_in_32(board_num, channel, ai_range)
            # Convert the raw value to engineering units
            eng_units_value = ul.to_eng_units_32(board_num, ai_range, value)
        # Display the raw value
        print('Raw Value:', value)
        # Display the engineering value
        print('Engineering Value: {:.3f}'.format(eng_units_value))
    except Exception as e:
        print('\n', e)
        if use_device_detection:
if __name__ == '__main__':

Rate this Article:

Add Your Comments

For comments email

Article ID: 50842

Last Modified:5/26/2021 3:03:30 PM

Article has been viewed 91 times.