So I picked up an ACS70331 based sensor from seeed. Using the internal ADC of the ESP8266 board (10 bits) and some code provided by the ACS board manufacturer with some tweaks I am able to read a current that is showing up on my multimeter as 76mA, as 79mA with the ESP. Given other sensors I've tried, this one is doing great.
Here is the code that works:
#define RefVal 3.3
#define Pin A0
const int averageValue = 500;
long int sensorValue = 0;
float sensitivity = 1000.0 / 200.0; //1000mA per 200mV
float Vref = 1604.88;
void setup()
{
Serial.begin(115200);
}
static float tempval;
void loop()
{
for (int i = 0; i < 20; i++)
{
for (int i = 0; i < averageValue; i++)
{
int temp;
temp = analogRead(A0);
if (temp > sensorValue)
{
sensorValue = temp;
}
delayMicroseconds(40);
}
tempval += sensorValue;
}
sensorValue = tempval / 20.0;
tempval = 0;
// The on-board ADC is 10-bits
// Different power supply will lead to different reference sources
// example: 2^10 = 1024 -> 5V / 1024 ~= 4.88mV
// unitValue= 5.0 / 1024.0*1000 ;
float unitValue = RefVal / 1024.0 * 1000 ;
float voltage = unitValue * sensorValue;
//When no load,Vref=initialValue
Serial.print("initialValue: ");
Serial.print(voltage);
Serial.println("mV");
float current = ((voltage - Vref) * sensitivity) * 0.707;
voltage = unitValue * sensorValue - Vref;
Serial.print(voltage);
Serial.println("mV");
Serial.print("current: ");
Serial.print(current);
Serial.println("mA");
Serial.print("\n");
sensorValue = 0;
delay(1000);
}
Now, I figured that a 16bit ADC would give me less of a margin of error. I have the ADS1115 and came across this thread: AC Energy Meter calculation using ACS712 - General Electronics - Arduino Forum
I tried to "merge" methods found in the thread and in the first link (this first code block) with no luck; probably because I don't fully understand how to.
For example:
-the ADS has numerous "gain" methods that I am not sure if and which I should use and how that would affect the calculations. For example, say I read a single analog measurement from the sensor at the peak of the AC sinus wave. How would I get the current mathematically with this ADC?
-Numerous commenters in the linked thread mentioned changing the
sampling rate of the ADC and in other forum posts I've read people trying to do this with no luck. I've read other threads where some stated that the max sampling rate is too slow for AC and >1000 is preferred.
-the ADS has an internal reference yet many examples, including the one in the linked thread, have static values set for the voltage (e.g. 5, 3.3). Presumably, introduces errors if the supply voltage isn't perfect? What's the point of the internal reference then?
Code attempt:
#include <Wire.h>
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x48);
int mVperAmp = 200;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup() {
Serial.begin(115200);
ads.begin();
}
void loop() {
Voltage = getVPP();
VRMS = (Voltage / 2.0) * 0.707;
AmpsRMS = (VRMS * 1000) / mVperAmp;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 32768; // store min value here
uint32_t start_time = millis();
while ((millis() - start_time) < 1000) //sample for 1 Sec
{
readValue = ads.readADC_SingleEnded(0);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the maximum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 3.3) / 32768;
return result;
}
Reporting 30mA when multi-meter has it at 80mA.
Sorry for all the questions. I think I bit off more than I can chew.
More info: Voltage on ESP, ADS and ACS: 3.3v. ADS connected over i2c; ADDR pin to ground (default address). ACS in series with load and multimeter. Trying to measure less than 100mA AC.
A note: I am aware that the ACS does NOT have a high isolation voltage rating. I am simply trying to get a reliable reading with a current sensor, and the ADS, where the load is of low amperage (<100mA) until my other sensor arrives that has a greater isolation voltage. That being said, this sensor should be fine for testing as long as there isn't a surge.
I have another thread on detecting AC voltage with an opto. That is still alive, I am just waiting for parts and experimenting with other sensors.