ค้นหาโค้ด

การวัดกระแสไฟฟ้าโดยใช้เซนเซอร์วัดกระแส Allegro ACS758 ร่วมกับจอ LCD1602 (I2C) สำหรับ Arduino

การวัดกระแสไฟฟ้าโดยใช้เซนเซอร์วัดกระแส Allegro ACS758 ร่วมกับจอ LCD1602 (I2C) สำหรับ Arduino


ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีการวัดกระแสไฟฟ้าโดยใช้เซนเซอร์วัดกระแส Allegro ACS758 และแสดงค่าบนจอ LCD1602 โดยใช้การสื่อสารแบบ I2C การตั้งค่านี้มีประโยชน์สำหรับการใช้งานต่างๆ เช่น การตรวจสอบการใช้พลังงานในวงจร หรือการจัดการการใช้แบตเตอรี่ในอุปกรณ์ เมื่อสิ้นสุดโปรเจกต์นี้ คุณจะมีระบบที่ทำงานได้จริงซึ่งแสดงค่ากระแสไฟฟ้าแบบเรียลไทม์

LCD1602-I2C display module with 4 wires

เซนเซอร์วัดกระแส ACS758 สามารถวัดกระแสได้สูงสุดถึง 200A ทำให้เหมาะสำหรับการใช้งานที่มีกำลังสูง จอแสดงผล LCD1602 จะแสดงค่ากระแสเป็นแอมแปร์พร้อมกับแรงดันไฟฟ้าที่สอดคล้องกันซึ่งเซนเซอร์วัดกระแสตรวจจับได้ เพื่อให้เข้าใจขั้นตอนต่างๆ ชัดเจนยิ่งขึ้น โปรดดูวิดีโอ (ในวิดีโอที่เวลา 00:00) สำหรับการสาธิตด้วยภาพ

อธิบายฮาร์ดแวร์

ส่วนประกอบหลักของโปรเจกต์นี้ประกอบด้วยเซนเซอร์วัดกระแส Allegro ACS758 และจอแสดงผล LCD1602 พร้อม I2C ACS758 ทำงานโดยการวัดสนามแม่เหล็กที่เกิดจากกระแสไฟฟ้าที่ไหลผ่านตัวนำ โดยจะให้แรงดันเอาต์พุตที่เป็นสัดส่วนกับกระแสที่ไหล ซึ่ง Arduino สามารถอ่านค่าได้ง่าย

LCD1602 เป็นจอแสดงผลตัวอักษรขนาด 16x2 ที่ใช้การสื่อสารแบบ I2C ทำให้สามารถเชื่อมต่อและควบคุมได้ง่ายด้วยพินข้อมูลเพียงสองพิน ซึ่งช่วยลดจำนวนสายไฟที่ต้องใช้ ทำให้กระบวนการเดินสายง่ายขึ้น อินเทอร์เฟซ I2C ช่วยให้อุปกรณ์หลายตัวสามารถสื่อสารผ่านบัสเดียวกันได้ ทำให้เป็นตัวเลือกที่หลากหลายสำหรับโปรเจกต์ต่างๆ

รายละเอียดจาก Datasheet

ผู้ผลิตAllegro Microsystems
หมายเลขชิ้นส่วนACS758
แรงดันไฟฟ้าลอจิก/IO3.3V / 5V
แรงดันไฟฟ้าจ่าย5V
กระแสเอาต์พุต (ต่อช่อง)สูงสุด 200 A
กระแสสูงสุด (ต่อช่อง)200 A
คำแนะนำความถี่ PWMไม่มี
เกณฑ์ลอจิกอินพุต0.3V (ต่ำ), 2.7V (สูง)
แรงดันตก / RDS(on) / อิ่มตัวสูงสุด 0.5V
ขีดจำกัดความร้อนสูงถึง 150°C
แพ็คเกจTO-220
หมายเหตุ / รุ่นต่างๆมีตัวเลือกแบบสองทิศทางและทิศทางเดียว

  • ตรวจสอบให้แน่ใจว่ามีการระบายความร้อนที่เหมาะสมสำหรับการใช้งานกระแสสูง
  • แยกแหล่งจ่ายไฟเพื่อหลีกเลี่ยงสัญญาณรบกวนที่ส่งผลต่อการอ่านค่า
  • ใช้สายคู่ตีเกลียวสำหรับเส้นทางกระแสเพื่อลดการรบกวน
  • ตรวจสอบแรงดันเอาต์พุตที่สอดคล้องกับกระแสศูนย์เพื่อการปรับเทียบ
  • ตรวจสอบขั้วเมื่อใช้เซนเซอร์แบบทิศทางเดียว

คำแนะนำการเดินสายไฟ

Arduino wiring for ACS758 with LCD

ในการเดินสายไฟเซนเซอร์วัดกระแส Allegro ACS758 และจอแสดงผล LCD1602 ให้เริ่มต้นด้วยการเชื่อมต่อไฟและกราวด์ เชื่อมต่อพิน VCC ของ ACS758 เข้ากับพิน 5V บน Arduino และเชื่อมต่อพิน GND เข้ากับกราวด์ของ Arduino พินเอาต์พุตสัญญาณ (Vout) จาก ACS758 ควรเชื่อมต่อกับพินอินพุตแอนะล็อก A0 บน Arduino

ถัดไป สำหรับจอแสดงผล LCD1602 ให้เชื่อมต่อพิน VCC เข้ากับพิน 5V บน Arduino และพิน GND เข้ากับกราวด์ สาย SDA (สายข้อมูล) จาก LCD ควรเชื่อมต่อกับพิน A4 ของ Arduino และสาย SCL (สายสัญญาณนาฬิกา) ควรเชื่อมต่อกับพิน A5 การตั้งค่านี้ช่วยให้การสื่อสารแบบ I2C ระหว่าง Arduino และ LCD1602 ทำงานได้

ตัวอย่างโค้ดและคำอธิบาย

สเก็ตช์ Arduino เริ่มต้นด้วยการกำหนดพินอินพุตสำหรับเซนเซอร์วัดกระแส ตัวแปร VIN ถูกกำหนดเป็น A0 ซึ่งเป็นตำแหน่งที่จะอ่านค่าเอาต์พุตของเซนเซอร์ แรงดันไฟฟ้าจ่ายและรุ่นของ ACS758 ก็ถูกกำหนดเช่นกัน ความไวสำหรับรุ่นที่เลือกจะคำนวณโดยใช้อาร์เรย์ sensitivity

#define VIN A0 // กำหนดพิน A0 ของ Arduino เป็นอินพุตแรงดันไฟฟ้า (V in)
const float VCC = 5.0; // แรงดันไฟฟ้าจ่าย 5V หรือ 3.3V
const int model = 2; // ระบุรุ่น (ดูด้านล่าง)

ในฟังก์ชัน setup() จอ LCD จะถูกเริ่มต้นและแสดงข้อความต้อนรับ ไฟแบ็คไลท์จะถูกเปิดเพื่อให้มองเห็นได้ชัดเจนขึ้น นี่คือจุดที่คุณสามารถปรับแต่งข้อความแสดงผลเริ่มต้นให้เหมาะกับโปรเจกต์ของคุณได้

void setup() {
    Serial.begin(9600); // เริ่มต้นซีเรียลมอนิเตอร์
    lcd.begin(); // เริ่มต้นจอ LCD
    lcd.backlight(); 
    lcd.print("Robojax");
    lcd.setCursor (0,1); // ไปที่จุดเริ่มต้นของบรรทัดที่ 2 
    lcd.print("ACS758 Current Sensor"); 
    delay(2000); // หน่วงเวลา 2 วินาที
    lcd.clear();  
}

ในฟังก์ชัน loop() แรงดันไฟฟ้าจากเซนเซอร์จะถูกอ่านและประมวลผลเพื่อคำนวณกระแส หากแรงดันไฟฟ้าเกินขีดจำกัดที่กำหนดไว้ กระแสจะถูกแสดงบนทั้งซีเรียลมอนิเตอร์และจอ LCD หากไม่ตรวจพบกระแส ข้อความ "No Current" จะถูกแสดง ซึ่งช่วยให้สามารถตรวจสอบการไหลของกระแสได้แบบเรียลไทม์

void loop() {
  float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // อ่านแรงดันไฟฟ้าจากเซนเซอร์
  float current = voltage / FACTOR;
  if (abs(voltage) > cutOff) {
    Serial.print("V: "); Serial.print(voltage, 3); // พิมพ์แรงดันไฟฟ้า
    lcd.print(current); // แสดงกระแสบนจอ LCD
  } else {
    Serial.println("No Current"); // แสดงผลเมื่อไม่ตรวจพบกระแส
  }
  delay(500); // รอก่อนการอ่านค่าครั้งถัดไป
}

โปรดดูคำอธิบายโค้ดแบบเต็มในวิดีโอ (ในวิดีโอที่เวลา 01:30) สำหรับคำอธิบายโดยละเอียดของแต่ละส่วน

การสาธิต / สิ่งที่คาดหวัง

เมื่อเปิดเครื่อง ระบบจะแสดงกระแสที่ไหลผ่านเซนเซอร์บนจอ LCD แบบเรียลไทม์ คุณสามารถทดสอบการตั้งค่าได้โดยการจ่ายกระแสที่ทราบค่าแล้วสังเกตค่าที่อ่านได้ หากเซนเซอร์ตรวจพบกระแส ค่าแรงดันและกระแสจะอัปเดตบนจอแสดงผล หากไม่มีกระแส ข้อความ "No Current" จะปรากฏขึ้น ควรระมัดระวังการต่อขั้วกลับด้าน ซึ่งอาจทำให้ค่าที่อ่านได้ไม่ถูกต้องหรือทำให้ชิ้นส่วนเสียหายได้

ภาพ

LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
ACS758-sensor-0
ACS758-sensor-0
ACS758-sensor-1
ACS758-sensor-1
ACS758-sensor-3
ACS758-sensor-3
ACS758-sensor-4
ACS758-sensor-4
Arduino wiring for ACS758 with LCD
Arduino wiring for ACS758 with LCD
108-Allegro ACS758 current sensor with LCD1602 (I2C) for Arduino
ภาษา: C++
/*
 * 
 * Arduino Sketch for Allegro ACS758 Current Sensor with LCD1602 & I2C module
 * 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 with I2C https://www.youtube.com/watch?v=q9YC_GVHy5A
 * 
 * 
 * Written by Ahmad Shamshiri on Tuesday, June 19, 2018, at 20:40 in Ajax, Ontario, Canada
 * for Robojax.com
 * View the video instruction for this code at https://youtu.be/tug9wjCwDQA
 * 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 cut-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   
The 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

// ======== start of LCD1602 with i2C settings
#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  LCD1602 with i2C settings
void setup() {
    //Robojax.com ACS758 Current Sensor 
    Serial.begin(9600);// initialize serial monitor
    Serial.println("Robojax Tutorial");
    Serial.println("ACS758 Current Sensor");
    Serial.println("with LCD1602 & I2C");    
    // initialize the LCD, 
    lcd.begin();   
  // Turn on the backlight and print a message.
  lcd.backlight(); 
    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 code ACS758 with LCD1602 and I2C
  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");   
  lcd.backlight();
 //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);
}

สิ่งที่คุณอาจต้องการ

แหล่งข้อมูลและเอกสารอ้างอิง

ยังไม่มีทรัพยากรเลย

ไฟล์📁

ไฟล์ Fritzing

ไฟล์อื่น ๆ

  • Alegro ACS Current Library
    Arduino library for Allegro ACS current sensor modules. Provides functions to read current measurements from ACS712 and similar sensors.
    robojax_Alegro_ACS_Current_library.zip 0.02 MB