Arduino


Contrôler la lumière d'ambiance (ruban LED) dans une pièce.

Publié le 22/12/2018



Contrôler la lumière d'ambiance dans une pièce (par exemple la cuisine) via un arduino nano.



Dans cet exemple, les LED s'allument progressivement dès qu'une personne est détectée. L'éclairage dure 30 secondes (sauf si la personne est toujours présente), puis s'éteint progressivement.



En option, on peut également allumer un petit spot en le branchant sur le relais.



Dans la vidéo, le spot ne s'allume pas car il a grillé...



Cet éclairage est très pratique la nuit, lorsqu'on a une petite soif: Plus besoin d'allumer les lumières qui font mal aux yeux...

Liste des composants nécessaires pour la réalisation du circuit :

  • Arduino nano1
  • Relais1
  • Ruban LED1


Schéma du montage électronique :


Pour les amateurs d'impression 3D, j'ai fait quelques templates qui permettent d'imprimer divers boitiers pouvant accueillir les divers composants utilisés dans mes tutoriels.
- Pour un composant PIR, vous trouverez le boitier à imprimer ici.

    Il faut compter environ 4h pour imprimer les composants de ce boitier.


- Pour un relais, vous trouverez le boitier à imprimer ici.
    Il faut compter environ 2h pour imprimer les composants de ce boitier.




L'ensemble des impressions 3D a été réalisé sur une imprimante Creality3D Ender-3 pro avec les réglages standards suivants:

Pour les personnes possédant Fritzing, voici le schéma électronique.


Voici le code à télécharger dans votre Arduino:

//*****************************************************************************************//
//                            LED cuisine Information and Tests
//                                Ludovic GAUTHIER, May 2016
//*****************************************************************************************//
 
// Build 1
// r1 160508 - initial build 
//*****************************************************************************************//
#define build 1
#define revision 1
//*****************************************************************************************//

#define pinLED 5                    // Pin pour les LED
#define pinPIR 2                    // Pin pour le capteur PIR
#define pinRelais 11                // Pin pour le relais

#define DELAI_LED_ALLUMEE 30*1000    // Temps d'éclairage des LED en ms

int pirState = LOW;                 // Mémorisation de l'état du capteur PIR
unsigned long _il_tempsDepart = 0;  // Temps écoulé depuis le démarrage de l'arduino nano

const byte TENSION_MAX = 255*3/3.3; // 3.3V pour nano, 5v pour UNO

//*****************************************************************************************
//                                      Initial Setup{
//*****************************************************************************************//  
void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600); 
  
  Serial.println(F("*************************************************************************"));
  Serial.println(F("Titre:    LED cuisine Info et Tests"));
  Serial.print(F("File:     ")); Serial.println(__FILE__); 
  Serial.print(F("Build:    ")); Serial.print(build);     Serial.print(F(".")); Serial.println(revision);
  Serial.print(F("Compile:  ")); Serial.print(__TIME__);  Serial.print(F(", "));   Serial.println(__DATE__);
  Serial.print(F("Free RAM: ")); Serial.print(freeRam()); Serial.println(F("B\r\n"));
  Serial.println(F("*************************************************************************"));

  Serial.println(F("\nInitialisation..."));
  Serial.println(F("************"));  
  Serial.println(F("Arduino Nano"));  
  Serial.println(F("************"));

  Serial.println(F("\nEclairage d'ambiance de la cuisine\n"));    

  Serial.println(F("On patiente 5 secondes"));
  delay(5000);
  
  Serial.println(F(""));
  Serial.println(F("Connection du capteur PIR"));
  Serial.println(F(""));
  
  Serial.println(F("***********************"));
  Serial.println(F("*     GND DATA VCC    *"));
  Serial.println(F("*      .    .   .     *"));
  Serial.println(F("*                     *"));
  Serial.println(F("*                     *"));
  Serial.println(F("*      O        O     *"));
  Serial.println(F("*    Timer    Sensi   *"));
  Serial.println(F("***********************"));
  Serial.println(F(""));

  Serial.print(F("On calibre le capteur PIR "));
  
  for(int i = 0; i < 3; i++)
  {
      Serial.print(F("."));
      delay(1000);
  }

  Serial.println(F(""));
  Serial.println(F(""));
  Serial.print(F("Tension max Led = "));
  Serial.print(F(" (3 v)"));
  Serial.println(TENSION_MAX);
  Serial.println(F(""));
  Serial.println(F("Démarrage des controles !"));
  Serial.println(F(""));
  
  pinMode(pinLED, OUTPUT);      
  pinMode(pinRelais, OUTPUT); 
  digitalWrite(pinRelais, LOW);     
}

int Easing(int i)
{
  // (p) puissance 4
  return (((float)i/100)*((float)i/100)*((float)i/100)*((float)i/100))*TENSION_MAX;
}

void allumeLED()
{
  int j=0;
  
  for (int i=0;i<=100;i++)
  {
    Serial.print(j);
    Serial.print(F(" "));
    
    analogWrite(pinLED, j);
    delay(20);

    Serial.println(Easing(i));
    
    j=Easing(i);
  }
}

void eteintLED()
{
  int j=255;
  
  for (int i=100;i>=0;i--)
  {
    Serial.print(j);
    Serial.print(F(" "));
    
    analogWrite(pinLED, j);
    delay(20);
    
    Serial.println(Easing(i));
    
    j=Easing(i);
  }
}

//*****************************************************************************************//
//                                      MAIN LOOP
//*****************************************************************************************//
void loop() 
{ 
  int val = digitalRead(pinPIR);
  
  Serial.print("val=");
  Serial.print(val);
  Serial.print(" pirState=");
  Serial.print(pirState);
  Serial.println("");

  // Si un mouvement est déjà signalé
  // et que le temps max d'allumage n'est pas dépassé
if (pirState == HIGH && millis() - _il_tempsDepart < DELAI_LED_ALLUMEE)
   {
    Serial.println(F(" Pause 1s"));    
    delay(1000);

    // A chaque nouveau mouvement on retarde le chrono
    if (val == HIGH) 
    { 
      _il_tempsDepart = millis();
    } 
  }
  else
  {    
    // Un mouvement est signalé
    if (val == HIGH) 
    {            
      // C'est le premier signalement  
      if (pirState == LOW) 
      {
        // On vient de détecter un mouvement
        Serial.println("Mouvement détecté!");
        // On mémorise la détection de mouvement
        pirState = HIGH;
        // On allume le ruban à LED
        Serial.println(F("Allume LED"));
        allumeLED(); 
        delay(2000);
        // On allume la lampe de la vitrine
        digitalWrite(pinRelais, HIGH);
        // On mémorise le dernier mouvement  
        _il_tempsDepart = millis();  
      }
    } 
    else 
    {
      // Un mouvement était signalé
      if (pirState == HIGH)
      {
        Serial.println("Alerte terminée !");
        Serial.println(F("Eteint LED"));
        // On éteint la lampe de la vitrine
        digitalWrite(pinRelais, LOW);  
        delay(2000);
        // On éteint le ruban à LED
        eteintLED();  
        // We only want to print on the output change, not state
        pirState = LOW;        
      }
    }
  
    digitalWrite(4, pirState);
  }
}

// ********************************************************************************** //
//                              OPERATION ROUTINES
// ********************************************************************************** //
// FREERAM: Returns the number of bytes currently free in RAM  
int freeRam(void) 
{
  extern int  __bss_end, *__brkval; 
  int free_memory; 
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end); 
  } 
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval); 
  }
  return free_memory; 
}
Nombre de vue(s): 4317

Forum


Vos avis/remarques...


Veuillez vous connecter pour laisser vos commentaires...