Sunday, June 16, 2013

USB Port Voltage and Current Tester

Project Background: We have USB ports everywhere - PCs, Tablets, Mobiles and consumer electronics items. We know USB port gives 5V; how do know it is exactly 5V, how much current is drawn from the port by the connected device and is the current capacity of the port is as per the USB specification. Measuring for the above calls for using multi-meter(s), breaking the wire for sensing current etc. This makes the whole process error prone and time consuming so I thought of designing a small testing equipment.

Project objective: This project aims to make user friendly testing equipment with below capabilities.
  1. Simultaneously check 2 USB ports (2 channels)
  2. Check Vbus voltage is within specification in no load (without connecting any device)
  3. Connect a USB device to an USB port and check whether USB device is using current as per specification and also check whether the Vbus voltage remains within specification
  4. Without a USB device, load the USB port with dummy load at various current levels and check whether the port is giving the maximum current as per specification.
  5. Have a Real time clock to time stamp the readings
  6. Local display as well as monitoring the testing results from monitoring computer
  7. Additionally measure the test environment temperature & humidity
USB Port details: USB Port was originally conceived & developed by Intel (the CPU manufacturer) and subsequently the development of this standard was taken over by a non-profit organization called USB Implementers Forum, Inc (www.usg.org). 

USB port was designed to easily connect various devices to computer with just 4 wires. It transfers data serially between the computer and the attached devices. The signal transmission at electrical level is basically by means LVDS (Low Voltage Differential Signaling).
                                         
The designers of USB port have thought of powering a device from USB port itself. For that purpose one of the carries the voltage & it is called Vbus Pin. The USB 1.x and 2.0 specifications gives a 5 V supply on a Vbus pin from which a connected USB devices can draw power. To ensure interoperability, the specification demands the designers to ensure this voltage is maintained between 4.75V to 5.25 V (5 V±5%) across Vbus and GND pins.

Further as per USB specification, the current that can be drawn from USB port Vbus pin is defined in terms of Unit. 100 mA is defined as one Unit in USB 2.0, and 150 mA is defined as one Unit in USB 3.0 Specification. In USB 2.0, port can give a maximum of 5 Unit loads (500 mA) and in USB 3.0 it can be 6 Unit loads (900 mA). Only within this current range, the voltage of 5 V±5% is guaranteed as per specification.

From USB device point of view, two types of devices are defined: they are low power and high power. A low power device can draw a maximum of 1 unit load and a high power device can draw the maximum number of unit loads as allowed by the USB standard. It is also mentioned in the USB standard that all devices should first start as low power device upon connection to USB port and then switch to high power load as required. This is to ensure 5V bus in host is not unduly loaded simultaneously with multiple high power USB devices.

It is clear from the above explanation that USB port follows a tight rule both from Host side (from where USB port originates) and also from device side. These norms ensure USB devices of different types from different manufactures works fine with different USB Host ports. The norms from Voltage and current can be summarized as below.
  • USB Host which provides the USB port should give specified voltage at Vbus pin and specified tolerance should be maintained.
  • USB port current capability should be as per standard and Voltage tolerance also should be maintained in the load range.
  • From protection point of view, USB port may have some mechanism so that if a connected device draws too much of load, the port is protected from damage.
  • At the time of connection all USB device should start in low power mode and then later become high power device if required

Project Block Diagram: Below picture shows the block diagram of the project.
The project consists of major sections as mentioned below.
  1. Microcontroller Section
  2. Power supply section
  3. Voltage & current sensing and signal condition section
  4. DC load section
  5. Input & output section
  6. Other support components
Major reasons for selecting this microcontroller are
  • Program Memory Type - Flash
  • Program Memory (KB) - 128
  • CPU Speed (MIPS) - 12
  • RAM Bytes -3,800
  • Digital Communication Peripherals - 2-A/E/USART, 2-MSSP(SPI/I2C)
  • Capture/Compare/PWM Peripherals -7 CCP, 3 ECCP
  • Timers - 4 x 8-bit, 4 x 16-bit
  • ADC - 10 ch, 12-bit
  • Comparators - 3
  • USB (ch, speed, compliance) - 1, Full Speed, USB 2.0
  • Temperature Range (C) -40 to 85 deg
  • Operating Voltage Range (V) - 2 to 3.6V DC
  • Pin Count - 28 PDIP
  • RTC - Built in
  • Programming - ICSP, Through boot loader
  • Compiler Support - C18 C compiler with libraries support
  • Development Environment - Microchip MPLAB IDE
This Microcontroller with the firmware program developed in C18 C language will do the below activities.
  • Microcontroller will run at 8Mhz clock speed using it’s own internal Oscillator
  • Will interact with monitoring PC through USB interface through D+ & D- lines (Pin 16 & Pin 17) 
  • ADC will be configured for 12bit A to D conversion and 4 channels will be used for measuring 2 USB port’s Voltage & current.
  • RTC will be run to maintain date & time using a 32.768Khz crystal
  • Through SPI interface, Digital to Analog IC will generate control voltage for DC dummy load. This dummy load will simulate various load conditions on USB ports
  • SPI interface will also connect to Serial to Parallel shift register 74HC595 to connect to a 2x16 LCD display for local monitoring
  • Ports PGD and PGC will be brought to a header for In circuit programming to update latest firmware
Power Supply Section: This equipment needs a power supply of 3.3V DC. It accepts 9 to 20V AC/DC at the DC Jack. It is rectified by a bridge rectifier and filtered through a combination of 1000uF capacitor. The filtered voltage is fed to a Voltage regulator circuit.

Voltage regulator is the adjustable type fixed type LM117 3.3V & LM7805 5V. Capacitor C3 & C23 (0.1uF) at the output voltage regulator improves the filtering and avoids oscillation at regulator. Thus the regulated 3.3V DC & 5V DC fed to all other circuits.

Voltage & current sensing and signal condition section: This is the critical section of the equipment. The design of this section should be such way it should not introduce additional error in the circuit and also it should be able measure small changes in the measured current/voltage. The circuit measures both Current & Voltage; from complexity point of view, current measuring is difficult when compared to voltage measuring. Below figure shows how it is achieved.

Voltage measuring is done by way of a voltage divider and the divided voltage is connected to A to D converter of Microcontroller. As the Microsoft AD converter reference voltage is set as 2.5V, the Vbus voltage of 5V is divided by resistors R5 & R6. This gives a full scale value 2.5V for 5V input. AD resolution being 12bit, the lowest measurable voltage becomes 2.5V/2^12=0.00061V which 0.61mV. Though the noise in the circuit may not yield this much of lowest value measurement, definitely measurement of 1 to 2mV change should be achievable.

The general method to measure current is to insert a known resistance to the circuit under measurement in series and measure the voltage drop across the resistor. From the measured voltage drop, the current can be easily found by Ohms law (I=V/R). Looking at the lowest value of current to be measured, highest value to be measured and the maximum drop allowed, a shunt resistance value 0.1 Ohm was chosen. To ensure accuracy, the resistance tolerance was chosen at 1%. With this resistor, the max USB spec current or 500mA & 900mA will yield a drop of 50mV & 90mV respectively. 

After getting the voltage drop, it cannot be directly fed to the AD converter because the drop is across the shunt resistor and not with reference to Ground (GND). To convert this to GND referenced voltage, various circuits we analyzed consisting of Op Amps.  Elaborate checking at various semiconductor manufacturers revealed that it may better to use dedicated IC specially built for such purpose. The chosen IC was from Linear Technology LT®6106 which is a versatile high side current sense amplifier.
                                              
The beauty of this IC is – by changing the 2 resistor values, suitable amplification of the voltage drop can be achieved. The voltage at the OUT terminal is GND referenced and it is fed to the AD converter of the Microcontroller. Inside Microcontroller firmware, suitable division and multiplication is done so that the current value is arrived.

DC Load Section: This is another difficult section in the project. This section is basically to simulate different load conditions on Vbus and to see how the voltage is maintained and whether the voltage maintained is within the specification of USB. When researching for this, various ides came up starting from the simple idea of having different load resistor and connecting the same to the load through a switch assembly (relays). After analyzing varies ideas, it was concluded to have circuit as below in Figure.
                          
The circuit is basically to use Digital to Analog converter to covert a digital value given by the Microcontroller in to an analog voltage. This is done by a dedicated DAC chip MCP4812 from Microchip. This is a dual channel 10bit DAC with inbuilt 2.5V reference voltage source. This connects to the Microcontroller through SPI interface. The converted analog voltage is then fed to an Op Amp to control MOFET. The Op Amp used is MCP617 and the MOSFET used is BUZ11 of equivalent. The current conducted by BUZ11 MOSFET is fed back to the Op Amp. Basically the MOSFET is operated in linear region as constant current source. The current is set by the voltage from DAC. So by varying the digital value sent to the DAC by Microcontroller, the current allowed by MOSFET is varied simulating different load conditions on the Vbus of USB port. Careful selection of R18, R19 and detailed processing Microcontroller firmware ensures the desired load range is achieved.

Input & Output Section: The input & output section helps to locally control the equipment. It consists of 4 push button switches and LCD Display. The buttons are named “Menu”, “Up”, “Down” and “Enter” to manually input and control the equipment. The switches are sensed by Microcontroller. The Microcontroller port pins connected to the switches are in logic level 1 through pull up resister. When the switch is pressed, the corresponding port pin is connected to GND (logic 0) this is sensed by Microcontroller firmware and appropriate action is taken inside the controller. Refer below figure.
                        
For local display, 2x16 Character LCD compatible to Hitachi HD44870 which is very famous among designers is used. This LCD connects to the Microcontroller through its Data pins (D4 to D7) and 2 control lines name RS, & E. Currently there is difficulty in the number of free ports available in the Microcontroller to operate this LCD so a small revision in the circuit is being carried out to control the LCD through a Serial to Parallel shift register.


Other support components: Other support components consists of power supply filter capacitors, crystal, various connectors, optional Temperature / Humidity sensor DHT11, ICSP header for in-circuit programming etc.

Development, Programming, Testing and Equipment build up: During the project design, testing was taken up during the design itself stage by stage. Most of the testing on hardware circuit design and firmware was done in the bread board itself. This was done so that multiple iterations were not required for PCB design. PCB fabrication being a costly and time consuming activity, this approach helped to save time and money.

To simplify the testing of the hardware and firmware, it was decided to test small small section of the hardware and firmware code as the design was progressing. Below are list individual blocks that was identified and tested along the project development.
  1. Power ON the Microcontroller with basic wiring of power, MCLR, ICSP and USB host connection.
  2. Program Microcontroller with slightly modified version of boot loader with the help of external programmer ICD2.
  3. Reaching this stage of Microcontroller operation ensured further programming of Microcontroller firmware does not need the external ICD2 programmer. The programming can be done using USB port itself with the help of boot loader. The boot loader can be activated by keeping RB2 at logic 0 at the time of power ON or reset of Microcontroller.
  4. Firmware developed first to communicate with PIC and to decode simple commands like, getting firmware version string, setting a port value etc. At this stage, communication with Microcontroller from PC was done through a terminal emulator program.
  5. SPI interface related code was written so that Microcontroller was able to communicate with DAC IC MCP4812 over SPI interface. Testing of controlling each channel, switching a channel OFF, changing output value etc were done. The output Voltage value of DAC was cross verified with a multimeter.
  6. LCD related code was written and sample output was checked at LCD. .canon Initialize ports RB1, RB2, RB3 & RB5 as input
  7. RTC module was initialized and code was written to set the time from PC interface and the same was displayed in LCD. Picture taken at this point of time is shown below for reference.
  8. ADC module was initialised and from PC command was sent to read a particular channel. Dummy inputs were connected to the input channels and output reading was verified for the given input.
  9. Code was written to read input push buttons Read the push button status in process loop.
  10. USB SOF packet handler interrupt was used which happens every 1ms to de-bounce push button for around 100ms
  11. Depending on which button s pressed, cycle through menu and sub menu routines for appropriate user control / input.
  12. If more than a 1 to 2 second of inactivity found at push button, leave menu/sub menu routines and come to normal

With all the testing done & hardware and firmware verified, PCB design was finalized for sending it to PCB fabricator. It is worthwhile to mention two points at this junction that is USB current sending & actual DC load simulation was done at bread board level. This is because of the complexity involved in using SMD and big through hole components involved in these sections.

Once the PCB was received from PCB fabricator; it was checked for any defect like any electrical continuity or short circuit problem. In fact, a hole for LCD mounting was not drilled and the same was done using hand drilling machine. Soldering of components were done.

Finally the board was powered up and connected to the PC for remaining program code verification and development. The sections USB voltage & current measurement and DC Load were programmed and finally verified. Measured values were verified with the help of Agilent multimeter for accuracy.

Photos showing various stages of development:
Bread board
Final PCB


Assembly pictures


Complete schematic diagram: Will upload later

Firmware: Needs lot of clean-up, will give link later

PC side control software: Work under progress...... 

Summary: Prototype developed and basic working measurement demonstrated. Most of the project objectives were met. Below improvements needs to be taken care in Revison 2 of the design.
  1. Power supply section needs to be reviewed so that 5V rail has 5V whether is fed by USB or external adopter. In the current design, when powered by USB, we can not get 5V
  2. DC load section does not load more than 400 to 450mA even when the D/A converter gives the maximum output. This section needs to redesigned so that upto 1000mA to 1200mA loading is possible.
  3. PCB design review - traces close to mounting holes, trace lenght in few areas
  4. Firmware clean up
  5. PC side software completion
Further development: Though the equipment was designed to specially test the USB port which is having the low voltage of 5V and low current of less than 1A, the same concept can be used variety of other ways. This equipment can further be improved by way of design & firmware modification to do the below things:
  1. With the help of few additional adapter cables, this can be used to study charging characteristics of mobile phones, tablets etc which are chargeable through USB port
  2. By modifying the current carrying capacity of the dummy load, current sensing resister and high side current sensing amplifier, this equipment can be made to study the charging and discharging characteristics of various batteries.
  3. With the above it can be even used for calibrate notebook batteries which calls for complete discharge and charge cycles multiple times with a predefined current settings
  4. With suitable modification in current carrying capacity of the dummy load, current sensing resistor, high side current sensing amplifier, voltage divider network for voltage sensing and the firmware, this equipment can be used for testing small solar panels etc
  5. With suitable modification in current carrying capacity of the dummy load, current sensing resistor, high side current sensing amplifier, voltage divider network for voltage sensing and the firmware, this equipment can be used for testing small DC power supplies.

No comments:

Post a Comment