ค้นหาโค้ด

อัลเลโกร ACS758 เซนเซอร์วัดกระแสพร้อมจอ LCD และการป้องกันกระแสเกินสำหรับ Arduino

อัลเลโกร ACS758 เซนเซอร์วัดกระแสพร้อมจอ LCD และการป้องกันกระแสเกินสำหรับ Arduino

ในบทช่วยสอนนี้ เราจะเรียนรู้วิธีการใช้เซนเซอร์วัดกระแส Allegro ACS758 ร่วมกับจอ LCD เพื่อแสดงค่ากระแสและ implement การป้องกันกระแสเกิน การตั้งค่านี้ช่วยให้เราสามารถตรวจสอบกระแสและตัดการเชื่อมต่อโหลดโดยอัตโนมัติหากเกินขีดจำกัดที่กำหนดไว้ล่วงหน้า โปรเจกต์นี้รวมทั้งส่วนฮาร์ดแวร์และซอฟต์แวร์เข้าด้วยกันเพื่อสร้างระบบตรวจสอบกระแสที่ใช้งานได้จริง

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

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

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

เซนเซอร์ ACS758 ทำงานบนหลักการที่เรียกว่าการตรวจจับแบบ Hall effect ซึ่งช่วยให้สามารถวัดกระแสได้โดยไม่ต้องสัมผัสทางไฟฟ้าโดยตรง แรงดันเอาต์พุตจะเปลี่ยนแปลงตามปริมาณกระแสที่ไหลผ่าน ซึ่งเป็นวิธีที่ปลอดภัยและมีประสิทธิภาพในการตรวจสอบโหลดไฟฟ้า

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

ผู้ผลิต Allegro MicroSystems
หมายเลขชิ้นส่วน ACS758
แรงดันไฟฟ้าลอจิก/IO 3.3V / 5V
แรงดันไฟฟ้าจ่าย 5V
กระแสเอาต์พุต (ต่อช่อง) สูงสุด 200A
กระแสสูงสุด (ต่อช่อง) 200A
คำแนะนำความถี่ PWM N/A
เกณฑ์ลอจิกอินพุต 0.5 x VCC (แบบสองทิศทาง)
แรงดันตก / RDS(on) / ความอิ่มตัว N/A
ขีดจำกัดความร้อน 150°C
แพ็คเกจ ติดตั้งบน PCB
หมายเหตุ / รุ่นต่างๆ มีหลายรุ่นสำหรับช่วงกระแสที่แตกต่างกัน

 

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

ส่วนประกอบที่จำเป็น

  • เซนเซอร์วัดกระแส ACS758
  • รีเลย์ 12V 100A
  • LCD1602 พร้อม I2C (4 สาย)
  • ทรานซิสเตอร์ 2N2222 หรือ 2N3904
  • ตัวต้านทาน 1k โอห์ม 1/4W หรือกำลังใดๆ
  • แหล่งจ่ายไฟสำหรับรีเลย์
  • แหล่งจ่ายไฟสำหรับโหลดของคุณ
  • เบรดบอร์ด
  • สายจัมเปอร์

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

Arduino wiring for ACS758 current sensor with LCD and protection relay
Arduino wiring for ACS758 current sensor with LCD and protection relay

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

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

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

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

#define VIN A0 // กำหนดพิน Arduino A0 เป็นอินพุตแรงดันไฟฟ้า (V in)
const int relayPin = 2; // ตั้งค่าพินดิจิทัลสำหรับรีเลย์
const float maxCurrent = 15.00; // ตั้งค่ากระแสสูงสุด

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

void setup() {
    pinMode(relayPin, OUTPUT); // ตั้งค่า relayPin เป็นเอาต์พุต
    Serial.begin(9600); // เริ่มต้นซีเรียลมอนิเตอร์
    lcd.begin(); // เริ่มต้น LCD
    lcd.backlight(); // เปิดไฟแบ็คไลท์
    lcd.print("Robojax");
}

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

void loop() {
  float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // อ่านแรงดันไฟฟ้าจากเซนเซอร์
  float current = voltage / FACTOR; // คำนวณกระแส
  if (current >= minCurrent) {
    if (current <= maxCurrent) {
      digitalWrite(relayPin, LOW); // ปิดรีเลย์เพื่อให้กระแสไหล
    } else {
      digitalWrite(relayPin, HIGH); // เปิดรีเลย์เพื่อตัดกระแส
    }
  }
}

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

เมื่อทุกอย่างเชื่อมต่อและอัปโหลดโค้ดเรียบร้อยแล้ว จอ LCD จะแสดงค่ากระแสปัจจุบัน หากกระแสเกินค่า maxCurrent ที่กำหนดไว้ รีเลย์จะทำงานและตัดการเชื่อมต่อโหลด คุณสามารถทดสอบได้โดยค่อยๆ เพิ่มกระแสโหลดและสังเกตการเปลี่ยนแปลงบนจอ LCD และในซีเรียลมอนิเตอร์ ควรหลีกเลี่ยงการเชื่อมต่อขั้วกลับด้าน เพราะอาจทำให้ชิ้นส่วนเสียหายได้ (ในวิดีโอที่เวลา 10:15)

ภาพ

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 current sensor with LCD and protection relay
Arduino wiring for ACS758 current sensor with LCD and protection relay
111-Arduino Code for Allegro ACS758 Current Sensor with LCD160
ภาษา: C++
/*
 * 
 * Arduino Sketch for Allegro ACS758 Current Sensor with LCD1602 & I2C module and current protection
 * This sensor can measure current at a range of up to 200A. It has overcurrent protection with a relay to disconnect the load if 
 * the current reaches beyond the limit.
 * It operates with 3.3V or 5V.
 * This sketch requires you to watch the following 2 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
 * 3- Combined 1 and 2 in one video https://www.youtube.com/watch?v=tug9wjCwDQA
 * 4- Allegro ACS with Robojax Library (latest video and code) https://youtu.be/sB6EULTix2k
 * 
 * Written by Ahmad Shamshiri on Tuesday, June 26, 2018 at 17:56 in Ajax, Ontario, Canada
 * for Robojax.com
 * You can watch a detailed video on the ACS758 Current sensor at: https://youtu.be/SiHfjzcqnU4
 * This code has been explained in this video: https://youtu.be/GE4I10IZ1jY
 * This code has been downloaded from Robojax.com
 */
#define VIN A0 // define the Arduino pin A0 as voltage input (V in)
const int relayPin = 2;// set a digital pin for relay
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)

const float maxCurrent = 15.00;// set maximum Current 
int maxCurrentWait = 6000;// wait time before current is connected

float minCurrent = 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   
The sensitivity array holds 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


// ======== 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 
    pinMode(relayPin,OUTPUT);// set relayPin as output
    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 blacklight 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( current >= minCurrent){
     if(current <= maxCurrent)
     {
              Serial.print("Current Limit: ");
              Serial.print(maxCurrent,3);// print voltage with 3 decimal places
              Serial.print("A, 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("I Limit: ");
              lcd.setCursor (9,1); // go to start of 2nd line
              lcd.print(maxCurrent);
              lcd.setCursor (15,1); // go to start of 2nd line
              lcd.print("A");   
              lcd.backlight();
             //end of loopcode Robojax code ACS758 with LCD1602 and I2C

              digitalWrite(relayPin,LOW);// turn the relay OFF to allow the current.
     }else{
      // the lines bellow will execute if current reaches above the maxCurrent value
              digitalWrite(relayPin,HIGH);// turn the relay ON to disconnect the current.
              
              Serial.print("Max Reached:");
              Serial.print(maxCurrent,3);// print the maxCurrent
              Serial.println("A");              
              Serial.print("Disconnected ");

              //start of loop Robojax code ACS758 with LCD1602 and I2C
              lcd.clear();
              lcd.setCursor (0,0); // set to line 1, char 0  
              lcd.print("I Max: ");
              lcd.setCursor (8,0); // go to start of 2nd line
              lcd.print(maxCurrent);
              lcd.setCursor (15,0); // go to start of 2nd line
              lcd.print("A");
            
              lcd.setCursor (0,1);    
              lcd.print("Disconnected");    
              lcd.backlight();
             //end of loopcode Robojax code ACS758 with LCD1602 and I2C 
             delay(maxCurrentWait );// wait for maxCurrentWait  seconds     
     }
    
  }else{
    Serial.println("No Current");
  lcd.clear();    
  lcd.setCursor (0,0);    
  lcd.print("No Current");
  digitalWrite(relayPin,LOW);// turn the relay OFF to allow the current.
          
  }
  delay(500);
}

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

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

ไฟล์📁

แผ่นข้อมูล (pdf)