使用 Allegro ACS758 电流传感器配合 SSD1306 OLED 显示屏测量电流(适用于 Arduino)
呢個教學會帶你了解點樣用 Allegro ACS758 電流傳感器嚟量度電流,並且將結果顯示喺 SSD1306 OLED 顯示屏上面。呢個項目會涉及讀取流經電路嘅電流,並且將數據視覺化咁顯示喺 OLED 屏幕上,方便監察。你會學到點樣連接元件、寫代碼,同埋了解各部分點樣一齊運作。


如果你想知道代碼同佢嘅功能,記得睇吓條片,入面有更詳細嘅解釋(片入面 02:45 位置)。
硬件解釋
呢個項目嘅主要元件包括 Allegro ACS758 電流傳感器同 SSD1306 OLED 顯示屏。ACS758 係一個霍爾效應電流傳感器,可以高精度咁量度流經導體嘅電流,並且提供一個同電流成比例嘅輸出電壓。佢可以處理高達 200A 嘅電流,並且可以喺 3.3V 或者 5V 電壓下運作。
SSD1306 OLED 顯示屏係一個細細哋、低功耗嘅顯示屏,透過 I2C 通訊。佢成日都用喺 Arduino 項目入面,因為容易整合,而且可以清晰咁顯示圖形同文字。呢啲元件夾埋一齊,就成為一個強勁嘅工具,可以用嚟監察唔同應用入面嘅電流。
數據手冊細節
| 製造商 | Allegro Microsystems |
|---|---|
| 零件編號 | ACS758ECB-200U |
| 邏輯/IO 電壓 | 3.3 V / 5 V |
| 供電電壓 | 5 V |
| 輸出電流(每通道) | 200 A |
| 峰值電流(每通道) | 200 A |
| PWM 頻率指引 | 唔適用 |
| 輸入邏輯閾值 | 0.3 V(低),2.7 V(高) |
| 電壓降 / RDS(on) / 飽和 | 0.05 V |
| 熱限制 | -40 至 125 °C |
| 封裝 | SOIC-8 |
| 備註 / 變體 | 有雙向同單向型號可供選擇 |
- 確保 ACS758 傳感器方向正確,先至可以讀得準。
- 如果接近最大電流極限運作,就要用散熱器,防止過熱。
- 將傳感器嘅電源去耦,確保運作穩定。
- 檢查接線,避免浮空輸入導致讀數出錯。
- 校準傳感器輸出,確保電流量度準確。
接線指示

要接駁 Allegro ACS758 電流傳感器同 SSD1306 OLED 顯示屏,首先連接電源。將 ACS758 嘅 VCC 腳連去 Arduino 嘅 5V 輸出,然後將 GND 腳連去 Arduino 嘅接地(GND)。ACS758 嘅輸出信號腳(Vout)就要連去 Arduino 嘅模擬輸入腳 A0。
跟住,對於 SSD1306 OLED 顯示屏,將 VCC 腳連去 Arduino 嘅 5V 輸出,GND 腳連去接地。將 OLED 嘅 SDA 腳連去 Arduino 嘅 SDA 腳(大部分 Arduino 板係 A4),然後將 OLED 嘅 SCL 腳連去 SCL 腳(大部分 Arduino 板係 A5)。確保所有連接都穩固,先至可以可靠咁運作。
代碼示例同講解
#define VIN A0 // 將 Arduino 腳 A0 定義為電壓輸入(V in)
const float VCC = 5.0; // 供電電壓 5V 或者 3.3V
const int model = 2; // 輸入型號(睇下面)
喺代碼入面,變數 VIN 被指派去模擬腳 A0,用嚟讀取 ACS758 傳感器嘅電壓。VCC 變數設定供電電壓,而 model 變數就定義用緊邊個 ACS758 型號,呢個會影響靈敏度同輸出電壓嘅計算。
void loop() {
float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // 讀取傳感器嘅電壓
float current = voltage / FACTOR; // 根據電壓計算電流
}
喺 loop() 函數入面,代碼用 analogRead(VIN) 讀取傳感器嘅原始電壓,然後根據定義嘅靈敏度將佢轉換成電流值。咁就可以實時監察流經電路嘅電流。
if(abs(voltage) > cutOff) {
display.clearDisplay();
robojaxText("Current:", 0, 22, 2, false);
}
呢個條件語句會檢查電壓嘅絕對值係咪大過 cutOff 限制。如果係真,就會清除顯示屏,並且將電流讀數更新到 OLED 上面。咁樣就可以確保只有顯著嘅電流值先至會顯示出嚟,避免顯示屏太亂。
示範 / 預期效果
當你運行個程式,OLED 顯示屏會實時顯示量度緊嘅電流。如果冇電流流動,顯示屏會顯示「No Current」。確保接線正確,避免好似極性反轉呢啲問題,否則可能會導致讀數唔準(片入面 05:30 位置)。
/*
* Arduino Sketch for Allegro ACS758 Current Sensor with SSD1306 OLED Display
* can be used with 128x64 or 128x32 OLED displays with SSD1306 chip
* this sensor can measure current at a range of up to 200A
* It operates with 3.3V or 5V
* This video requires you to watch the following 2 videos before using this code:
* 1- ACS758 Sensor https://www.youtube.com/watch?v=SiHfjzcqnU4
* 2- SSD1306 OLED Display https://www.youtube.com/watch?v=UmYiHTOz-5k
*
* View the video instructions for this code at https://youtu.be/aHg9UrK9s9c
* Written by Ahmad Shamshiri for Robojax Video channel www.Robojax.com
* Date: June 24, 2018, at 21:44 in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: This code is "AS IS" and for educational purposes only.
* This code has been downloaded from https://robojax.com
*
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 64
#define LOGO16_GLCD_WIDTH 128
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
///*** ACS758 settings starts
#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 cutt off 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
//**** ACS758 settings ends
void setup() {
Serial.begin(9600);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
// init done
}
void loop() {
//Robojax.com ACS758 Current Sensor values start
float voltage_raw = (5.0 / 1023.0)* analogRead(VIN);// Read the voltage from sensor
voltage = voltage_raw - QOV + 0.003 ;// 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");
display.clearDisplay();
robojaxText("V:", 0, 0, 2, false);
String voltageS = String(voltage);// get voltage and convert it to string for display
robojaxText(voltageS +"V", 20, 0, 2, false);
robojaxText("Current:", 0, 22, 2, false);
String currentS = String(current);// get current and convert it to string for display
robojaxText(currentS +"A", 0, 40, 3, false);
display.display();
}else{
Serial.println("No Current");
display.clearDisplay();
robojaxText("No Current", 0, 0, 2, false);
display.display();
}
//Robojax.com ACS758 Current Sensor values end
delay(500);
}
/*
* robojaxText(String text, int x, int y,int size, boolean d)
* text is the text string to be printed
* x is the integer x position of the text
* y is the integer y position of the text
* size is the text size, 1, 2, 3 etc
* d is either true or false. Use true to display immediately.
*/
void robojaxText(String text, int x, int y,int size, boolean d) {
display.setTextSize(size);
display.setTextColor(WHITE);
display.setCursor(x,y);
display.println(text);
if(d){
display.display();
}
delay(100);
}
你可能需要嘅嘢
-
亞馬遜
-
亞馬遜Purchase OLED 128x32 from Amazonamzn.to
-
阿里巴巴速卖通Purchase SSD1306 OLED 128x32 from AliExpresss.click.aliexpress.com
資源與參考資料
暫時冇資源。
文件📁
Arduino 函式庫 (zip)
-
SSD1306-OLED-Adafruit_SSD1306-master
robojax-SSD1306-OLED-Adafruit_SSD1306-master.zip0.02 MB
数据表 (pdf)
-
SSD1306 display datasheetSSD1306 is a single-chip CMOS OLED/PLED driver with controller for organic / polymer light emitting diode dot-matrix graphic display system. It consists of 128 segments and 64commons. This IC is designed for Common Cathode type OLED panel. The SSD1306 embeds with contrast control, display RAM and oscillator, which reduces the number of external components and power consumption. It has 256-step brightness control. Data/Commands are sent from general MCU through the hardware selectable 6800/8000 series compatible Parallel Interface, I2C interface or Serial Peripheral Interface. It is suitable for many compact portable applications, such as mobile phone sub-display, MP3 player and calculator, etc.
SSD1306_display_datasheet.pdf1.79 MB
Fritzing 文件
-
OLED Mono 0.56 inch 128x32
OLED Mono 0.56 inch 128x32.fzpz0.01 MB -
SSD1306 0.96in 128x64 I2C Monochrome OLED Display
SSD1306 0.96in 128x64 I2C Monochrome OLED Display.fzpz0.01 MB
用户手册
-
SSD1306 OLED manual
robojax-SSD1306-OLED-manual.pdf1.79 MB
其他文件
-
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