20A_Current_Sensor_(SKU_SEN0214) - jimaobian/DFRobotWiki GitHub Wiki
[[/image:Name_DFRxxxx_sample1.png|thumb|300px|right|Product Name
Update/modify/delete Forbidden, 禁止更改本图,请更改图片名称新建图片
]]
Introduction
This 20A current sensor module is based on Hall effect-based linear current chip, so wide range, easy usage, small size and high accuracy are available. You can use this module to measure direct current or alternating current. In design, we do the high voltage isolation, which ensures the safety in use. The output of this moudle is the analog voltage linear with measured current, and the Gravity 3P interface imporves the usability. Long time work with or upon 20A is not recommended. Pay attention to safety when measuring the high voltage!
Specification
- Supply Voltage: 5.0V
- Current Range: 0 ~ 20A
- Measure Endure: 220V AC
- Relative error: + 5%
- Size: 39mm * 22mm * 17mm
- Interface: Gravity PH2.0-3P
Board Overview
--------- | -------------------------- |
Num | Label |
1 | GND |
2 | 5V |
3 | Signal |
4 | Measuring current Input |
5 | Measuring current Output |
Tutorial
This tutorial will teach you to use the 20A current sensor.
Requirements
- Hardware
- UNO x1
- 20A Current Sensor module x1
- Gravity 3P Analog Wire x1
- Dupont Line Several
- Software
- Arduino IDE V1.6.8 Click to Download Arduino IDE from Arduino®
Connection Diagram
Sample Code
/***************************************************
This example reads 20A Current Sensor.
Created 2016-4-26
By berinie 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.
****************************************************/
int currentPin = A2;
void setup()
{
Serial.begin(115200);
}
void loop()
{
/*read DC current value*/
float CurrentValue = readDCCurrentValue(currentPin);
/*read AC current value*/
// float CurrentValue = readACCurrentValue(currentPin);
Serial.println(CurrentValue);
delay(500);
}
/*read DC Current Value function*/
float readDCCurrentValue(int Pin)
{
float DCValue = analogRead(Pin);
float DCCurrentValue = (DCValue / 1024.0 * 5000 - 5000 / 2) / 100;
return DCCurrentValue;
}
/*read AC Current Value function*/
float readACCurrentValue(int Pin)
{
static int peak[5] = {0, 0, 0, 0, 0};
static int voltageValue[10] = {0};
static float voltage = 0;
unsigned long testTime = millis();
/*read peak value*/
while (millis() - testTime < 120)
{
for (int i = 0; i < 10; i++)
{
voltageValue[i] = analogRead(Pin);
delayMicroseconds(1);
}
InsertSort(voltageValue, 10);
peak[0] = voltageValue[9];
InsertSort(peak, 5);
}
voltage = (peak[1] + peak[2] + peak[3]) / 3;
for (int i = 0; i < 5; i++)
{
peak[i] = 0;
}
float Vref = readVref();
float ACCurrentValue = (voltage / 1024 * Vref - Vref / 2) / 100 /1.414;
return ACCurrentValue;
}
/*Insertion sort algorithm*/
void InsertSort(int arr[], int count)
{
int i, j, temp;
for (i = 1; i < count; i++)
{
temp = arr[i];
for (j = i - 1; j > -1 && arr[j] > temp; j--)
{
arr[i] = arr[j];
arr[j] = temp;
}
}
}
/*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
}
FAQ
For any questions/advice/cool ideas to share, please visit DFRobot Forum.
More
shopping from dfrobot store or dfrobot distributor.