ESP32 and Current Sensor ACS720
I'm really in need of some help… I have a ESP32 device and I'm trying to get the Gravity Analog 20A (SEN0214) current sensor working with it.
I've downloaded the example associated sketch, and did a few mods for my device, but I don't get accurate readings at all from the sensor. I'm sure it's something to do with how I've configured things. I did run the untouched sketch on a standard Uno and it works just fine. Here's what I've modified for my ESP32 set up….
1) I built a voltage divider ( sensor signal wire to 1k ohm resister to 2k ohm resister to ground, mid point to pin 39 on ESP32) as the ESP32 is a 3.3v device and I don't want to send a 5v signal to the pin (this is probably the root cause if the bad values). 5V power pin to VCC on sensor, GND to GND pin on sensor. The power pin is actually getting 5.2v from the ESP32.
Then, in the DCCurrentValue calculation I changed the resolution from 1024 to 4095 as the ESP32 has a higher resolution ADC.
The code, it seems to me, should work on a 3.3v device as there is selection code for that. I don't know it the sensor can run at 3.3v, so maybe what I'll do is remove the voltage devider and just power the sensor off 3.3 volts… But if you have a suggestion I would really appreciate it.
I've attached the code below.
/***********************************************************
This sample code shows how to use 20A current sensor module.
Created 2016-4-26
By Bernie Chen <[email protected]>
GNU Lesser General Public License.
See <http://www.gnu.org/licenses/> for details.
All above must be included in any redistribution
****************************************************/
/***********Notice and Trouble shooting***************
1.Connection and Diagram can be found here, http://www.dfrobot.com/wiki/index.php?title=20A_Current_Sensor_SKU:SEN0214
2.This code is tested on Arduino Uno.
****************************************************/
const int currentSensorPin = 39; //define sensor pin
const int mVperAmp = 100; // use 185 for 5A Module, and 66 for 30A Module
float Vref = 0; //read your Vcc voltage,typical voltage should be 5000mV(5.0V)
void setup()
{
Serial.begin(115200);
Vref = readVref(); //read the reference votage(default:VCC)
}
void loop()
{
/*If you are reading DC current, use this function to read DC current*/
float CurrentValue = readDCCurrent(currentSensorPin);
/*If you are reading AC current, use this function to read AC current*/
//float CurrentValue = readACCurrent(currentSensorPin);
Serial.println(CurrentValue);
delay(500);
}
/*read DC Current Value function*/
float readDCCurrent(int Pin)
{
int analogValueArray[31];
for(int index=0;index<31;index++)
{
analogValueArray[index]=analogRead(Pin);
}
int i,j,tempValue;
for (j = 0; j < 31 - 1; j ++)
{
for (i = 0; i < 31 - 1 - j; i ++)
{
if (analogValueArray[i] > analogValueArray[i + 1])
{
tempValue = analogValueArray[i];
analogValueArray[i] = analogValueArray[i + 1];
analogValueArray[i + 1] = tempValue;
}
}
}
float medianValue = analogValueArray[(31 - 1) / 2];
float DCCurrentValue = (medianValue / 4095.0 * Vref - Vref / 2.0) / mVperAmp; //Sensitivity:100mV/A, 0A @ Vcc/2
return DCCurrentValue;
}
/*read reference voltage*/
long readVref()
{
long result;
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
ADCSRB &= ~_BV(MUX5); // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#endif
#if defined(__AVR__)
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1126400L / result; //1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186
return result;
#elif defined(__arm__)
return (3300); //Arduino Due
#else
return (3300); //Guess that other un-supported architectures will be running a 3.3V!
#endif
}
Tried that, didn't seem to work. The link you provided shows the power requirements as 4.5v to 5.5v…
Gary.MackYou can remove the voltage divider and power the sensor directly from the ESP32's 3.3V pin.
https://www.digikey.com/en/products/detail/allegro-microsystems/ACS720KLATR-35AB-T/7363745
lia.ifat