Saturday, December 9, 2023

USB Interface Using Python Software

By A. Robson Benjamin. He is associate professor of Physics at American College, Madurai, Tamil Nadu. His interests include design and development of USB based data acquisition and automation of electronic gadgets

- Advertisement -

This is a simple transistor’s output characteristic curve-tracer program through PIC microcontroller & USB interface using Python programming language. Output characteristic curve of a transistor is automated using Python software and USB interface. The application software for collecting the measured value and displaying it on the monitor screen is developed using Python.

Types of USB

In recent PCs, legacy ports such as parallel port, serial port and so on have been replaced with USB. A USB’s plug-and-play feature can be used to automate laboratory instruments/data-acquisition systems. To meet the needs of various applications using USB interface, three speeds of operation have been designed in USB V2.0 specification, namely, low speed (1.5Mbps), full speed (12Mbps) and high speed (480Mbps).

Recently, USB3.0 specification has introduced super speed with 5Gbps.The physical USB connection uses four wires. Two wires are used to carry the differential signal (D+ and D-), and the other two are for power and ground. To transfer data between the host PC and the device USB V2.0 supports four types of data transfers, namely, control, bulk, interrupt and isochronous.

- Advertisement -

Control transfer is mainly used to configure a device when it is first connected to the PC. The process of configuration is known as enumeration. It is defined as the initial exchange of information by which the host learns about the device and assigns an appropriate device driver.

parts list

Bulk data transfers are used when data is generated or consumed in relatively large quantities.

Interrupt data transfers are used for timely but reliable delivery of data.
Isochronous data transfers occupy a pre-negotiated amount of USB bandwidth with pre-negotiated delivery latency (also called streaming real-time transfers).
One of the major barriers to designers of USB peripherals is developing device drivers for custom-built USB devices. This has been removed with the use of LibUSB-Win32, which is an open source driver.

Host software

USB supports master-slave configuration. Master is always the PC. Software that resides and controls the communication flow in the PC is known as host software. In this article, the host software used to communicate with the USB mass storage class/communication device class is Python.

Python is an interpreted language, which can save considerable time during program development because no compilation and linking are necessary. It is a popular programming language used for both standalone programs and scripting applications. It is free, portable, powerful and remarkably easy to use.

The interpreter can be used interactively, which makes it easy to experiment with features of the language. Programs written in Python are typically much shorter than equivalent C or C++ programs. There are two ways to use the interpreters, namely, interactive mode and script mode. In interactive mode, the interpreter prints the result as the program is typed.

The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready. Alternatively, you can store the code in a file and use the interpreter to execute the contents of the file, which is called a script.

Software required

The required software include Python 2.5 (python-2.5.2) or higher IDE, Win32 Python (pywin32-210.win32-py2.5), Matplot Library (matplotlib-0.91.2.win32-Py2.5), Numeric Python (numpy-1.0.4.win32-py2.5) and PyUSB-1.0.0a2.

PyUSB-1.0.0a2 is a Python library allowing easy USB access. PyUSB-1.0.0a2 version is written in Python, which allows Python programmers with no background in C to understand how PyUSB works. PyUSB can run on any platform with Python 2.4 and later version. Communicating with a USB device has never been so easy. USB is a complex protocol, but PyUSB has all the necessary functions needed to configure a USB-supported device. PyUSB modules have two sub-modules: usb.core as the main module and usb.util containing utility functions.

PyUSB 1.0 tutorial gives the basic information about the complex USB protocol. If you want to know all information related to USB, refer the books USB Design by Example by John Hyde and USB Complete by Jan Axelson.

Functions needed for USB configuration and data acquisition are incorporated in, namely, __init__(self,val1,val2), set_portb_output(self,val), set_portd_output(self,val), read_analog_0(self), read_analog_1(self) and set_voltage12(self, val).
Details are as follows:
• __init__(self,val1,val2) takes care of USB communication.
• set_portb_output(self,val) helps set port B values from 0 to 255.
• set_portd_output(self,val) helps set port D values from 0 to 255.
• read_analog_0(self) helps read analogue values from channel 0.
• read_analog_1(self) helps read analogue values from channel 1.
• set_voltage12(self, val) helps send a 12-bit value to DAC MAX5154.

Software installation

Install Python2.5.2 IDE or higher in Windows 7 operating system. Then, install the supporting tools one by one. Default folder is C folder (C:Python25). Extract PyUSB-1.0.0a2 zip file to Python25 folder. Now, using Command Prompt, go to PyUSB folder, which is inside Python25 folder, and install PyUSB using the following command (also shown in Fig. 1):

>python install

Having installed the necessary software, place and files inside Python25 folder.

Fig. 1: Command Prompt screenshot
Fig. 1: Command Prompt screenshot



Electronics News

Truly Innovative Tech

MOst Popular Videos

Electronics Components