Proyecto Arduino para mini centralita domótica que controla luces y ventilador con botones y relés usando resistencias pull-up y debounce por software.
// Mini centralita domótica: control de luces y ventilador mediante botones y relés
// Cada botón conmuta el estado del relé respectivo
// Definición de pines de botones
const int pulsanteLuce1 = 2;
const int pulsanteLuce2 = 3;
const int pulsanteVentola = 4;
// Definición de pines de relés (accionados mediante transistores)
const int releLuce1 = 8;
const int releLuce2 = 9;
const int releVentola = 10;
// Variables para almacenar estado de relés
bool statoLuce1 = false;
bool statoLuce2 = false;
bool statoVentola = false;
// Variables para gestión de debounce
unsigned long ultimoTempoLuce1 = 0;
unsigned long ultimoTempoLuce2 = 0;
unsigned long ultimoTempoVentola = 0;
const unsigned long debounceDelay = 50; // debounce de 50 ms
void setup() {
// Configuración de pines de botones como INPUT_PULLUP
pinMode(pulsanteLuce1, INPUT_PULLUP);
pinMode(pulsanteLuce2, INPUT_PULLUP);
pinMode(pulsanteVentola, INPUT_PULLUP);
// Configuración de pines de relés como OUTPUT
pinMode(releLuce1, OUTPUT);
pinMode(releLuce2, OUTPUT);
pinMode(releVentola, OUTPUT);
// Inicializar relés apagados (LOW = relé apagado, depende del módulo)
digitalWrite(releLuce1, LOW);
digitalWrite(releLuce2, LOW);
digitalWrite(releVentola, LOW);
// Serial para depuración
Serial.begin(9600);
Serial.println("Centralita domótica iniciada");
}
void loop() {
// Control botón luz 1
if (leggiPulsante(pulsanteLuce1, &ultimoTempoLuce1)) {
statoLuce1 = !statoLuce1; // Invertir estado
digitalWrite(releLuce1, statoLuce1 ? HIGH : LOW);
Serial.print("Luz 1: ");
Serial.println(statoLuce1 ? "Encendida" : "Apagada");
}
// Control botón luz 2
if (leggiPulsante(pulsanteLuce2, &ultimoTempoLuce2)) {
statoLuce2 = !statoLuce2;
digitalWrite(releLuce2, statoLuce2 ? HIGH : LOW);
Serial.print("Luz 2: ");
Serial.println(statoLuce2 ? "Encendida" : "Apagada");
}
// Control botón ventilador
if (leggiPulsante(pulsanteVentola, &ultimoTempoVentola)) {
statoVentola = !statoVentola;
digitalWrite(releVentola, statoVentola ? HIGH : LOW);
Serial.print("Ventilador: ");
Serial.println(statoVentola ? "Encendido" : "Apagado");
}
}
// Función para leer botón con debounce
// Devuelve true si el botón fue presionado (transición estable HIGH->LOW)
bool leggiPulsante(int pin, unsigned long* ultimoTempo) {
static bool statoPrecedente[11] = {HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH};
bool statoAttuale = digitalRead(pin);
unsigned long tempoCorrente = millis();
// Comprobar transición de HIGH a LOW (botón presionado)
if (statoPrecedente[pin] == HIGH && statoAttuale == LOW) {
// Verificar debounce
if (tempoCorrente - *ultimoTempo > debounceDelay) {
*ultimoTempo = tempoCorrente;
statoPrecedente[pin] = statoAttuale;
return true;
}
}
statoPrecedente[pin] = statoAttuale;
return false;
}Arduino Uno (x1)
Relé de 5V y 1 canal (x3) para control de luces y ventilador
Diodo 1N4007 (x3) para protección contra corrientes inversas en los relés
Transistor NPN (ej. 2N2222) (x3) para accionar los relés
Resistencia 1kΩ (x3) para base del transistor
Resistencia 10kΩ (x3) para pull-down del transistor (opcional)
Botón pulsador (x3)
Resistencia interna de pull-up de Arduino (usada en los botones)
Alimentación 5V (USB o externa)
CONEXIONES: