Cerca codice

Sensore di corrente Allegro ACS758 con LCD e protezione da sovracorrente per Arduino

Sensore di corrente Allegro ACS758 con LCD e protezione da sovracorrente per Arduino

In questo tutorial impareremo come usare il sensore di corrente Allegro ACS758 con un LCD per visualizzare le letture di corrente e implementare la protezione da sovracorrente. Questa configurazione ci permette di monitorare la corrente e di disconnettere automaticamente il carico se supera un limite prestabilito. Il progetto combina componenti hardware e software per creare un sistema di monitoraggio della corrente funzionale.

Seguendo questa guida, sarai in grado di collegare correttamente i componenti e di comprendere la logica di programmazione del codice. Per una spiegazione più visiva, assicurati di guardare il video associato (nel video a 00:00).

Hardware spiegato

I componenti chiave di questo progetto includono il sensore di corrente Allegro ACS758, un display LCD1602 con interfaccia I2C e una scheda Arduino. Il sensore ACS758 misura la corrente che lo attraversa e fornisce in uscita una tensione proporzionale alla corrente. Il display LCD1602 visualizza le letture di corrente e i messaggi di stato, mentre l'Arduino elabora i dati e controlla il relè per la protezione da sovracorrente.

Il sensore ACS758 funziona secondo un principio chiamato rilevamento a effetto Hall, che gli permette di misurare la corrente senza contatto elettrico diretto. La tensione di uscita varia in base alla quantità di corrente che lo attraversa, fornendo un modo sicuro ed efficiente per monitorare i carichi elettrici.

Dettagli della scheda tecnica

Produttore Allegro MicroSystems
Numero di parte ACS758
Tensione logica/IO 3.3V / 5V
Tensione di alimentazione 5V
Corrente di uscita (per canale) 200 A massimo
Corrente di picco (per canale) 200A
Linee guida sulla frequenza PWM N/D
Soglie logiche di ingresso 0.5 x VCC (bidirezionale)
Caduta di tensione / RDS(on)/ saturazione N/D
Limiti termici 150°C
Pacchetto montaggio su PCB
Note / varianti Diversi modelli disponibili per differenti intervalli di corrente

  • Garantire una corretta dissipazione del calore se si opera vicino ai limiti massimi.
  • Utilizzare condensatori di disaccoppiamento per stabilizzare la tensione di alimentazione.
  • Verificare che il relè utilizzato possa sopportare la corrente massima del carico.
  • Fai attenzione al cablaggio per evitare cortocircuiti.
  • Tra gli errori comuni ci sono gli ingressi flottanti; assicurarsi che tutte le connessioni siano sicure.
  • Monitorare il surriscaldamento del sensore durante l'uso prolungato.

Componenti necessari

  • sensore di corrente ACS758
  • Relè 12 V 100 A
  • LCD1602 con I2C (4 fili)
  • Transistor 2N2222 o 2N3904
  • 1k ohm 1/4W o qualsiasi resistore di potenza
  • Alimentatore per relè
  • Alimentazione per il tuo carico
  • Protoboard
  • Cavi jumper

Istruzioni di cablaggio

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

Per cablare il sensore di corrente Allegro ACS758 e il display LCD1602, inizia collegando il sensore ACS758. Collega ilVCCpin del sensore al pin 5V dell'Arduino. IlGNDIl pin dovrebbe essere collegato a un pin GND sull'Arduino. IlSIl pin (signal) del sensore dovrebbe essere collegato al pin di ingresso analogicoA0sull'Arduino.

Successivamente, per l'LCD1602, collega ilVCCpin al pin 5V sull'Arduino e ilGNDImmobilizzare a terra. IlSDAIl pin del display LCD dovrebbe collegarsi alA4pin (SDA) sull'Arduino, mentre ilSCLil pin dovrebbe collegarsi alA5pin (SCL) sull'Arduino. Infine, collega un modulo relè al pin digitale2per controllare il carico in base alle letture di corrente.

Esempi di codice e guida passo passo

Nel codice Arduino, iniziamo definendo identificatori chiave comeVIN, che rappresenta il pin di ingresso analogico collegato al sensore ACS758. IlrelayPinè impostato per il controllo del relè, mentremaxCurrentdefinisce la soglia per la protezione da sovracorrente.

#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 maxCurrent = 15.00; // set maximum Current

Ilsetup()La funzione inizializza il display LCD e configura il pin del relè come uscita. Stampa anche un messaggio di benvenuto sul display LCD, informando l'utente sul sensore di corrente in uso.

void setup() {
    pinMode(relayPin, OUTPUT); // set relayPin as output
    Serial.begin(9600); // initialize serial monitor
    lcd.begin(); // initialize the LCD
    lcd.backlight(); // Turn on the blacklight
    lcd.print("Robojax");
}

Nelloop()Nella funzione leggiamo continuamente la tensione dal sensore e calcoliamo la corrente. Se la corrente supera il limite massimo, il relè viene attivato per scollegare il carico. Questa logica garantisce che il sistema si protegga dalle condizioni di sovracorrente.

void loop() {
  float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // Read the voltage from sensor
  float current = voltage / FACTOR; // Calculate current
  if (current >= minCurrent) {
    if (current <= maxCurrent) {
      digitalWrite(relayPin, LOW); // turn the relay OFF to allow current
    } else {
      digitalWrite(relayPin, HIGH); // turn the relay ON to disconnect current
    }
  }
}

Dimostrazione / Cosa aspettarsi

Una volta che tutto è collegato e il codice è caricato, l'LCD mostrerà le letture di corrente. Se la corrente supera il valore definitomaxCurrent, il relè si attiverà, scollegando il carico. Puoi verificarlo aumentando gradualmente la corrente di carico e osservando le variazioni sull'LCD e nel monitor seriale. Assicurati di evitare collegamenti con polarità invertita, poiché ciò può danneggiare i componenti (nel video a 10:15).

Immagini

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
Lingua: 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);
}

Cose di cui potresti avere bisogno

File📁

Scheda tecnica (pdf)