ArduinoでAllegro ACS758電流センサーとSSD1306 OLEDディスプレイを使って電流を測定する
このチュートリアルでは、Allegro ACS758 電流センサーを使って電流を測定し、その結果を SSD1306 OLED ディスプレイに表示する手順を案内します。本プロジェクトでは、回路を流れる電流を読み取り、そのデータを OLED 画面上に視覚的に表現して簡単に監視できるようにします。部品の接続方法、コードの記述方法、そして各要素がどのように連携するかを学びます。


コードとその機能を理解したい場合は、詳細な説明を見るためにビデオ(02:45)を必ず確認してください。
ハードウェアの解説
このプロジェクトの主な構成要素は、Allegro社のACS758電流センサーとSSD1306 OLEDディスプレイです。ACS758はホール効果型の電流センサーで、導体を流れる電流を高精度で測定し、その電流に比例した出力電圧を提供します。最大200Aまでの電流に対応し、3.3Vまたは5Vで動作します。
SSD1306 OLEDディスプレイは、I2Cで通信する小型かつ低消費電力のディスプレイです。統合が容易で、グラフィックやテキストを鮮明に表示できるため、Arduinoのプロジェクトで一般的に使われています。これらのコンポーネントを組み合わせることで、さまざまな用途で電流を監視するための強力なツールになります。
データシートの詳細
| 製造元 | アレグロ・マイクロシステムズ |
|---|---|
| 部品番号 | ACS758ECB-200U |
| ロジック/入出力電圧 | 3.3 V / 5 V |
| 電源電圧 | 5 V |
| 出力電流(チャンネルあたり) | 200 A |
| ピーク電流(チャンネルあたり) | 200 A |
| PWM周波数に関するガイダンス | 該当なし |
| 入力ロジック閾値 | 0.3 V(低)、2.7 V(高) |
| 電圧降下 / RDS(オン時)/ 彩度 | 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ピンをGNDに接続します。OLEDのSDAピンをArduinoのSDAピン(ほとんどのArduinoボードではA4)に、OLEDのSCLピンをArduinoのSCLピン(ほとんどのArduinoボードではA5)に接続します。信頼性の高い動作のため、すべての接続が確実に行われていることを確認してください。
コード例と解説
#define VIN A0 // define the Arduino pin A0 as voltage input (V in)
const float VCC = 5.0; // supply voltage 5V or 3.3V
const int model = 2; // enter the model (see below)コード内では、変数VINアナログピンA0に割り当てられており、ACS758センサーの電圧を読み取ります。そのVCC変数は供給電圧を設定し、そしてmodel変数は使用されているACS758モデルを定義し、感度および出力電圧の計算に影響します。
void loop() {
float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // Read the voltage from sensor
float current = voltage / FACTOR; // Calculate the current based on voltage
}〜の中でloop()関数内で、コードは〜を使ってセンサーから生の電圧を読み取りますanalogRead(VIN)定義された感度に基づいてそれを電流値に変換します。これにより回路を流れる電流をリアルタイムで監視できます。
if(abs(voltage) > cutOff) {
display.clearDisplay();
robojaxText("Current:", 0, 22, 2, false);
}この条件式は、電圧の絶対値が指定された値より大きいかどうかを確認します。cutOfflimit。true の場合、表示をクリアして 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);
}
必要かもしれないもの
-
アマゾンAmazonでOLED 128x32を購入してください。amzn.to
-
アマゾンAmazonでSSD1306 OLEDを購入するamzn.to
-
アリエクスプレスAliExpressからSSD1306 OLED 128x32を購入するs.click.aliexpress.com
リソースと参考文献
まだリソースはありません。
ファイル📁
Arduinoライブラリ(zip)
-
SSD1306-OLED-Adafruit_SSD1306-master
robojax-SSD1306-OLED-Adafruit_SSD1306-master.zip0.02 MB
フリッツィングファイル
-
OLEDモノ 0.56インチ 128x32
OLED Mono 0.56 inch 128x32.fzpz0.01 MB -
SSD1306 0.96インチ 128x64 I2C モノクロ OLED ディスプレイ
SSD1306 0.96in 128x64 I2C Monochrome OLED Display.fzpz0.01 MB
ユーザーマニュアル
-
SSD1306 OLEDマニュアル
robojax-SSD1306-OLED-manual.pdf1.79 MB