Last active
April 19, 2024 04:01
-
-
Save jLynx/748e78e1ae4aabbb7f72a1a91a4a8eed to your computer and use it in GitHub Desktop.
ADS1100 Arduino
This file contains 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
#include <Wire.h> | |
#define ADS1100_ADDRESS 0x48 // https://www.ti.com/lit/ds/sbas239b/sbas239b.pdf | |
#define BATTERY_MIN_VOLTAGE 3.0 | |
#define BATTERY_MAX_VOLTAGE 4.2 | |
#define BATTERY_CAPACITY 2500.0 | |
#define BATTERY_ENERGY 9.25 | |
void setup() { | |
Wire.begin(); | |
Serial.begin(9600); | |
} | |
void loop() { | |
// Start a conversion | |
Wire.beginTransmission(ADS1100_ADDRESS); | |
Wire.write(0x00); | |
Wire.endTransmission(); | |
// Wait for the conversion to complete | |
delay(10); | |
// Read the conversion result | |
Wire.requestFrom(ADS1100_ADDRESS, 2); | |
uint16_t raw = (Wire.read() << 8) | Wire.read(); | |
// Convert the raw value to voltage | |
float voltage = (raw * 4.096) / 32768.0; | |
// Calculate the remaining capacity, energy, and percentage | |
float remainingCapacity = (voltage - BATTERY_MIN_VOLTAGE) / (BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * BATTERY_CAPACITY; | |
float remainingEnergy = (voltage - BATTERY_MIN_VOLTAGE) / (BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * BATTERY_ENERGY; | |
float batteryPercentage = (voltage - BATTERY_MIN_VOLTAGE) / (BATTERY_MAX_VOLTAGE - BATTERY_MIN_VOLTAGE) * 100.0; | |
// Limit the values to the valid range | |
remainingCapacity = constrain(remainingCapacity, 0, BATTERY_CAPACITY); | |
remainingEnergy = constrain(remainingEnergy, 0, BATTERY_ENERGY); | |
batteryPercentage = constrain(batteryPercentage, 0, 100.0); | |
// Print the battery information to the serial monitor | |
Serial.print("Battery Voltage: "); | |
Serial.print(voltage, 2); | |
Serial.println(" V"); | |
Serial.print("Remaining Capacity: "); | |
Serial.print(remainingCapacity, 0); | |
Serial.println(" mAh"); | |
Serial.print("Remaining Energy: "); | |
Serial.print(remainingEnergy, 2); | |
Serial.println(" Wh"); | |
Serial.print("Battery Percentage: "); | |
Serial.print(batteryPercentage, 1); | |
Serial.println("%"); | |
Serial.println(); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment