Chargeur Solaire

Description

Le chargeur solaire est un dispositif complet de recharge de batterie : batterie au plomb 12V ou chargeur USB (pour smatrphone, …).

Il comprend ;

  • un panneau solaire 12V nominal
  • un régulateur de charge 12V pour batterie au plomb
  • un convertisseur DC/DC : 12V-5V (USB)

De plus, il est instrumenté, afin de pouvoir réaliser en temps réel des mesures, et les afficher. Pour cela il comporte :

  • un microcontrôleur Arduino UNO
  • un capteur de courant/tension INA219
  • un afficheur LCD 2 lignes

 

Modélisations

Maquette volumique

Document Onshape

 

Matériel

Capteur courant ACS712

datasheet-ebs011

Grove – Voltage Divider

LMV358ID_Datasheet

sol4ucn2a7v02

 

Programme

#include <LiquidCrystal.h>
#include <Adafruit_INA219.h>

LiquidCrystal lcd(12,11,5,4,3,2);
Adafruit_INA219 ina219;

// Variables globales

const int periodeMesure = 1000;  // millisecondes
unsigned long lastMesureTime = 0; 

// Menu
uint8_t menu = 4;
const uint8_t max_menu = 4;
const uint8_t BTN_PIN = 8;
int buttonState;            // the current reading from the input pin
int lastButtonState = LOW;  // the previous reading from the input pin
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

// Grandeurs mesurées
float shuntvoltage = 0; // mV
float busvoltage = 0;   // V
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
float E = 0.0; // Énergie (J)

unsigned long t = 0;
unsigned long t_1 = 0;

void setup() {
  Serial.begin(9600);

  lcd.begin(16,2);
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    lcd.print("Echec INA219");
    while (1) { delay(10); }
  }
  
  pinMode(BTN_PIN, INPUT_PULLUP);
  digitalWrite(BTN_PIN, HIGH);

  t = millis();
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(BTN_PIN);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;
      if (buttonState == HIGH) {
        menu += 1;
        if (menu > max_menu) {
          menu = 0;
        }
        //Serial.println(menu);
        afficher();

      }
    }
  }
  lastButtonState = reading;

  // Mesures et affichage
  if ((millis() - lastMesureTime) > periodeMesure) {
    lastMesureTime = millis();
    mesurer();
    afficher();
  }
}  
  
void afficher() { 
  lcd.clear();
  if (menu == 1) {  //affichage du courant
    lcd.setCursor(0,0);
    lcd.print("Courant bat     ");
    lcd.setCursor(0,1);
    lcd.print("I = ");
    lcd.print(prefixe(current_mA/1000, 3));
    lcd.print("A    ");
  }
  else if (menu == 2) {  //affichage de la tension
    lcd.setCursor(0,0);
    lcd.print("Tension bat     ");
    lcd.setCursor(0,1);
    lcd.print("U = ");
    lcd.print(prefixe(busvoltage, 3));
    lcd.print("V    ");
  }
  else if (menu == 3) {  //affichage de la puissance
    lcd.setCursor(0,0);
    lcd.print("Puissance bat   ");
    lcd.setCursor(0,1);
    lcd.print("P = ");
    lcd.print(prefixe(power_mW/1000, 3));
    lcd.print("W    ");
  }
  else if (menu == 4) {  //affichage de l'énergie produite sur 24h//
    lcd.setCursor(0,0);
    lcd.print("Energie bat    ");
    t_1 = t;
    t = millis();
    E += power_mW/1e6 * (t-t_1);  // Joules
    lcd.setCursor(0,1);
    lcd.print("E = "); 
    lcd.print(prefixe(E, 2));
    lcd.print("J    ");
  }
  else {
    lcd.setCursor(0,0);
    lcd.print("Mesures batterie");
    lcd.setCursor(0,1);
    lcd.print("bouton : menu");
  }
}


void mesurer() {
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = busvoltage*current_mA; //ina219.getPower_mW();

  Serial.println(String("U = ") + busvoltage + " V");
  Serial.println(String("I = ") + current_mA + " mA");
  Serial.println(String("P = ") + power_mW + " mW");
  Serial.println(String("P = ") + busvoltage*current_mA + " mW");
  Serial.println(String("E = ") + E + " J");
  Serial.println();
}


/******************************************************************************************/
char __mathHelperBuffer[17];
char * prefixe(float f, int n) {
  int pos = 0;
  
  // Handle negative numbers
  bool neg = (f < 0.0);
  if (neg)
  {
    __mathHelperBuffer[pos++] = '-';
    f = -f;
  }

  //Serial.println(f,4);
  char p;
  if (f >= 1000) {
    p = 'k';
    f /= 1000;
  } else if (f > 1) {
    p = ' ';
  } else if (f > 1/1000) {
    p = 'm';
    f *= 1000;
  }

  //Serial.println(f,4);
  
  int exponent = 0;
  
  while (f >= 10.0)
  {
    f /= 10;
    exponent++;
  }
  while (f < 1 && f != 0.0)
  {
    f *= 10;
    exponent--;
  }

  //Serial.print(f,4);
  //Serial.print("  E");
  //Serial.println(exponent);
  
  f *= pow(10, n-1);
  f = round(f);
  //Serial.println(f,4);

  char digits[n+1];
  int i = 0;
  int e = int(f);
  bool z = false;
  while (i <= n) {
    if (n-i-1 == exponent) {
      if (z) {
        digits[n-i] = '.';
      } else {
        digits[n-i] = '\0';
      }
      z = true;
    } else {
      if ((e % 10 != 0) || z) {
        digits[n-i] = (e % 10)  + '0';
        z = true;
      } else {
        digits[n-i] = '\0';
      }
      e /= 10;
    }
    i++;
  }

  //Serial.println(digits);
  if (digits[0] == '.') {
    __mathHelperBuffer[pos++] = '0';
  }
  i = 0;
  while ((i < n+1) && (digits[i] != '\0')) {
    __mathHelperBuffer[pos++] = digits[i];
    i++;
  }

  if (p != ' ') {
    __mathHelperBuffer[pos++] = ' ';
    __mathHelperBuffer[pos++] = p;
  }
  __mathHelperBuffer[pos] = '\0';
  
  return __mathHelperBuffer;
}






/******************************************************************************************/
float moy_analog( int pin )
{ 
  int valeur; 
  float moyenne = 0; 
  int nbr_lectures = 50; 
  for( int i = 0; i < nbr_lectures; i++ ){ 
    valeur = analogRead( pin ); 
    moyenne = moyenne + float(valeur);
    delay(2); 
  } 
  moyenne = moyenne / float(nbr_lectures); 
  return moyenne; 
}

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *