`#include <Wire.h>~
#define I2C_ADDRESS 97 // Default I2C address for ACS71020
// Full-scale voltage and current constants #define FULL_SCALE_V 230.0 // Example: Full-scale voltage #define FULL_SCALE_I 30.0 // Example: Full-scale current
// Register definitions #define REG_IRMS_VRMS 0x20 #define REG_IRMS_VRMS_PROM 0x21 #define REG_ACTPWR_PROM 0x22 #define REG_APPPWR 0x23 #define REG_REAPWR 0x24 #define REG_PFACTOR 0x25
uint8_t acs_RxData[4]; uint8_t currentAddress = I2C_ADDRESS;
void setup() { Wire.begin(); // Initialize I2C Serial.begin(115200); // Initialize Serial for debugging delay(1000); // Allow peripherals to stabilize
// Check initial connection if (!acs71020_check_connection()) { Serial.println("ERROR: ACS71020 not detected! Check wiring and power."); while (true) { delay(1000); // Halt program if the sensor is not detected } }
Serial.println("ACS71020 connected and ready."); }
bool acs71020_check_connection() { Wire.beginTransmission(currentAddress); uint8_t error = Wire.endTransmission(); return (error == 0); // Returns true if the device is detected }
void acs71020_read(uint8_t reg) { Wire.beginTransmission(currentAddress); Wire.write(reg); if (Wire.endTransmission(false) != 0) { Serial.println("ERROR: Failed to communicate with ACS71020!"); return; } Wire.requestFrom(currentAddress, (uint8_t)4); // Request 4 bytes
for (int i = 0; i < 4; i++) { if (Wire.available()) { acs_RxData[i] = Wire.read(); } else { acs_RxData[i] = 0; // Fill with zero if no data available } } }
float acs71020_get_voltage() { float aux_v = 0;
acs71020_read(REG_IRMS_VRMS); aux_v = ((float)(acs_RxData1 * 256 + acs_RxData[0])) / 0x8000; aux_v = aux_v * FULL_SCALE_V;
return aux_v; }
float acs71020_get_current() { float aux_i = 0;
acs71020_read(REG_IRMS_VRMS_PROM); aux_i = ((float)(acs_RxData[3] * 256 + acs_RxData[2])); aux_i = aux_i * FULL_SCALE_I / 0x4000;
return aux_i; }
void check_and_update_i2c_address() { uint8_t newAddress = 0; bool deviceFound = false;
// Scan for the ACS71020 device for (uint8_t address = 1; address < 127; address++) { Wire.beginTransmission(address); if (Wire.endTransmission() == 0) { newAddress = address; deviceFound = true; break; } }
if (!deviceFound) { Serial.println("ERROR: ACS71020 not detected on I2C bus!"); delay(1000); // Pause to avoid flooding the Serial Monitor return; }
if (newAddress != currentAddress) { Serial.printf("ALERT: ACS71020 I2C address changed from 0x%X to 0x%X!\n", currentAddress, newAddress); currentAddress = newAddress; // Update the current address } }
void loop() { // Check for I2C address changes check_and_update_i2c_address();
// Get sensor values float voltage = acs71020_get_voltage(); float current = acs71020_get_current();
// Display the readings Serial.printf("Voltage (Vrms): %.2f V\n", voltage); Serial.printf("Current (Irms): %.2f A\n", current); Serial.println();
delay(1000); // Delay 1 second }`
#include <Wire.h>~
#define I2C_ADDRESS 97 // Default I2C address for ACS71020
// Full-scale voltage and current constants
#define FULL_SCALE_V 230.0 // Example: Full-scale voltage
#define FULL_SCALE_I 30.0 // Example: Full-scale current
// Register definitions
#define REG_IRMS_VRMS 0x20
#define REG_IRMS_VRMS_PROM 0x21
#define REG_ACTPWR_PROM 0x22
#define REG_APPPWR 0x23
#define REG_REAPWR 0x24
#define REG_PFACTOR 0x25
uint8_t acs_RxData[4];
uint8_t currentAddress = I2C_ADDRESS;
void setup() {
Wire.begin(); // Initialize I2C
Serial.begin(115200); // Initialize Serial for debugging
delay(1000); // Allow peripherals to stabilize
// Check initial connection
if (!acs71020_check_connection()) {
Serial.println("ERROR: ACS71020 not detected! Check wiring and power.");
while (true) {
delay(1000); // Halt program if the sensor is not detected
}
}
Serial.println("ACS71020 connected and ready.");
}
bool acs71020_check_connection() {
Wire.beginTransmission(currentAddress);
uint8_t error = Wire.endTransmission();
return (error == 0); // Returns true if the device is detected
}
void acs71020_read(uint8_t reg) {
Wire.beginTransmission(currentAddress);
Wire.write(reg);
if (Wire.endTransmission(false) != 0) {
Serial.println("ERROR: Failed to communicate with ACS71020!");
return;
}
Wire.requestFrom(currentAddress, (uint8_t)4); // Request 4 bytes
for (int i = 0; i < 4; i++) {
if (Wire.available()) {
acs_RxData[i] = Wire.read();
} else {
acs_RxData[i] = 0; // Fill with zero if no data available
}
}
}
float acs71020_get_voltage() {
float aux_v = 0;
acs71020_read(REG_IRMS_VRMS);
aux_v = ((float)(acs_RxData[1] * 256 + acs_RxData[0])) / 0x8000;
aux_v = aux_v * FULL_SCALE_V;
return aux_v;
}
float acs71020_get_current() {
float aux_i = 0;
acs71020_read(REG_IRMS_VRMS_PROM);
aux_i = ((float)(acs_RxData[3] * 256 + acs_RxData[2]));
aux_i = aux_i * FULL_SCALE_I / 0x4000;
return aux_i;
}
void check_and_update_i2c_address() {
uint8_t newAddress = 0;
bool deviceFound = false;
// Scan for the ACS71020 device
for (uint8_t address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
newAddress = address;
deviceFound = true;
break;
}
}
if (!deviceFound) {
Serial.println("ERROR: ACS71020 not detected on I2C bus!");
delay(1000); // Pause to avoid flooding the Serial Monitor
return;
}
if (newAddress != currentAddress) {
Serial.printf("ALERT: ACS71020 I2C address changed from 0x%X to 0x%X!\n", currentAddress, newAddress);
currentAddress = newAddress; // Update the current address
}
}
void loop() {
// Check for I2C address changes
check_and_update_i2c_address();
// Get sensor values
float voltage = acs71020_get_voltage();
float current = acs71020_get_current();
// Display the readings
Serial.printf("Voltage (Vrms): %.2f V\n", voltage);
Serial.printf("Current (Irms): %.2f A\n", current);
Serial.println();
delay(1000); // Delay 1 second
}
Kindly help me to solve this problem !!!
Thank you.