搜索代码

使用ACS712通过I2C在LCD1602或LCD2004上显示电流

使用ACS712通过I2C在LCD1602或LCD2004上显示电流

在本教程中,我们将学习如何使用Allegro ACS712电流传感器测量交流和直流电流,并通过I2C在LCD1602或LCD2004上显示读数。这个项目特别适用于监控各种应用中的电流,提供实时电流数据的视觉显示。到最后,您将拥有一个可以准确显示LCD电流读数的工作设置。

LCD1602-I2C display module with 4 wires

有关接线和代码的说明,请务必查看视频中的指定时间戳(视频在 00:00)。

硬件解释

该项目的主要组件是ACS712电流传感器,能够测量高达30A的电流水平。它通过利用霍尔效应将流过它的电流转换为成比例的电压输出来工作。Arduino可以读取该电压输出以计算电流水平。传感器有三个连接引脚:VCC用于电源,GND用于接地,OUT用于电压信号。

LCD1602或LCD2004显示器通过I2C连接,这使得布线和控制更加简便,只需要两个数据引脚(SDA和SCL),再加上电源和接地。这使得在不杂乱布线的情况下,简单显示当前读数。

数据表详情

制造商阿莱格罗微系统
零件编号ACS712ELCTR-30A-T
逻辑/IO电压4.5-5.5 伏
供电电压5 V
每个通道的输出电流30 A
PWM频率指导N/A
输入逻辑阈值0.5 V(静态输出)
电压降 / RDS(开)/ 饱和度1.2 毫欧
热极限150 °C 最高
包裹TO-220

  • 确保电流在20A以上时有适当的散热。
  • 使用适当的线规(30A 使用 12 AWG)以避免过热。
  • 将VCC连接到5V,将GND连接到地以确保正常运行。
  • 确保OUT引脚连接到Arduino上的模拟输入引脚(例如,A0)。
  • 注意不同ACS712型号的灵敏度差异。
  • 根据数据表的规定使用电容器进行解耦。

接线说明

Arduino wiring for Allegro ACS712 current sensor with LCD

将ACS712电流传感器与Arduino和LCD连接,首先将ACS712的VCC引脚连接到Arduino的5V引脚。接下来,将ACS712的GND引脚连接到Arduino的地线引脚。ACS712的OUT引脚应连接到模拟引脚。A0在Arduino上,电压信号将被读取。

对于LCD1602或LCD2004,将VCC引脚连接到5V引脚,将GND引脚连接到地。LCD的SDA引脚应连接到Arduino上的SDA引脚(通常A4在 UNO 上,SCL 引脚应连接到 Arduino 上的 SCL 引脚(通常)A5在UNO上)。确保所有连接稳固,以确保正常功能。

代码示例和操作步骤

在代码中,我们定义了模拟输入引脚用于读取传感器输出。#define VIN 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.

接下来,我们根据使用的ACS712型号设置灵敏度。灵敏度值存储在一个数组中,以便根据型号选择方便访问。

const int model = 2; // enter the model number (0, 1, or 2 for 5A, 20A, or 30A)
float sensitivity[] = {0.185, 0.100, 0.066}; // sensitivity values for each model

在的setup()功能,我们初始化串口监视器和LCD显示器。这使我们能够通过LCD和串口监视器查看当前读数,以便进行调试。

void setup() {
    Serial.begin(9600); // initialize serial monitor
    lcd.begin(); // initialize the LCD
    lcd.backlight(); // turn on the backlight
    lcd.print("Robojax ACS712"); // display initial text
    delay(2000); // wait for 2 seconds
}

loop()该函数读取传感器的电压,根据灵敏度计算电流,并相应地显示结果。如果电流超过指定的截止限值,它将显示该值;否则,它会指示未检测到电流。

演示 / 期待什么

开通电路后,LCD 应显示“Robojax ACS712”,接着显示“电流:”以及以安培为单位的测量电流值。如果电流低于截止限值,显示屏将显示“无电流”。在用高电流测试时要小心,因为传感器可能会发热(视频中在 12:00)。

为了测试设置,逐渐增加通过ACS712的电流,并观察LCD和串行监视器上的变化。确保电流不超过传感器的额定限制,以避免损坏。

图像

Allegro ACS712 DC/AC电流传感器与LCD1602/LCD2004 I2C及Arduino代码
Allegro ACS712 DC/AC电流传感器与LCD1602/LCD2004 I2C及Arduino代码
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
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
Arduino wiring for Allegro ACS712 current sensor with LCD
Arduino wiring for Allegro ACS712 current sensor with LCD
131-Allegro ACS712 DC/AC current sensor with LCD1602/LCD2004 I2C and Arduino code
语言: C++
/*
 * 
 * Arduino Sketch for Allegro ACS712 Current Sensor with LCD1602/LCD2004 with I2C 
 * This sensor can measure current at a range of up to 30A.
 * The current is measured and displayed on LCD1602 or LCD2004.
 * The current is also displayed on the Serial monitor (click Tools->Serial monitor).
 * It operates with 5V.
 * Please watch the video instruction and explanation for this code.
 * 
 * Written by Ahmad Shamshiri on July 14, 2018 at 07:02 in Ajax, Ontario, Canada
 * for Robojax.com
 * View the video instruction at https://youtu.be/kzXouSzvcp8
 * This code has been downloaded from Robojax.com
 
  * You must watch the following two videos before you can understand this:
 * 1- Introduction to ACS712 Current Sensor: https://www.youtube.com/watch?v=DVp9k3xu9IQ
 * 2- LCD1602 with I2C display: https://www.youtube.com/watch?v=q9YC_GVHy5A
 */
#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  
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


// start of settings for LCD1602 with I2C
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// end of settings for LCD1602 with I2C

void setup() {
    //Robojax.com ACS712 Current Sensor 
    Serial.begin(9600);// initialize serial monitor
    Serial.println("Robojax Tutorial");
    Serial.println("ACS712 Current Sensor");
  // initialize the LCD
  lcd.begin();  
  lcd.backlight();
  lcd.print("Robojax ACS712");
  lcd.setCursor(0,1);
  lcd.print("Demo"); 
  delay(2000);     
}

void loop() {
  

  //Robojax.com ACS712 Current Sensor with LCD1601
  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");
    //start of loop Robojax code ACS712 with LCD1602 and I2C
    lcd.clear();
    lcd.setCursor (0,0); // set to line 1, char 0  
    lcd.print("Robojax ACS712");
    lcd.setCursor (0,1); // set to line 1, char 0  
    lcd.print("Current: ");
    lcd.setCursor (9,1); // go to start of 2nd line
    lcd.print(current);
    lcd.setCursor (14,1); // go to start of 2nd line
    lcd.print("A");
 
  }else{
    Serial.println("No Current");
    lcd.clear();
    lcd.setCursor (0,0); // set to line 1, char 0 
    lcd.print("No Current");     
  }
  delay(500);
}

|||您可能需要的东西

文件📁

Arduino 库(zip 格式)

数据手册 (pdf)

Fritzing 文件