📜 ⬆️ ⬇️

Controlling lighting scenarios with Arduino

The reasons for the emergence of this project are essentially two. One is systematically blown lamps in the hallway. The second is the experience of working with automation systems based on industrial programmable controllers (designing systems for AOB, AHS, ASDU, etc.) and a desire to apply this knowledge for the benefit of their households.

As you may have guessed, the discussion will deal with automatic lighting control, but since it is boring and not modern to simply turn on the light by the motion sensor, the engineering thought led me to the following option: automatic control of lighting scenarios (day and night) with the possibility of both manual and remote switching modes.

I think we should start with the scheme, and further make explanations to it.

Wiring diagram
scheme

Also visual will be the floor plan.
')
Hallway plan
image

So in the hallway there are 2 scenarios of lighting, daylight with LED strip on the ceiling and night, with LED strip on the floor level. Both modes are controlled by two motion sensors located above the door and next to the mirror, as can be seen from the plan. Initially, only one motion sensor was installed above the door, but to my surprise, the light periodically turned off when there were people in the room. It turned out that the sensor is not able to catch the small manipulations made at the mirror, especially if the object is back to him and creates a blind zone in front of him. The second discovery for me was that some people can spend more than 1-2 minutes at the mirror (I usually have 15 seconds). The first solution that came to mind was to install a more advanced presence sensor like Esylux + relay, but the cost of such equipment would exceed the cost of the rest of the project, so a simpler option was chosen to install a second motion sensor for “blind zones”.

The motion sensor was assembled from the module HC-SR501 + terminal box.
Motion Sensor
image
image

The algorithm of the sensors will be presented in the code, but in a nutshell, the light of one of the sensors is enough to turn on the light, to turn off you need the absence of a signal from both.

For the ceiling was chosen tape SMD 5050, 300 LEDs for 5 meters (12V 72W). Monochrome with a warm yellow tint. The tape is switched on via the relay module.

For night illumination was selected tape SMD 5050 RGB, 150 LEDs for 5 meters. Only blue color is activated (less wires). Control through the transistor module L298N (note that the inversion of the output signal in this module, the value 255 in the program will turn off the LEDs). The brightness of the tape is set to the minimum so as not to blind the eyes.

Led tape
image
image

The choice of the day / night lighting mode is carried out either by a push-button switch on the wall, or by a remote control with the same priority. Software is implemented on the principle of the switch through operation.

I decided to make the body of the controller, the relay and the Transistor module myself from the terminal box, securing all the equipment on the printed circuit board, it turned out quite reliably. The box itself is attached to a DIN rail. On the reverse side of the board, to reduce the number of wires, there are soldered connections, and terminal blocks for input power supply 12V are soldered to the board, which feed both the controller and the tapes.

Controller
image
image

Below is the program code for the Arduino.

Code
#include "IRremote.h" int calibrationTime = 10; byte CellingLed = 9; /*   */ byte ledB = 6; //   byte pirDoor = 5; //   byte pirMirror = 4; //   IRrecv irrecv(2); // IR  byte dayNight = 7; //  ,  - / decode_results results; byte irSignal = 0; byte buttonstate; long unsigned int moveTime; //        long unsigned int pause = 30000; // ,      boolean movementDetected = true; // true -   boolean moveFlag; //         void setup () { irrecv.enableIRIn(); Serial.begin(9600); pinMode(CellingLed,OUTPUT); pinMode(ledB,OUTPUT); digitalWrite(dayNight, HIGH); //      7 pinMode(pirDoor,INPUT); pinMode(pirMirror,INPUT); digitalWrite(pirDoor, LOW); digitalWrite(pirMirror, LOW); //    Serial.print("Calibrating"); for(int i = 0; i < calibrationTime; i++) { Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); } void loop() { if ( irrecv.decode( &results)){ delay(300); if (results.value == 0xFF3AC5) {irSignal = 0;} /*       */ if (results.value == 0xFFBA45) {irSignal = 1;} irrecv.resume(); } //    (/, IR /) byte DayNightSwitch = !digitalRead(dayNight); byte a; byte b; if (DayNightSwitch == LOW) {a=1;} else {a=0;} if (irSignal == 1) {b=1;} else {b=0;} if (a==b) {buttonstate = 1;} else {buttonstate = 0;} byte pirstate; if (digitalRead(pirDoor) == HIGH || digitalRead(pirMirror) == HIGH) { pirstate = HIGH;} if (digitalRead(pirDoor) == LOW && digitalRead(pirMirror) == LOW) { pirstate = LOW;} //   if(pirstate == HIGH && buttonstate == 0 ) { if(movementDetected) { movementDetected = false; Serial.println("Motion detected"); delay(50); } moveFlag = true; analogWrite(ledB,240);//   digitalWrite(CellingLed, LOW); } if(pirstate == LOW & buttonstate == 0 ) { if(moveFlag) { moveTime = millis(); moveFlag = false; } if(!movementDetected && millis() - moveTime > pause ) { movementDetected = true; Serial.println("Motion finished"); delay(50); analogWrite(ledB,255); } } //   if (pirstate == HIGH && buttonstate == 1 ) { if(movementDetected) { movementDetected = false; Serial.println("Motion detected"); delay(50); } moveFlag = true; digitalWrite(CellingLed,HIGH); analogWrite(ledB,255); } if (pirstate == LOW && buttonstate == 1 ) { if(moveFlag) { moveTime = millis(); moveFlag = false; } if(!movementDetected && millis() - moveTime > pause ) { movementDetected = true; Serial.println("Motion finished"); delay(50); digitalWrite(CellingLed,LOW); } } } 


This lighting scheme has been working for me for half a year already, in general, it is comfortable with its functionality. Of course, there are ideas on modernization, in particular, I would like to replace the remote control from infrared to radio. Add a few radio-controlled outlets. Also add a web interface to manage from mobile devices. Third-party services seem to me not reliable enough. At the moment, the inclusion, shutdown and indication of the presence of movement is already working, but this is another story.

Web interface
image

Source: https://habr.com/ru/post/396257/


All Articles