搜索代码

使用Allegro ACS712直流/交流电流传感器与Arduino代码

使用Allegro ACS712直流/交流电流传感器与Arduino代码

在本教程中,我们将学习如何使用Allegro ACS712电流传感器通过Arduino测量交流和直流电流。ACS712传感器输出与通过它的电流成比例的电压,使我们能够轻松读取并在Arduino的串行监视器上显示电流值。该项目非常适合希望在各种应用中监测电流的人,例如电源或电池系统。

我们将建立一个简单的电路,将ACS712传感器连接到Arduino,然后编写一个程序来读取传感器的输出。预期结果是在串口监视器上显示实时电流读数。有关实现的详细说明,请务必查看视频(视频中的02:00)。

硬件解析

Allegro ACS712 电流传感器是一个紧凑的模块,可以测量高达 30A 的电流,具体取决于您选择的版本。它有三个引脚:VCC 用于供电,GND 用于接地,还有一个输出引脚提供与测量电流对应的电压。传感器采用霍尔效应原理,电流通过导体时会产生磁场。此磁场被检测并转换为电压,然后发送到 Arduino。

该模块提供不同的电流等级(5A、20A和30A),输出电压等级分别为185 mV/A、100 mV/A和66 mV/A。这意味着,每当传感器中流过一安培的电流时,您将获得一个相应的电压输出,您可以读取和处理。这个特性使其在各种应用中都具备多功能性。

数据表详细信息

制造商艾利格罗微系统公司
零件号ACS712ELCTR-30A-T
逻辑/IO电压4.5-5.5 伏
供电电压5伏
输出电流(每通道)30 A 最大
PWM频率指导N/A
输入逻辑阈值N/A
电压降 / RDS(开)/ 饱和度1.2 毫欧
热限制150 °C 最大
包裹TO-220
注释 / 变体提供5A、20A和30A可选。

  • 确保正确接线,以避免短路。
  • 使用适当规格的电线以适应电流额定值(30A时使用12 AWG)。
  • 在操作过程中监测模块的温度。
  • 考虑为高电流应用添加散热器。
  • 根据您的具体应用校准传感器输出。

接线说明

Arduino wiring for Allegro ACS712 current sensor

要将Allegro ACS712传感器连接到Arduino,请首先将ACS712的VCC引脚连接到Arduino上的5V引脚。接下来,将传感器的GND引脚连接到Arduino上的任意GND引脚。ACS712的输出引脚应连接到Arduino上的模拟输入引脚A0。

对于当前的测量,将负载与传感器串联连接。这意味着电源的正极线应连接到电流传感器的一个输入端子,而另一个端子连接到负载。负载的另一侧应返回电源,完成电路。确保使用适合您正在测量的电流的线规,以避免过热。

代码示例和步骤说明

以下代码片段初始化模拟引脚并设置串口监视器:

#define VIN A0 // define the Arduino pin A0 as voltage input (V in)
const float VCC = 5.0; // supply voltage is from 4.5 to 5.5V. Normally 5V.

在这里,我们定义VIN作为传感器输出连接的模拟输入引脚。我们还设置了供电电压。VCC到5伏。

在循环函数中,我们从ACS712传感器读取电压并计算电流:

float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // Read the voltage from sensor
float current = voltage / sensitivity[model]; // Calculate current based on sensitivity

在这里,我们从传感器读取原始电压,并根据所使用传感器的模型通过灵敏度数组将其转换为实际电流。

最后,我们将当前读数打印到串行监视器:

if (abs(current) > cutOffLimit) {
    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.println("A");
} else {
    Serial.println("No Current");
}

本节检查电流是否超过定义的截止限值,并将电压和电流读数打印到串口监视器。如果电流低于阈值,则输出“无电流”。

请参阅文章下方加载的完整代码。

演示 / 期待什么

当您运行代码并给电路供电时,您应该在串行监视器上看到电流读取值。随着您调整负载,读取值应相应变化,前提是电流高于截止限制。要注意传感器的温度,尤其是在接近其最大额定值时。

图像

Arduino wiring for Allegro ACS712 current sensor
Arduino wiring for Allegro ACS712 current sensor
ACS712_in_series_with_load
ACS712_in_series_with_load
allegro_ACS712_module-1
allegro_ACS712_module-1
allegro_ACS712_module-2
allegro_ACS712_module-2
allegro_ACS712_module-4
allegro_ACS712_module-4
allegro_ACS712_module-0
allegro_ACS712_module-0
130-Allegro ACS712 DC/AC current sensor with Arduino code
语言: C++
/*
 * 
 * Arduino Sketch for Allegro ACS712 Current Sensor 
 * This sensor can measure current at a range of up to 30A.
 * It operates with 5V.
 * Please watch the video instruction and explanation for this code.
 * 
 * Written by Ahmad Shamshiri on Sunday, June 17, 2018 at 22:06 in Ajax, Ontario, Canada
 * for Robojax.com
 * View the video instruction at https://youtu.be/DVp9k3xu9IQ
 * This code has been downloaded from Robojax.com
 */
#define VIN A0 // define the Arduino pin A0 as voltage input (V in)
const float VCC   = 5.0;// supply voltage is from 4.5 to 5.5V. Normally 5V.
const int model = 2;   // enter the model number (see below)

float cutOffLimit = 1.01;// set the current which, below that value, doesn't matter. Or set 0.5

/*
          "ACS712ELCTR-05B-T",// for model use 0
          "ACS712ELCTR-20A-T",// for model use 1
          "ACS712ELCTR-30A-T"// for model use 2  
The sensitivity array is holding the sensitivity of the ACS712
current sensors. Do not change. All values are from page 5 of the data sheet          
*/
float sensitivity[] ={
          0.185,// for ACS712ELCTR-05B-T
          0.100,// for ACS712ELCTR-20A-T
          0.066// for ACS712ELCTR-30A-T
     
         }; 


const float QOV =   0.5 * VCC;// set quiescent Output voltage of 0.5V
float voltage;// internal variable for voltage

void setup() {
    //Robojax.com ACS712 Current Sensor 
    Serial.begin(9600);// initialize serial monitor
    Serial.println("Robojax Tutorial");
    Serial.println("ACS712 Current Sensor");
}

void loop() {
  

  //Robojax.com ACS712 Current Sensor 
  float voltage_raw =   (5.0 / 1023.0)* analogRead(VIN);// Read the voltage from sensor
  voltage =  voltage_raw - QOV + 0.012 ;// 0.000 is a value to make voltage zero when there is no current
  float current = voltage / sensitivity[model];
 
  if(abs(current) > cutOffLimit ){
    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.println("A");
  }else{
    Serial.println("No Current");
  }
  delay(500);
}

资源与参考

尚无可用资源。

文件📁

数据手册 (pdf)

Fritzing 文件