Giter Site home page Giter Site logo

eskopal / usb-power-gauge Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adafruit/usb-power-gauge

3.0 4.0 1.0 231 KB

Code for the Adafruit USB Power Gauge mini kit

Home Page: http://www.adafruit.com/products/1549

C++ 38.44% C 23.69% Arduino 37.87%

usb-power-gauge's Introduction

USB Power Gauge updates by Eugene Skopal

Version 2.4 -- 12-Jan-2014

Observations / Changes

I noticed the green led was flickering again. I cleaned up the code quite a bit and increased the buffer size to accomidate the new timestamp.

1) Update LED info before printing our report to minimize green LED flicker.
2) Breakup code into subroutines to make code more readable.
3) Increase Output Buffer size to accomidate time stamps.
4) Remove unneeded volatile keywords.
5) Added more comments.

Version 2.3 -- 10-Jan-2014

Observations / Changes

User wbphelps suggested adding time stamps to the reports emitted by the USB Power Gauge. He also reported issues with the RC Oscillator timing, which becomes more obvious when watching timestamps over a few minutes.

I added timestamps to the printouts. The time is not very accurate as it is based on the internal RC Oscillator - but it does give you an sense of how long charging or other activities have been running.

I added the ability to adjust the RC Oscillator. From the source code:

You can now calibrate the RC Oscillator by using a terminal program that can
display the time each message is received in milliseconds.  I use TERMINAL
by Bray++ from https://sites.google.com/site/terminalbpp/.
   
Compile and load this program into your USB Power Gauge and let it run.  
It is set to alter the OSCCAL by a value of -4 which was correct for the
USB POWER GAUGE I received.
Simply watch the program run for awhile in TERMINAL with its Time option checked.
If the timestamps are occurring too soon: (e.g. 1.400 then 2.350 then 3.270) 
  edit MY_OSCCAL_DELTA to be more negative -4 becomes -5.
If the timestamps are occurring too late: (e.g. 1.400 then 2.500 then 3.590) 
  edit MY_OSCCAL_DELTA to be more positive -4 becomes -3.
You must also edit CAL_DATA_VERSION to a different value (e.g. E2 becomes E3) in
order to force the OSCCAL delta calibration value to be updated.
Once you have edited the values go back to step 1 until you see the timestamps
occurring both too soon and too late.
Finally, you need to plug the USB POWER GAUGE into an accurate 5V USB port to reset
the 5V calibration value.

There are two overlapping ranges of OSCCAL values, 0-0x7F and 0x80-0xFF.  My factory value
is 0x86.  If I were to set MY_OSCCAL_DELTA to -7, that would move me into the top of the 
other range and the oscillator would suddenly go much faster.  In that case I would then 
need a much larger delta (like -100) to continue to slow down the RC oscillator.

Version 2.2 -- 02-Dec-2013

Observations / Changes

I noticed that I was just about out of memory for the updated sketch (99%) was in use. I updated the sketch to conserve a little more memory by using only Print() and not Println(). The build shows 98% in use.

I had downloaded the "trinket" support files from ADAfruit to work on the USB Power Gauge. It occurred to me that the ATtiny85 has 8192 bytes of flash, but the sketch reported only 5310 bytes available. This is specified in the ..\Arduino\hardware\attiny\boards.txt file that is part of the trinket support files supplied by Adafruit. It limits the trinket sketch to 5310 bytes of memory so that the trinket bootloader is not overwritten. Since we do not use a bootloader, there is no reason to conserve that space, so I updated the boards.txt file to include an “Adafruit USB Power Gauge” board that has 8192 bytes of memory. You need to copy that file over the existing boards.txt, and change the sketch to use the "Adafruit USB Power Gauge" board to get access to the extra memory. The build now shows 63% in use.

Version 2.1 -- 01-Dec-2013

Observations

When testing the USB Power Gauge I noticed that when the voltage was low, the green LED went out as it was designed to do. I found this confusing because with no load, it appeared that there was no power, not that the voltage was too low.

Also, when looking for the appropriate place to post a notice about this new firmware, I noticed that others had asked for the power gauge to support 2A (10w) power supplies (for Apple IPAD).

Code Changes

  1. Compute the calibration value for 2.56v reference

  2. Read currents up to 2.56 Amps

  3. Flash Green LED at 4 hz if voltage is less than 4.5 volts

  4. Blink Green LED at .5 hz if watts > 5.125, display watts as 6,7,8,9,10

  5. Blink Flashing Green LED if low voltage and high watts (Flashes 4 times in one second, then off for one second)

Version 2.0 -- 30-Nov-2013

Original Code Observations

When using the USB Power Gauge I found the green power led was flickering as well as the first blue led. I was concerned that my USB hub was not providing a solid 5v supply, so I hooked up a USB-TTL serial cable and checked the voltage. I found that the voltage was over 5v and that it was being misreported. This meant there was some other reason for the green LED flickering.

The first blue LED flickered randomly even though there was no load connected.

Code Review Observations

I reviewed the code available here on GitHub and discovered the reason for the Green LED flickering was the SoftwareSerial implementation. Even though the original code cleverly used interrupts to control the Charlie-Plexing (http://en.wikipedia.org/wiki/Charlieplexing) of the LED's SoftwareSerial turned off the interrupt system while it was outputting the individual characters causing a noticeable delay in the LED display. The original code tried to time the output of characters to minimize the flickering, but it was not really successful.

The incorrect 5V value was due to the calibration value being incorrect for the USB-Power-Gauge I had received.

Finally, the blue LED flickering was due to two issues. First, the output of the current monitor is very noisy, with random, but infrequent, readings of several ma of current even with no load connected. Secondly, the original code uses a clever "gamma" table to try and vary the brightness of the LEDs for current between 1 and 1000 ma. Unfortunately, the implementation resulted in the LED turning on even when the value was only 1ma.

Code Changes

I tracked down code that implemented a UART using a TIMER (http://www.siwawi.arubi.uni-kl.de/avr_projects). I renamed it TimerSerial and re-worked it to run on the ATtiny85 in the Arduino environment. This fixed the flickering Green LED.

I added code to automatically update the calibration value twice if it is missing. This allowed me to erase the EEPROM using AVRDUDE and a USBTinyISP. When the USB-Power-Gauge reboots after erasing the EEPROM, it computes a calibration value based on the inaccurate 5v supply of the programmer. I then simply unplug the USB-Power-Gauge and plug it into a Desktop computer USB port that presents a very accurate 5v supply. The calibration value is recomputed and is a good deal more accurate.

I rewrote the main loop to average the current value for one second. This removed the random noise from the current sensor. The code now displays both the average current and the peak current each second.

I corrected the "gamma" display routines to leave the LED completely off for very small values of current.

Finally, I added a feature TimerSerial that allows the program to sense the value of the TX pin. When it isn't sending a status update it watches for the TX pin to be pulled (by a 1K resistor to Ground). If it's pulled low for 3 seconds, once it is released, a new calibration value is written.

usb-power-gauge's People

Contributors

eskopal avatar ladyada avatar

Stargazers

A-Ron avatar Steven Lu avatar William Phelps avatar

Watchers

William Phelps avatar  avatar  avatar A-Ron avatar

Forkers

wbphelps

usb-power-gauge's Issues

Problems compiling

Just recently got this little device and want to see about making it more accurate with higher powered devices, and came across your pull...

Compiling this I get the following:

In file included from /Applications/Development/Electronics/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/WString.h:29:0,
                 from /Applications/Development/Electronics/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Print.h:26,
                 from /Applications/Development/Electronics/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/Stream.h:26,
                 from /var/folders/t1/98d__2cx6j3d1b3zws9n2csw0000gn/T/build3861436463000155196.tmp/sketch/TimerSerial.h:5,
                 from adafruit_usbpowergauge.ino:87:
/Applications/Development/Electronics/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/WString.h:38:74: error: __c causes a section type conflict with __c
 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
                                                                          ^
adafruit_usbpowergauge.ino:277:12: note: in expansion of macro 'F'
/Applications/Development/Electronics/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/WString.h:38:74: note: '__c' was declared here
 #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
                                                                          ^
adafruit_usbpowergauge.ino:491:12: note: in expansion of macro 'F'
Error compiling.

Now I should note that I'm running Mac OSX 10.11 Beta 3, Arduino 1.6.6 Build 2015/28/08 with ATTiny support from https://github.com/damellis/attiny/tree/ide-1.6.x, so there are a whole lot of places for me to look, but I thought I'd just check to see if you're still watching this and ask what specific environment you were using when you last compiled this code. I'll be able to try this under Linux with Arduino IDE (1.6.4 or 5 I think... definitely not the latest at any rate) in a couple of days, but on the off chance you have any thoughts...

Thanks!

Replacing the Device Original Source Code

Hi;

I am using the Adafruit USB Power Gauge to measure the Raspberry Pi 3 power consumption and based on my observations, it is not accurate at all while working with the original code. Probably it has some serious calibration issues. I'd like to replace the original source code with yours, which you claim is more accurate. Could you please tell me what the best way to do so is? I'd be really thankful.

Cheers,
Vahid

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.