Created
March 27, 2017 08:54
-
-
Save ryo1kato/e0687b4047a276f383cc525370902201 to your computer and use it in GitHub Desktop.
amp-hour meter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const uint32_t shuntMilliOhm = 10100; | |
| const uint32_t refMilliVoltage = 1100; | |
| const int sensorPin = A0; | |
| const int updateIntervalMsec = 500; | |
| void setup() { | |
| Serial.begin(9600); | |
| analogReference(INTERNAL); | |
| Serial.println(""); | |
| Serial.println("Secs,ADC,mV,uA,(uAmsec),uAh,uAh/h"); | |
| } | |
| uint32_t prev = 0; | |
| uint32_t uAmsec = 0; | |
| uint32_t uAh= 0; | |
| uint32_t lastStatusUpdate = 0; | |
| void loop() { | |
| uint32_t now = micros(); | |
| if (prev != 0) { | |
| uint32_t sensorValue = analogRead(sensorPin); | |
| uint32_t mV = refMilliVoltage * sensorValue / 1023UL; | |
| uint32_t uA = mV * 1000UL * 1000UL / shuntMilliOhm; | |
| uAmsec += uA * (now - prev) / 1000UL; | |
| if (uAmsec > 1000UL * 3600UL ) { | |
| uAh += uAmsec / (1000UL * 3600UL); | |
| uAmsec = uAmsec % (1000UL * 3600UL); | |
| } | |
| if (now - lastStatusUpdate > updateIntervalMsec * 1000UL) { | |
| Serial.print("\r \r"); | |
| Serial.print(millis()/1000, DEC); Serial.print(","); | |
| Serial.print(sensorValue, DEC); Serial.print(","); | |
| Serial.print(mV, DEC); Serial.print(","); | |
| Serial.print(uA, DEC); Serial.print(","); | |
| Serial.print(uAmsec, DEC); Serial.print(","); | |
| Serial.print(uAh, DEC); Serial.print(","); | |
| Serial.print(uAh*3600UL/(millis()/1000UL), DEC); | |
| lastStatusUpdate = now; | |
| } | |
| } | |
| prev = now; | |
| delay(1); | |
| } | |
| ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment