USB-C Power Metering with the ChargerLAB KM003C: A Google Twinkie Alternative?
by Ganesh T S on July 5, 2023 8:00 AM EST- Posted in
- Gadgets
- USB PD
- Power Assessment Tool
- USB-C
API Access on Windows
Custom user programs and scripts to access the data from the KM003C are comparatively easier to create on Windows compared to Linux. One of the primary reasons is that ChargerLAB supplies a HID demo program (ZIP) on its technical support page. It includes a VisualStudio 2019 project with C++ source code to access the data from the device. The KM003C also presents itself as an USB device as well as a serial device to the OS. In the ideal case, we should have seen the latter interface even in Linux as /dev/ttyACM0, but that was not the case in our Ubuntu install.
Powershell is my preferred scripting language on Windows machines, and accessing the KM003C as a serial device is as simple as opening the appropriate COM device output in the above command. As mentioned in the API document, the baud rate and other parameters do not matter.
The command to be sent over the serial port for getting the ADC data (the 32-bit word, 0x0C000200) is prepared. Similar to libusb, the device write function expects a UTF-8 string. Unlike libusb, the read function is not blocking. In order to avoid polling for the data, or waiting for a pre-defined amount of time (by which there is no guarantee that the read might have returned the data from the device or not), a Powershell event is set up to trigger whenever data is received on the opened port.
The read out process is isolated out into a script block, and the logic to compute the instantaneous and averaged power consumption is similar to what was done using Python on Linux. The main difference is that we wait for the event triggered by the data receipt before the call to the read from the port.
The script block is called in a loop every 100ms and generates output that is similar to the one created by the Python script in the previous section - CSV strings with a timestamp, followed by instantaneous voltage, current, and power, and the averaged versions of the same.
The clean-up process involves closing the port and deregistering / removing the event set up to track the data receipt from the port.
20 Comments
View All Comments
ballsystemlord - Wednesday, July 5, 2023 - link
It's unfortunate that the KM003C is windowz only closed source.ganeshts - Wednesday, July 5, 2023 - link
It is not a major problem, as I have demonstrated in a couple of sections. You can use libusb or any other serial port access program to communicate with the KM003C and create a wrapper suited for your particular use-case.That said, one aspect I should have mentioned in the conclusions is related to the ability to update the firmware independent of the closed-source Windows program. Coupled with better API documentation, that would basically make the device pretty functional without having to rely on the closed-source program.
ballsystemlord - Wednesday, July 5, 2023 - link
Oh, I see.QChronoD - Wednesday, July 12, 2023 - link
Since you posted a screenshot of the code for connecting and pulling out the relevant power data, are you OK with people using it? Are you planning on posting it on github or somewhere?ganeshts - Thursday, July 13, 2023 - link
Absolutely! Code is free for anyone to start off with as the base point for their own programs.My github account has been idle for a few years now. I will probably upload a refined version of the code that appears in this review later this year when I get time. The only reason I didn't post the code as text is to avoid making the review appear like a StackOverflow post :)
TrevorH - Thursday, July 6, 2023 - link
You could have saved me from going straight to the last page by putting the price at the top of the article. At $10 I would have snapped your hand off, at $110 it's a definite no.ganeshts - Thursday, July 6, 2023 - link
I think it would be impossible to hit $10 for this type of product. Even BOM cost for microcontroller + 5 ADCs would easily exceed that (even if they are bought in 1Ku quantities). Need to add software development and distribution costs. I would imagine break-even is itself around $40 - $50.ads295 - Saturday, July 8, 2023 - link
+1It's not for "hobbyists" as mentioned in the article.
Bp_968 - Thursday, August 3, 2023 - link
Not sure I agree that 50$ puts it out of reach of "hobbyists". I'm a hobbyist and own a oscilloscope, and a number of other test tools more expensive then 50-100$. If all your doing is seeing what voltage your usb battery is negotiating with your switch or steamdeck then yes, this device is overkill. But if you building anything that's using PD or other usb-c features this could be extremely useful.I'm unlikely to build anything anytime soon that would require the features this thing supports but I still want one! ;)
DanNeely - Thursday, July 6, 2023 - link
"The KM003C can support up to 50V / 6A (full USB-PD 3.1 specifications, with EPR up to 240W)."Unless this device is going significantly beyond USB-PD levels I believe this should read 5A not 6.