Auteur Sujet: ASC 712 capteur à effet hall  (Lu 433 fois)

frederic

  • Newbie
  • *
  • Messages: 30
    • Voir le profil
ASC 712 capteur à effet hall
« le: octobre 20, 2024, 03:10:54 pm »
Bonjour,

Je vous mets à disposition un sketch pour un va et viens analogique avec démarrage et arrêt progressif.
Matériels: 1 Arduino uno, 1 L298N, 3 ASC712 

Le code est pas de moi juste apporté quelque modification et ajout, il y a certainement mieux, mais sa fonctionne très bien,
J' espère que sa peut vous servir.
Si vous avez des modifications ou des améliorations à apporter pas de problème. 

#define VERSION "1.001.006"
#define SYS_ID "ACS712 Sensor Test"
const int pin = 1;
const int pin1 = 2;
const int pin2 = 3;
int pin8 = 8;
int ena = 9;
int in1 = 8;
int in2 = 7;
int vitesse ;
int direct ;


// Sampling Parameters
const unsigned long sampleTime = 58000UL; // 58 ms
const unsigned long numSamples = 300UL;
// sample interval is in microseconds
// must be greater than 100μs, the conversion time of the internal ADC
const unsigned long sampleInterval = sampleTime/numSamples;

#define SENSITIVITY 185 // per ACS712 5A data sheet, in mv/A
#define DETECTION_MULTIPLIER 1.095 // change as necessary to improve detection accuracy
#define CALIBRATION_READS 5000

// variables to hold sensor quiescent readings
int aqv;
int aqv1;
int aqv2; 
float aqc;
float aqc1;
float aqc2;

void setup()
{
  pinMode(ena, OUTPUT); 
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(pin8,HIGH);

  TCCR1B &= 0xF8;
TCCR1B |= B00000010;
 
  float sense;
  Serial.begin(9600);
  Serial.println(String(F(SYS_ID)) + String(F(" - SW:")) + String(F(VERSION)));
  Serial.print("\n\nCalibrating the sensor at pin ");
  Serial.println(pin);
  aqv = determineVQ(pin);
  Serial.print("AQV: ");
  Serial.print(aqv, 4);
  Serial.println(" mV");
  aqc = determineCQ(pin, aqv);
  Serial.print("AQC: ");
  Serial.print(aqc, 4);
  Serial.println(" mA");
  sense = (aqc * DETECTION_MULTIPLIER) - aqc;
  Serial.print("Detection Threshold: ");
  Serial.print(sense * 1000, 4);
  Serial.println(" mA\n\n");
 
  float sense1;
  Serial.print("\n\nCalibrating the sensor at pin ");
  Serial.println(pin1);
  aqv1 = determineVQ(pin1);
  Serial.print("AQV1: ");
  Serial.print(aqv1, 4);
  Serial.println(" mV");
  aqc1 = determineCQ(pin1, aqv1);
  Serial.print("AQC1: ");
  Serial.print(aqc, 4);
  Serial.println(" mA");
  sense1 = (aqc1 * DETECTION_MULTIPLIER) - aqc1;
  Serial.print("Detection Threshold: ");
  Serial.print(sense1 * 1000, 4);
  Serial.println(" mA\n\n");
 

  float sense2;
  Serial.print("\n\nCalibrating the sensor at pin ");
  Serial.println(pin2);
  aqv2 = determineVQ(pin2);
  Serial.print("AQV2: ");
  Serial.print(aqv2, 4);
  Serial.println(" mV");
  aqc2 = determineCQ(pin2, aqv2);
  Serial.print("AQC2: ");
  Serial.print(aqc, 4);
  Serial.println(" mA");
  sense2 = (aqc2 * DETECTION_MULTIPLIER) - aqc2;
  Serial.print("Detection Threshold: ");
  Serial.print(sense2 * 1000, 4);
  Serial.println(" mA\n\n");
  delay(5000);


}

void loop(){
   

  float current = readCurrent(pin, aqv);
  bool occupied = current > (aqc * DETECTION_MULTIPLIER);

 
 
  Serial.print("Current Sensed:");
  Serial.print(current * 1000,1);
  Serial.print(" mA\t\t");
  Serial.print("The block1 is ");
  if(occupied){ if (direct == 1){
   for (vitesse = 255; vitesse >=30; vitesse -=5)
    {analogWrite(ena,vitesse);
    delay(50);
    }
    digitalWrite(in1, 0); 
    digitalWrite(in2, 1);
    for (vitesse = 30; vitesse <=255; vitesse +=5)
    {analogWrite(ena,vitesse);
    delay(50);
    }
    //digitalWrite(in1, 1); 
    //digitalWrite(in2, 0);
    Serial.println("occupied");
   
  }
  direct = 2;
    Serial.println("occupied");
   
  } else {
    Serial.println("not occupied");
  }

float current1 = readCurrent1(pin1, aqv1);
  bool occupied1 = current1 > (aqc1 * DETECTION_MULTIPLIER);
 
  Serial.print("Current Sensed1:");
  Serial.print(current1 * 1000,1);
  Serial.print(" mA\t\t");
  Serial.print("The block2 is ");
  if(occupied1){
    Serial.println("occupied");
   
  } else {
    Serial.println("not occupied");
  }

  float current2 = readCurrent2(pin2, aqv2);
  bool occupied2 = current2 > (aqc2 * DETECTION_MULTIPLIER);

  Serial.print("Current Sensed2:");
  Serial.print(current2 * 1000,1);
  Serial.print(" mA\t\t");
  Serial.print("The block3 is ");
  if(occupied2){if (direct == 2){
   for (vitesse = 255; vitesse >=30; vitesse -=5)
    {analogWrite(ena,vitesse);
    delay(50);
    }
    digitalWrite(in1, 1); 
    digitalWrite(in2, 0);
    for (vitesse = 30; vitesse <=255; vitesse +=5)
    {analogWrite(ena,vitesse);
    delay(50);
    }
    //digitalWrite(in1, 1); 
    //digitalWrite(in2, 0);
    Serial.println("occupied");
   
  }
  direct = 1;
  } else {
    Serial.println("not occupied");
  }
 
 /*
if (essai == 1) {
    if (test == 1) {
      digitalWrite(0, HIGH);
*/
 
  delay(100);
 
}

//////////////////////////////////////////
// ACS712 Current Sensor Functions
//////////////////////////////////////////
float readCurrent(int PIN, float adc_zero)
{
  float currentAcc = 0;
  unsigned int count = 0;
  unsigned long prevMicros = micros() - sampleInterval ;
  while (count < numSamples)
  {
    if (micros() - prevMicros >= sampleInterval)
    {
      float adc_raw = (float) analogRead(PIN) - adc_zero;
      adc_raw /= SENSITIVITY; // convert to amperes
      currentAcc += (adc_raw * adc_raw);
      ++count;
      prevMicros += sampleInterval;
    }
  }
  //https://en.wikipedia.org/wiki/Root_mean_square
  float rms = sqrt((float)currentAcc / (float)numSamples);
  return rms;
}

float readCurrent1(int PIN, float adc_zero1)
{
  float currentAcc = 0;
  unsigned int count = 0;
  unsigned long prevMicros = micros() - sampleInterval ;
  while (count < numSamples)
  {
    if (micros() - prevMicros >= sampleInterval)
    {
      float adc_raw1 = (float) analogRead(PIN) - adc_zero1;
      adc_raw1 /= SENSITIVITY; // convert to amperes
      currentAcc += (adc_raw1 * adc_raw1);
      ++count;
      prevMicros += sampleInterval;
    }
  }
  //https://en.wikipedia.org/wiki/Root_mean_square
  float rms1 = sqrt((float)currentAcc / (float)numSamples);
  return rms1;
}

float readCurrent2(int PIN, float adc_zero2)
{
  float currentAcc = 0;
  unsigned int count = 0;
  unsigned long prevMicros = micros() - sampleInterval ;
  while (count < numSamples)
  {
    if (micros() - prevMicros >= sampleInterval)
    {
      float adc_raw2 = (float) analogRead(PIN) - adc_zero2;
      adc_raw2 /= SENSITIVITY; // convert to amperes
      currentAcc += (adc_raw2 * adc_raw2);
      ++count;
      prevMicros += sampleInterval;
    }
  }
  //https://en.wikipedia.org/wiki/Root_mean_square
  float rms2 = sqrt((float)currentAcc / (float)numSamples);
  return rms2;
}
//////////////////////////////////////////
// Calibration
// Track Power must be OFF during calibration
//////////////////////////////////////////

int determineVQ(int PIN) {
  float VQ = 0;
  //read a large number of samples to stabilize value
  for (int i = 0; i < CALIBRATION_READS; i++) {
    VQ += analogRead(PIN);
    delayMicroseconds(sampleInterval);
  }
  VQ /= CALIBRATION_READS;
  return int(VQ);
}

int determineVQ1(int PIN) {
  float VQ1 = 0;
  //read a large number of samples to stabilize value
  for (int i = 0; i < CALIBRATION_READS; i++) {
    VQ1 += analogRead(PIN);
    delayMicroseconds(sampleInterval);
  }
  VQ1 /= CALIBRATION_READS;
  return int(VQ1);
}

int determineVQ2(int PIN) {
  float VQ2 = 0;
  //read a large number of samples to stabilize value
  for (int i = 0; i < CALIBRATION_READS; i++) {
    VQ2 += analogRead(PIN);
    delayMicroseconds(sampleInterval);
  }
  VQ2 /= CALIBRATION_READS;
  return int(VQ2);
}

float determineCQ(int pin, float aqv) {
  float CQ = 0;
  // set reps so the total actual analog reads == CALIBRATION_READS
  int reps = (CALIBRATION_READS / numSamples);
  for (int i = 0; i < reps; i++) {
    CQ += readCurrent(pin, aqv);
  }
  CQ /= reps;
  return CQ;
}

float determineCQ1(int pin1, float aqv1) {
  float CQ1 = 0;
  // set reps so the total actual analog reads == CALIBRATION_READS
  int reps1 = (CALIBRATION_READS / numSamples);
  for (int i = 0; i < reps1; i++) {
    CQ1 += readCurrent(pin1, aqv1);
  }
  CQ1 /= reps1;
  return CQ1;
}

float determineCQ2(int pin2, float aqv2) {
  float CQ2 = 0;
  // set reps so the total actual analog reads == CALIBRATION_READS
  int reps2 = (CALIBRATION_READS / numSamples);
  for (int i = 0; i < reps2; i++) {
    CQ2 += readCurrent(pin2, aqv2);
  }
  CQ2 /= reps2;
  return CQ2;
}

Dominique

  • Global Moderator
  • Hero Member
  • *****
  • Messages: 3026
  • 100% Arduino et N
    • Voir le profil
Re : ASC 712 capteur à effet hall
« Réponse #1 le: octobre 20, 2024, 03:54:43 pm »
Merci Frédéric,

C’est un exemple très intéressant d’utilisation du capteur ACS712 pour mesurer le courant consommé et de la fonction PWM, dans un exemple ferroviaire qui intéresse tout le monde.

Merci pour ce partage et j’espère que quelqu’un nous donnera les résultats de sa mise en œuvre.
Cordialement,
Dominique