#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 sensor setup
#define DS18B20_PIN 2 // Use the digital pin you connected the DS18B20 sensor to
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
#define VIN A0 // define the Arduino pin A0 as voltage input (Vin)
const float VCC = 5.0; // supply voltage 5V or 3.3V. If using PCB, set to 5V only.
const int model = 2; // enter the model (see below)
float cutOffLimit = 30.00; // reading cutoff current. 1.00 is 1 Ampere
// Models of ACS758
const char *models[] = {
"ACS758LCB-050B",
"ACS758LCB-050U",
"ACS758LCB-100B",
"ACS758LCB-100U",
"ACS758KCB-150B",
"ACS758KCB-150U",
"ACS758ECB-200B",
"ACS758ECB-200U"};
// Sensitivity array holding the sensitivity of the ACS758 current sensors. Do not change.
float sensitivity[] = {
40.0, // for ACS758LCB-050B
60.0, // for ACS758LCB-050U
20.0, // for ACS758LCB-100B
40.0, // for ACS758LCB-100U
13.3, // for ACS758KCB-150B
16.7, // for ACS758KCB-150U
10.0, // for ACS758ECB-200B
20.0 // for ACS758ECB-200U
};
// Quiescent Output voltage is a factor for VCC that appears at output
// when the current is zero.
// Do not change.
float quiescent_Output_voltage[] = {
0.5, // for ACS758LCB-050B
0.12, // for ACS758LCB-050U
0.5, // for ACS758LCB-100B
0.12, // for ACS758LCB-100U
0.5, // for ACS758KCB-150B
0.12, // for ACS758KCB-150U
0.5, // for ACS758ECB-200B
0.12 // for ACS758ECB-200U
};
const float FACTOR = sensitivity[model] / 1000; // set sensitivity for selected model
const float QOV = quiescent_Output_voltage[model] * VCC; // set quiescent Output voltage for selected model
float voltage; // internal variable for voltage
float cutOff = FACTOR / cutOffLimit; // convert current cutoff to mV
// Battery parameters
const float batteryCapacity = 35.0; // Battery capacity in Amp-hours (AH)
float remainingCapacity = batteryCapacity; // Remaining battery capacity in Amp-hours
void setup() {
Serial.begin(9600); // initialize the serial monitor
Serial.println("Robojax Tutorial");
Serial.println("ACS758 Current Sensor");
// Initialize the DS18B20 sensor
sensors.begin();
sensors.setResolution(11); // Set the sensor resolution to 11 bits for 0.25°C resolution
}
void loop() {
float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // Read the voltage from sensor
voltage = voltage_raw - QOV + 0.007; // 0.007 is a value to make voltage zero when there is no current
float current = voltage / FACTOR;
// Calculate the discharged charge over time
float dischargeCurrent = abs(current); // Taking the absolute value of the current for discharge
float dischargeTimeSeconds = 0.5; // Interval in seconds (adjust this according to your needs)
float dischargedCharge = dischargeCurrent * dischargeTimeSeconds / 3600.0; // Converted to Amp-hours
// Update the remaining battery capacity
remainingCapacity -= dischargedCharge;
if (remainingCapacity < 0) {
remainingCapacity = 0;
}
// Read DS18B20 temperature
sensors.requestTemperatures(); // Request temperature data from the sensor
float temperatureC = sensors.getTempCByIndex(0); // Read the temperature in Celsius
if (abs(voltage) > cutOff) {
Serial.print("V: ");
Serial.print(voltage, 3); // print voltage with 3 decimal places
Serial.print("V, I: ");
Serial.print(current, 2); // print the current with 2 decimal places
Serial.print("A, Remaining Capacity: ");
Serial.print(remainingCapacity, 2); // print the remaining capacity with 2 decimal places
Serial.print("AH, Battery Percentage: ");
float batteryPercentage = (remainingCapacity / batteryCapacity) * 100.0;
Serial.print(batteryPercentage, 2); // print the battery percentage with 2 decimal places
Serial.println("%");
} else {
Serial.println("No Current");
}
if (temperatureC != DEVICE_DISCONNECTED_C) {
Serial.print("Temperature: ");
Serial.print(temperatureC, 2); // Print temperature with 2 decimal places
Serial.println("°C");
} else {
Serial.println("Error reading temperature from DS18B20.");
}
delay(1000); // Adjust the delay according to your needs
}