DIY USB Current Meter (1): Component Selection and Schematic Design
USB current meters are quite commonly used in electronic DIY projects. They can be used to observe the real-time current, voltage, and power consumption of a target device.
As an electronic DIY enthusiast, making your own USB current meter is a natural step. Here, I will add some detailed explanations to the schematic of an open-source USB current meter. This will help beginners understand how to draw a schematic for a simple device.
Of course, I am still a beginner myself, so if there are any mistakes or shortcomings in this article, please feel free to provide feedback.
Finished USB Current Meter
Voltage, Current, Power, Capacity Interface
Power History Chart Interface
Requirements Analysis and Component Selection
When starting with an entry-level DIY product, it’s best not to make it too complicated. Start simple, define the scope of requirements first, and reduce the overall development difficulty. This not only decreases the workload of drawing schematics but also increases the success rate, preventing the project from being abandoned halfway. 🤣
Requirements Analysis
As a USB current meter, the primary functions are to collect metrics like current and voltage. It should also support the widely adopted USB Type-C interface and USB PD (Power Delivery) charging. Then, the collected data should be displayed on a screen.
Therefore, based on the requirements, the following features are roughly determined:
- USB Type-C input and output
- PD protocol passthrough
- Support for voltage detection from 5V to 20V
- Support for current detection from 0A to 5A
- Support for power calculation and capacity statistics
- Support for power history chart display
- Support for recording and clearing statistical data
- Button to switch between voltage/current data and power history chart
- Use a 12864 OLED to display relevant data
MCU Selection
In previous DIY projects, I’ve always used the ESP32 series MCUs. However, for a simple product like a USB current meter, which doesn’t need Wi-Fi or Bluetooth data transmission, we need to choose a different MCU.
While chatting in a DIY group, I saw a recommendation for the low-power RISC-V MCU CH32V003 from WCH. Let’s give it a try.
CH32V003 website: https://www.wch-ic.com/products/CH32V003.html
CH32V003 series is based on QingKe RISC-V2A core design of industrial-grade general-purpose microcontroller, support 48MHz system main frequency, with wide voltage, 1-wire serial debug interface, low-power consumption, ultra-small package, etc. CH32V003 series built-in a group of DMA controller, a group of 10-bit ADC, a group of op-amp comparators, multiple timers and standard communication interfaces USART, I2C, SPI, etc.
CH32V003 Features
- QingKe 32-bit RISC-V2A processor, supporting 2 levels of interrupt nesting
- Maximum 48MHz system main frequency
- 2KB SRAM, 16KB Flash
- Power supply voltage: 3.3/5V
- Multiple low-power modes: Sleep, Standby
- Power on/off reset, programmable voltage detector
- 1 group of 1-channel general-purpose DMA controller
- 1 group of op-amp comparator
- 1 group of 10-bit ADC
- 1×16-bit advanced-control timer, 1×16-bit general-purpose timer
- 2 WDOG, 1×32-bit SysTick
- 1 USART interface, 1 group of I2C interface, 1 group of SPI interface
- 18 I/O ports, mapping an external interrupt
- 64-bit chip unique ID
- 1-wire serial debug interface (SDI)
- Package: TSSOP20, QFN20, SOP16, SOP8
For this project, we need an I2C interface for the 12864 OLED and INA219, regular GPIOs for buttons, UART for debugging information output, and SDIO for programming. After comparing options, the TSSOP20 package CH32V003F4P6 is more suitable. It offers more GPIOs compared to the SOP8 package CH32V003J4M6, and the UART and SDIO are not multiplexed, which better meets the needs of this project.
Voltage and Current Sensing Selection
As a USB current meter, the most important aspect is the detection of voltage and current. This can be done using operational amplifiers or by choosing an off-the-shelf chip. To keep things simple, we’ll go with an off-the-shelf chip. Since I’ve used TI’s INA219 before, we’ll choose this one again.
INA219 website: https://www.ti.com/product/INA219
The INA219 is a current shunt and power monitor with an I2C- or SMBUS-compatible interface. The device monitors both shunt voltage drop and bus supply voltage, with programmable conversion times and filtering. A programmable calibration value, combined with an internal multiplier, enables direct readouts of current in amperes. An additional multiplying register calculates power in watts. The I2C- or SMBUS-compatible interface features 16 programmable addresses.
The maximum voltage range of the INA219 is 26V, which is sufficient for 20V in PD2.0 100W applications. However, if we encounter devices that support PD3.1 140W using 28V, it would exceed the range. Currently, chargers and devices supporting 140W are quite rare, so we won’t worry about this issue for now. In the future, we could consider using the INA226, which has a higher voltage range.
DC-DC Chip Selection
Since the USB current meter in this project needs to support USB PD and may require up to 100W detection, the VBUS voltage in the USB interface can reach up to 20V. However, the CH32V003 and INA219 used in this project do not support operating at such high voltages. Therefore, we need to use a DC-DC chip to step down the VBUS voltage to 3.3V to power the MCU and INA219.
LGS5145: https://item.szlcsc.com/5718438.html
The LGS5145 is a buck DC/DC regulator with an internal switch. It features SKIP control mode, combining low quiescent current with high switching frequency to achieve high efficiency over a wide range of load currents. In SKIP mode, short “burst” periods switch the inductor current through the internal power MOSFET, followed by sleep periods during which the power switch is off and the load current is supplied by the output capacitor. At light loads, the burst period occupies only a small fraction of the total cycle time, minimizing the average power current and significantly improving efficiency at light loads.
After searching on LCSC, I chose this compact DC-DC power chip, which uses a SOT-23 package and supports a maximum input voltage of 40V. This meets the requirement for stepping down the USB PD’s maximum input voltage of 20V.
Screen Selection
For a simple USB current meter, a commonly used 12864 OLED screen is sufficient. To further reduce the difficulty of assembly, I opted for a ready-made OLED screen module, avoiding the need to solder the screen and its peripheral components.
Additionally, to simplify development and reuse the I2C communication code from open-source projects, I selected a 12864 OLED screen module with a four-pin I2C interface.
Component Summary
The final components we selected are as follows:
- MCU: WCH CH32V003F4P6
- Voltage Monitoring: TI INA219
- DC-DC Converter: LGS5145
- Screen: 0.96-inch 12864 OLED screen module
Schematic Design
USB Type-C Input and Output
To keep the current meter compact, no USB Type-A interface was added, and both input and output use USB Type-C connectors.
Since USB PD power delivery needs to be supported, the power device and charger must negotiate using CC1 or CC2 configuration channels. Therefore, both USB Type-C female connectors use the 16-pin version.
The VBUS network for the two female connectors is named differently — one is VBUS, and the other is VBUS_OUT — because a sampling resistor needs to be connected between the two female connectors, requiring different network names.
To allow the power device and charger to negotiate the USB PD protocol properly, the CC1 and CC2 pins of the input and output connectors are directly connected.
Other pins of the USB Type-C are not used in the PD protocol but are used in other protocols such as QC or Apple 2.4A, which utilize the D+ and D- pins. Therefore, to ensure compatibility, the other pins are also directly connected.
Two important points to note with this setup:
- No 5.1KΩ Pull-Down Resistors on CC1 and CC2: Since CC1 and CC2 are not connected to 5.1KΩ pull-down resistors, the current meter will not start when using a C-C cable and PD charger. This is because the charger cannot negotiate the PD protocol and therefore will not supply power.
- Direct Connection of CC1 and CC2 Without MUX Chip: Since CC1 and CC2 are directly connected without a MUX chip, the PD protocol cannot be negotiated if the charging cable is connected in reverse.
Additionally, since the USB Type-C connectors are directly connected, there is no specific input or output side. Either side can be connected to the charger.
DC-DC Step-Down Power Supply
The schematic is directly drawn according to the LGS5145 datasheet. Generally, most chips provide a typical application circuit, and following the datasheet instructions to draw the schematic usually works well.
In the schematic, resistors R5, R6, and R7 are the voltage divider feedback resistors. From the datasheet, we can find the formula for calculating the voltage divider resistors under the section “Setting Output Voltage”:
Based on the formula, the actual output voltage can be calculated as Vout = 0.812 * (40K / 10K) = 3.428V
. This is slightly higher but still within the allowable input voltage range for the MCU and INA219, so it's not a significant issue. These resistor values were chosen mainly because I had 10K and 20K resistors on hand and didn't want to purchase additional resistors. If you don't have similar reuse needs, you can directly purchase resistors as suggested by the datasheet.
C6 is a feedforward compensation capacitor, typically used to improve the response speed of the DC-DC converter when the output current changes. It can be omitted if not necessary.
The final output network of the DC-DC converter is labeled as V3.3.
Additionally, to indicate whether the DC-DC converter is functioning correctly, an LED has been added to this section to indicate the power status, along with a 10K current-limiting resistor.
MCU Schematic
For the CH32V003, it is powered by 3.3V and includes a 100nF decoupling capacitor. The I2C bus has 10K pull-up resistors.
The network usage and descriptions are as follows:
- TXD, RXD for Debugging: These pins are used for UART communication during debugging.
- NRST, SWDIO for Programming: These pins are used for programming the MCU.
- GPIO for Button Detection (BTN1, BTN2): These GPIO pins are used to detect button presses.
- I2C Bus (I2C_SDA, I2C_SCL): According to the datasheet, when using I2C with the CH32V003, pull-up resistors should be added to the SCL and SDA lines. Here, 10K pull-up resistors are used.
- SPI Bus (SPI_MISO, SPI_MOSI, SPI_SCK): These pins are for SPI communication, though SPI is not used in this project.
INA219 Voltage and Current Measurement
The INA219 does not require many external components. You only need to add a decoupling capacitor for the power supply and connect VIN+ and VIN- to the two ends of the shunt resistor.
- A0 and A1 Pins: These pins determine the I2C address used for communication between the MCU and the INA219. Here, both A0 and A1 are grounded, so the INA219 will use 0x40 as the I2C address.
- R2 as the Shunt Resistor: The INA219 measures the voltage difference across the shunt resistor and uses Ohm’s law to calculate the current flowing through the circuit.
For this USB current meter project, to measure higher currents and avoid excessive heating, a 10mΩ shunt resistor is used. This ensures that even at 100W (20V at 5A), the power dissipation is only 0.25W, and in practical tests, it does not overheat even during prolonged 100W operation.
- SDA and SCL Pins: These pins connect to the I2C_SDA and I2C_SCL network for communication between the MCU and the INA219.
Screen and Buttons
Screen
This USB current meter uses a 0.96-inch 12864 OLED screen as the display device. To simplify the assembly, a ready-made module is used. You only need to place a 4-pin 2.54mm pin header. To position the screen correctly, an additional symbol for the 12864 screen module is included, which helps determine the pin header position during PCB design.
You just need to match the screen module pin order to the pin header network symbols.
Buttons
There are also two buttons used to switch pages and reset data on the USB current meter. These buttons are configured for low-level triggering. Each button has a 10K pull-up resistor connected to 3.3V, and the other side of the button is connected to GND.
UART and Download Interface
UART Interface
The UART interface is used for debugging, allowing you to output current variable values using logs while writing code. Here, we only include TXD, RXD, and GND pins, omitting V3.3 since the USB current meter will typically have external power when in use.
Download Interface
The download interface uses the CH32V003-defined SWDIO and NRST pins. You can use the WCH Link-E programmer to flash the CH32V003. Similar to the UART interface, if external power is already connected, you can connect only GND and SWDIO when connecting the programmer.
Summary
We have now completed the schematic drawing for the USB current meter project. In LCSC EDA, you can use the Design > Update/Convert Schematic to PCB menu to update the schematic into the PCB editor and start routing.
Project Open Source Address
This project has been published on the OSHwHub.com. You can directly view and clone it using the following link:
中文版本
中文版本的文章请访问: