USB Interface Using Python Software

By A. Robson Benjamin


Circuit and working

Circuit diagram of the USB interface using Python and transistor curve tracer is shown in Fig. 2. It is built around PIC18F4550 microcontroller, MAX5154 12-bit digital-to-analogue converter, voltage regulator AD780, voltage amplifier LM358, current-to-voltage converter OP741, power transistor SL100/CL100, transistor under study BC547 (T1) and other essential components needed for USB configuration.

Fig. 2: Circuit diagram of the USB interface and transistor curve tracer

The microcontroller has an on-chip USB transceiver, which is connected to the host PC through a USB cable. Clock frequency required for full-speed USB operation is derived from external 20MHz crystal. A pull-up resistor internal to the microcontroller configures the USB device as a full-speed device. In this design, power required for PIC18F4550 operation is drawn from the bus.

Base current of the transistor under study is varied from 0µA to175µA in eight steps using port D pins (D0, D1 and D2). Collector voltage is varied from 0V to 2.5V in eight steps to the digital-to-analogue converter from port B.

Output voltage from the digital-to-analogue converter is subsequently amplified about four times using LM358 and given to the base of the power transistor. Emitter of power transistor SL100 is connected to the collector of the transistor whose voltage is being varied.

Collector voltage is given to channel 0 of the built-in analogue-to-digital converter after dividing the voltage exactly by half using a potential divider arrangement to meet the voltage limitation of the microcontroller. Assuming that collector current is equal to emitter current, output is taken across the one-ohm resistance, and drop across one-ohm resistance is amplified about 50 times and given to channel 1 of the built-in analogue-to-digital converter.

The author’s prototype is shown in Fig. 3.

Fig. 3: Author’s prototype


An actual-size, single-side PCB layout for the USB interface is shown in Fig. 4 and its component layout in Fig. 5. After assembling the circuit on the PCB, enclose it in a suitable box.

Fig. 4: Actual-size PCB layout of the USB interface using Python software
Fig. 5: Component layout of the PCB

Download PCB and Component layout PDFs: Click here


All USB devices handle a standard set of requests, described in the USB specification. It is implemented using pic18_usb header file provided in CCS C compiler. The compiler provides the basic framework for enumeration. During enumeration, the host requests data structures called descriptors from the device. These descriptors contain information about the USB device and type of communication. If you want to design your own USB device, be aware of the endpoint details, vender IDs and product IDs. This information is necessary for device enumeration and is given in usb_desc_cdc1.h file. The firmware, in addition to USB requirement, implements the following tasks:
• Generates different base voltages
• Generates different emitter currents
• Measures collector voltages
• Sends data to PC

The USB host communicates with the USB peripheral through the device driver. The device driver is a software component that enables an application to access a hardware device. USB device drivers for Windows must conform to Win32 driver model.

Windows includes application programmer’s interface functions that enable applications to communicate with device drivers. The device driver is generated using Library USB Wizard, and the device is installed in the host computer. Libusb-win32 is a port of the USB library for Windows operating system. The library allows user space applications to access any USB device on Windows in a generic way without writing any line of kernel driver code.

Hardware installation procedure

Step 1.

After fusing the program (hex file) into the microcontroller, connect the board to the PC. As soon as the two are connected, a pop-up screen appears, as shown in Fig. 6.

Fig. 6: Hardware Wizard screen

Step 2.

Use inf- wizard found in libusb-win32-device-bin- folder to generate the driver for the attached device. If the driver is installed properly, the attached device will appear in Device Manager, as shown in Fig. 7.

Fig. 7: Device Manager window

Step 3.

Basic functions including USB configuration, setting port B bits and reading analogue voltages can be tested using default interactive Python window. Using set_portb_output (192) command, connected LEDs (LED1 and LED2) can be lit.

Similarly, analogue voltages can be measured by connecting the terminal (AN0) to 5V. The value corresponding to 5V is 255. It is better to test the basic function using interactive Python window (Fig. 8) before testing the actual board.

Fig. 8: Interactive Python shell

Step 4.

Open file from PythonWin 2.5.2 Interactive Window and run the script as shown in Fig. 9. The program output pattern is displayed using Tkinter software, which is shown in Fig. 10.

Fig. 9: Opening file and running it using pywin32
Fig. 10: Program output of IV characteristic curve of transistor
Fig. 10: Program output of IV characteristic curve of transistor

The USB based data-acquisition system using Python interface and open source general-purpose device driver reduces the complexity involved in USB connectivity. Interactive nature of Python software makes USB connectivity more user-friendly as compared to other visual languages.

Download Source File: Click here

A. Robson Benjamin 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


Please enter your comment!
Please enter your name here