使用 Allegro ACS758 电流传感器配合 LCD1602 为 Arduino 测量电流
呢個項目示範咗點樣用 Allegro ACS758 電流傳感器嚟量度電流,並且將讀數顯示喺連接 Arduino 嘅 LCD1602 屏幕上。ACS758 係一款多功能傳感器,可以量度高達 200 安培嘅電流,適合各種應用。
呢個項目提供咗一個實用嘅方法嚟監測電路入面嘅電流流動,呢樣嘢對好多應用嚟講都好重要,包括:
- 監測電器嘅電力消耗
- 建立電池管理系統
- 設計電動摩打控制器
- 製作以電流為基礎嘅保安系統
呢個指南會帶你了解所需嘅硬件、接線、程式碼,以及一個現場示範。
硬件針腳
硬件/組件
要整呢個項目,你需要以下組件:
- Arduino Uno(或兼容板)
- Allegro ACS758 電流傳感器(具體型號會決定最大電流量度範圍;記得要相應調整程式碼。(喺影片 00:14 同 03:18))
- 冇 I2C 模組嘅 LCD1602 顯示屏,呢個 LCD 有 12 條線
- 連接線
- 電源(5V)
- 負載(用嚟測試電流量度)
接線指南

LCD1602 嘅接線喺另一個影片入面有解釋(喺影片 01:24)。呢個項目嘅主要連接如下(喺影片 01:24):
- ACS758:VCC 接 5V,GND 接 GND,信號輸出(黃色線)接 Arduino 嘅 A0。
- ACS758 傳感器嘅兩條主線要同負載串聯連接(喺影片 02:09)。
程式碼解釋
Arduino 程式碼分為兩個主要部分:一個負責處理 ACS758 傳感器,另一個負責同 LCD1602 互動。程式碼入面用戶可以設定嘅部分包括:
#define VIN A0 // 將 Arduino 嘅 A0 針腳定義為電壓輸入(V in)
const float VCC = 5.0;// 供電電壓 5V 或 3.3V。如果用 PCB,只可以設為 5V。
const int model = 2; // 輸入型號(見下面)
float cutOffLimit = 1.00;// 讀數截止電流。1.00 即係 1 安培
model 變數需要根據所用嘅 ACS758 具體型號嚟設定(喺影片 03:18)。請參考程式碼註解入面嘅型號對應表。cutOffLimit 變數決定要顯示嘅最低電流(喺影片 03:48)。調整呢個數值可以過濾啲冇意義嘅讀數。
現場項目/示範
影片示範咗點樣連接電流錶嚟量度流經負載嘅電流(喺影片 02:18)。程式碼會將電流同電壓讀數顯示喺 LCD1602 同序列監視器上面(喺影片 07:02)。示範顯示咗當負載電流改變時,讀數會點樣動態更新(喺影片 07:14)。佢亦都強調咗當電流低過指定限制時,會顯示「No Current」(喺影片 07:59)。
章節
- [00:06] 簡介
- [00:34] 先決條件
- [01:24] 接線解釋
- [02:18] 電流量度示範
- [02:57] 程式碼解釋
- [07:02] 現場示範
- [08:18] 總結
109-Allegro ACS758 current sensor with LCD1602 for Arduino
語言: C++
/*
*
* Arduino Sketch for Allegro ACS758 Current Sensor with LCD1602
* This sensor can measure current at a range of up to 200A.
* It operates with 3.3V or 5V.
* This sketch requires you to watch the following two videos before using this code:
* 1- ACS758 Sensor https://www.youtube.com/watch?v=SiHfjzcqnU4
* 2- LCD1602 Display https://www.youtube.com/watch?v=S4ya3Q7uhJs
*
*
* Written by Ahmad Shamshiri on Tuesday, June 22, 2018 at 17:57 in Ajax, Ontario, Canada
* for Robojax.com
* View the video instruction for this code at https://youtu.be/Co-DrCa2slk
* 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 5V or 3.3V. If using PCB, set to 5V only.
const int model = 2; // enter the model (see below)
float cutOffLimit = 1.00;// reading cutoff current. 1.00 is 1 Amper
/*
"ACS758LCB-050B",// for model use 0
"ACS758LCB-050U",// for model use 1
"ACS758LCB-100B",// for model use 2
"ACS758LCB-100U",// for model use 3
"ACS758KCB-150B",// for model use 4
"ACS758KCB-150U",// for model use 5
"ACS758ECB-200B",// for model use 6
"ACS758ECB-200U"// for model use 7
Sensitivity array is holding the sensitivity of the ACS758
current sensors. Do not change.
*/
float sensitivity[] ={
40.0,// for ACS758LCB-050B
60.0,// for ACS758LCB-050U
20.0,// for ACS758LCB-100B
40.0,// for ACS758LCB-100U
13.3,// for ACS758KCB-150B
16.7,// for ACS758KCB-150U
10.0,// for ACS758ECB-200B
20.0,// for ACS758ECB-200U
};
/*
* Quiescent output voltage is a factor of VCC that appears at the output
* when the current is zero.
* For bidirectional sensors it is 0.5 x VCC.
* For unidirectional sensors it is 0.12 x VCC.
* For model ACS758LCB-050B, the B at the end represents Bidirectional (polarity doesn't matter).
* For model ACS758LCB-100U, the U at the end represents Unidirectional (polarity must match).
* Do not change.
*/
float quiescent_Output_voltage [] ={
0.5,// for ACS758LCB-050B
0.12,// for ACS758LCB-050U
0.5,// for ACS758LCB-100B
0.12,// for ACS758LCB-100U
0.5,// for ACS758KCB-150B
0.12,// for ACS758KCB-150U
0.5,// for ACS758ECB-200B
0.12,// for ACS758ECB-200U
};
const float FACTOR = sensitivity[model]/1000;// set sensitivity for selected model
const float QOV = quiescent_Output_voltage [model] * VCC;// set quiescent Output voltage for selected model
float voltage;// internal variable for voltage
float cutOff = FACTOR/cutOffLimit;// convert current cut off to mV
//************ END of ACS785 Settings
//******************** Start of LCD1602
// January 21, 2018 14:25
// original source https://www.arduino.cc/en/Tutorial/HelloWorld
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// Robojax.com ACS758 Current Sensor with LCD1602
Serial.begin(9600);// initialize serial monitor
Serial.println("Robojax Tutorial");
Serial.println("ACS758 Current Sensor");
Serial.println("with LCD1602 display");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.clear();
lcd.print("Robojax");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("ACS758 Current Sensor");
delay(2000);
lcd.clear();
}
void loop() {
// Robojax.com ACS758 Current Sensor with LCD1602
float voltage_raw = (5.0 / 1023.0)* analogRead(VIN);// Read the voltage from sensor
voltage = voltage_raw - QOV + 0.007 ;// 0.007 is a value to make voltage zero when there is no current
float current = voltage / FACTOR;
if(abs(voltage) > cutOff ){
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 ACS758 with LCD1602 and I2C
lcd.clear();
lcd.setCursor (0,0); // set to line 1, char 0
lcd.print("Current: ");
lcd.setCursor (9,0); // go to start of 2nd line
lcd.print(current);
lcd.setCursor (15,0); // go to start of 2nd line
lcd.print("A");
lcd.setCursor (0,1);
lcd.print("Sense V: ");
lcd.setCursor (9,1); // go to start of 2nd line
lcd.print(voltage);
lcd.setCursor (15,1); // go to start of 2nd line
lcd.print("V");
//end of loopcode Robojax code ACS758 with LCD1602 and I2C
}else{
Serial.println("No Current");
lcd.clear();
lcd.setCursor (0,0);
lcd.print("No Current");
}
delay(500);
// Robojax.com ACS758 Current Sensor with LCD1602
}
你可能需要嘅嘢
-
阿里巴巴速卖通Purchase Allegro ACS758 Current Sensor from AliExpresss.click.aliexpress.com
文件📁
其他文件
-
Alegro ACS Current LibraryArduino library for Allegro ACS current sensor modules. Provides functions to read current measurements from ACS712 and similar sensors.
robojax_Alegro_ACS_Current_library.zip0.02 MB
