Search Code

การใช้ ACS712 เพื่อแสดงกระแสบน LCD1602 หรือ LCD2004 พร้อม I2C

การใช้ ACS712 เพื่อแสดงกระแสบน LCD1602 หรือ LCD2004 พร้อม I2C

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

LCD1602-I2C display module with 4 wires

สำหรับความชัดเจนเกี่ยวกับการเดินสายไฟและโค้ด โปรดดูวิดีโอที่ไทม์สแตมป์ที่ระบุ (ในวิดีโอที่ 00:00)

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

ส่วนประกอบหลักของโปรเจกต์นี้คือเซนเซอร์วัดกระแส ACS712 ซึ่งสามารถวัดระดับกระแสไฟฟ้าได้สูงสุดถึง 30A เซนเซอร์นี้ทำงานโดยใช้เอฟเฟกต์ฮอลล์เพื่อแปลงกระแสไฟฟ้าที่ไหลผ่านให้เป็นแรงดันไฟฟ้าขาออกตามสัดส่วน แรงดันไฟฟ้าขาออกนี้สามารถอ่านได้โดย Arduino เพื่อคำนวณระดับกระแสไฟฟ้า เซนเซอร์มีพินสามขาสำหรับการเชื่อมต่อ: VCC สำหรับไฟเลี้ยง, GND สำหรับกราวด์ และ OUT สำหรับสัญญาณแรงดันไฟฟ้า

จอ LCD1602 หรือ LCD2004 เชื่อมต่อผ่าน I2C ซึ่งช่วยให้การเดินสายไฟและการควบคุมง่ายขึ้นโดยใช้พินข้อมูลเพียงสองพิน (SDA และ SCL) นอกเหนือจากไฟเลี้ยงและกราวด์ ทำให้การแสดงค่ากระแสไฟฟ้าเป็นเรื่องง่ายโดยไม่ทำให้การเดินสายไฟยุ่งเหยิงด้วยการเชื่อมต่อหลายจุด

รายละเอียดจากดาต้าชีต

ผู้ผลิตAllegro Microsystems
หมายเลขชิ้นส่วนACS712ELCTR-30A-T
แรงดันไฟฟ้าลอจิก/IO4.5–5.5 V
แรงดันไฟฟ้าจ่าย5 V
กระแสขาออก (ต่อช่อง)30 A
คำแนะนำความถี่ PWMไม่มี
เกณฑ์แรงดันไฟฟ้าอินพุตลอจิก0.5 V (เอาต์พุตขณะนิ่ง)
แรงดันตก / RDS(on) / ความอิ่มตัว1.2 mΩ
ขีดจำกัดความร้อน150 °C สูงสุด
แพ็คเกจTO-220

  • ตรวจสอบให้แน่ใจว่ามีการระบายความร้อนที่เหมาะสมสำหรับกระแสไฟฟ้าที่สูงกว่า 20A
  • ใช้ขนาดสายไฟที่เหมาะสม (12 AWG สำหรับ 30A) เพื่อหลีกเลี่ยงความร้อนสูงเกินไป
  • เชื่อมต่อ VCC เข้ากับ 5V และ GND เข้ากับกราวด์เพื่อการทำงานที่ถูกต้อง
  • ตรวจสอบให้แน่ใจว่าพิน OUT เชื่อมต่อกับพินอินพุตแอนะล็อกบน Arduino (เช่น A0)
  • ระวังความแตกต่างของความไวสำหรับรุ่น ACS712 ต่างๆ
  • ใช้ตัวเก็บประจุสำหรับการแยกสัญญาณตามที่ระบุในดาต้าชีต

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

Arduino wiring for Allegro ACS712 current sensor with LCD

ในการเดินสายไฟเซนเซอร์วัดกระแส ACS712 เข้ากับ Arduino และ LCD ให้เริ่มต้นโดยเชื่อมต่อพิน VCC ของ ACS712 เข้ากับพิน 5V บน Arduino จากนั้นเชื่อมต่อพิน GND ของ ACS712 เข้ากับพินกราวด์บน Arduino พิน OUT ของ ACS712 ควรเชื่อมต่อกับพินแอนะล็อก A0 บน Arduino ซึ่งจะอ่านสัญญาณแรงดันไฟฟ้าที่นี่

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

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

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

#define VIN A0 // กำหนดพิน A0 ของ Arduino เป็นอินพุตแรงดันไฟฟ้า (V in)
const float VCC   = 5.0; // แรงดันไฟฟ้าจ่ายอยู่ระหว่าง 4.5 ถึง 5.5V โดยปกติคือ 5V

ถัดไป เราตั้งค่าความไวตามรุ่นของ ACS712 ที่เราใช้ ค่าความไวถูกเก็บไว้ในอาร์เรย์ เพื่อให้เข้าถึงได้ง่ายตามการเลือกรุ่น

const int model = 2; // ใส่หมายเลขรุ่น (0, 1, หรือ 2 สำหรับ 5A, 20A, หรือ 30A)
float sensitivity[] = {0.185, 0.100, 0.066}; // ค่าความไวสำหรับแต่ละรุ่น

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

void setup() {
    Serial.begin(9600); // เริ่มต้นซีเรียลมอนิเตอร์
    lcd.begin(); // เริ่มต้นจอ LCD
    lcd.backlight(); // เปิดไฟแบ็คไลท์
    lcd.print("Robojax ACS712"); // แสดงข้อความเริ่มต้น
    delay(2000); // รอ 2 วินาที
}

ฟังก์ชัน loop() อ่านแรงดันไฟฟ้าจากเซนเซอร์ คำนวณกระแสไฟฟ้าตามค่าความไว และแสดงผลลัพธ์ตามนั้น หากกระแสไฟฟ้าเกินขีดจำกัดการตัดที่กำหนด จะแสดงค่า; มิฉะนั้น จะระบุว่าไม่มีการตรวจจับกระแสไฟฟ้า

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

เมื่อจ่ายไฟให้วงจร จอ LCD ควรแสดง "Robojax ACS712" ตามด้วย "Current: " พร้อมกับค่ากระแสไฟฟ้าที่วัดได้ในหน่วยแอมแปร์ หากกระแสไฟฟ้าต่ำกว่าขีดจำกัดการตัด จอจะแสดง "No Current" ควรใช้ความระมัดระวังเมื่อทดสอบกับกระแสไฟฟ้าสูง เนื่องจากเซนเซอร์อาจร้อนได้ (ในวิดีโอที่ 12:00)

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

ภาพ

Allegro ACS712 DC/AC current sensor with LCD1602/LCD2004 I2C and Arduino code
Allegro ACS712 DC/AC Current sensor with LCD1602/LCD2004 I2C with Arduino Code
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
ACS712_in_series_with_load
ACS712_in_series_with_load
allegro_ACS712_module-1
allegro_ACS712_module-1
allegro_ACS712_module-2
allegro_ACS712_module-2
allegro_ACS712_module-4
allegro_ACS712_module-4
allegro_ACS712_module-0
allegro_ACS712_module-0
Arduino wiring for Allegro ACS712 current sensor with LCD
Arduino wiring for Allegro ACS712 current sensor with LCD
131-Allegro ACS712 DC/AC current sensor with LCD1602/LCD2004 I2C and Arduino code
ภาษา: C++
/*
 * 
 * Arduino Sketch for Allegro ACS712 Current Sensor with LCD1602/LCD2004 with I2C 
 * This sensor can measure current at a range of up to 30A.
 * The current is measured and displayed on LCD1602 or LCD2004.
 * The current is also displayed on the Serial monitor (click Tools->Serial monitor).
 * It operates with 5V.
 * Please watch the video instruction and explanation for this code.
 * 
 * Written by Ahmad Shamshiri on July 14, 2018 at 07:02 in Ajax, Ontario, Canada
 * for Robojax.com
 * View the video instruction at https://youtu.be/kzXouSzvcp8
 * This code has been downloaded from Robojax.com
 
  * You must watch the following two videos before you can understand this:
 * 1- Introduction to ACS712 Current Sensor: https://www.youtube.com/watch?v=DVp9k3xu9IQ
 * 2- LCD1602 with I2C display: https://www.youtube.com/watch?v=q9YC_GVHy5A
 */
#define VIN A0 // define the Arduino pin A0 as voltage input (V in)
const float VCC   = 5.0;// supply voltage is from 4.5 to 5.5V. Normally 5V.
const int model = 2;   // enter the model number (see below)

float cutOffLimit = 1.01;// set the current which, below that value, doesn't matter. Or set 0.5

/*
          "ACS712ELCTR-05B-T",// for model use 0
          "ACS712ELCTR-20A-T",// for model use 1
          "ACS712ELCTR-30A-T"// for model use 2  
Sensitivity array is holding the sensitivity of the ACS712
current sensors. Do not change. All values are from page 5 of the data sheet          
*/
float sensitivity[] ={
          0.185,// for ACS712ELCTR-05B-T
          0.100,// for ACS712ELCTR-20A-T
          0.066// for ACS712ELCTR-30A-T
     
         }; 


const float QOV =   0.5 * VCC;// set quiescent Output voltage of 0.5V
float voltage;// internal variable for voltage


// start of settings for LCD1602 with I2C
#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 settings for LCD1602 with I2C

void setup() {
    //Robojax.com ACS712 Current Sensor 
    Serial.begin(9600);// initialize serial monitor
    Serial.println("Robojax Tutorial");
    Serial.println("ACS712 Current Sensor");
  // initialize the LCD
  lcd.begin();  
  lcd.backlight();
  lcd.print("Robojax ACS712");
  lcd.setCursor(0,1);
  lcd.print("Demo"); 
  delay(2000);     
}

void loop() {
  

  //Robojax.com ACS712 Current Sensor with LCD1601
  float voltage_raw =   (5.0 / 1023.0)* analogRead(VIN);// Read the voltage from sensor
  voltage =  voltage_raw - QOV + 0.012 ;// 0.000 is a value to make voltage zero when there is no current
  float current = voltage / sensitivity[model];
 
  if(abs(current) > cutOffLimit ){
    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 ACS712 with LCD1602 and I2C
    lcd.clear();
    lcd.setCursor (0,0); // set to line 1, char 0  
    lcd.print("Robojax ACS712");
    lcd.setCursor (0,1); // set to line 1, char 0  
    lcd.print("Current: ");
    lcd.setCursor (9,1); // go to start of 2nd line
    lcd.print(current);
    lcd.setCursor (14,1); // go to start of 2nd line
    lcd.print("A");
 
  }else{
    Serial.println("No Current");
    lcd.clear();
    lcd.setCursor (0,0); // set to line 1, char 0 
    lcd.print("No Current");     
  }
  delay(500);
}

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

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

ไฟล์📁

ไลบรารี Arduino (zip)

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

ไฟล์ Fritzing