Back to Home

My implementation of the automatic inclusion of light in the toilet (and without Arduino)

smart home · diy · avr · toilet · automation

My implementation of the automatic inclusion of light in the toilet (and without Arduino)

Hello!
Articles appear on Habré on the implementation of the Smart Home. The main problem (well, or just for me) is turning on / off the light in the bathroom. It seems that the thing is not tricky - but how many options are there. After reading the articles, including here and here , I thought, “But everything could have been simpler.”

This worm whetted me for about six months. And so, when it became freer with work, I matured.
I will say that I like to do programming and radio design from school. Microcontrollers gave real joy - all at once. But Arduino is not here not because I hate him, he is redundant for this task, or because I want to be not like everyone else, I just haven’t reached him yet (or he has reached me).
Let's get back to our sheep (well, either to our light, or to our toilet). Personally, for me to draw TK in my head (yes, to draw, when you still cannot even formulate it, not just write it on paper) is much more difficult than to realize it later. After weeks of thinking, this is what I got about:
  • the light should turn on when I open the door (I go for example);
  • the light should turn on when I close the door (I went into the bathroom with the door open and closed it behind me);
  • the light should turn on when I go in without touching the door (I looked in to wash my hands);
  • auto power off after a certain time;
  • the light should not turn off when I'm inside and do not even move.

It seems like everything is logical and simple, but in none of the articles I met did not find a beautiful solution. The simplest is a motion sensor. It turns on the light when someone is on and off after a while. For my purposes, he only needs a reed switch in a pair - to keep track of whether the door is open or closed.
I don’t understand why the manufacturers still haven’t reached this point. Or did they reach, but did not reach me?
The algorithm is simple:
  • if the motion sensor works, turn on the light;
  • if the state of the reed switch has changed (the door has opened / closed) - turn on the light;
  • if the motion sensor has worked with the door closed (the reed switch is closed) - do not turn off the light until the door is opened;
  • Well, turn off the light after a while.

Now TK is clear, I need:
  • Motion Sensor;
  • reed switch;
  • MK to control this mess.

The cheapest DD (infrared) was bought, some kind of reed switch, ATTiny2313.

We disassemble the motion sensor, we see inside:

the control board with an infrared receiver and a mirror in the middle and:

PSU and relay. I was lucky, in DD there is everything that is needed: a relay, a transistor for matching, the rest of the circuit (even a diode). When the sensor is triggered, a TTL signal is issued, it is enough to intercept it, and transmit the signal from my MK instead.
In ISIS, I drew a diagram (if done, it’s beautiful)
Scheme

in BASCOM-AVR wrote the program:
The code
$ regfile = "attiny2313.dat"
$ crystal = 4,000,000
$ hwstack = 40
$ swstack = 16
$ framesize = 32

Config Porta = Output
Config Portb = Output
Config Portd = Output
Config Portd.2 = Input
Config Portd.3 = Input
Config Int0 = Rising
Config Int1 = Change
Enable Interrupts
Enable Int0
Enable Int1
Config Debounce = 300
On Int0 Dd
On Int1 Gerkon
Dim Timecount As Integer
Dim Timelock As Bit

Timecount = 0
Timelock = 0
Portb.0 = 0
Portb.1 = 0

Do
If Timecount < 200 Then
Portb.0 = 1
Else
Portb.0 = 0
End If
If Timelock = 0 Then
Timecount = Timecount + 1
End If
If Timecount> 250 Then
Timecount = 250
End If
Waitms 100
Loop

Dd:
Disable Interrupts
Timecount = 0
If Pind. 3 = 1 Then
Timelock = 1
End If
Enable Interrupts
Return

Gerkon:
Disable Interrupts
Timecount = 0
If Pind. 4 = 0 Then
Timelock = 0
End If
Enable Interrupts
Return

Made an emulation, it seems like everything works (after debugging, of course). I assembled the layout and checked (it is not so difficult to assemble such layouts, the main thing is to start):

We cut the roads in the DD and connect the circuit according to the inflamed imagination :

Checked - it worked. Automatic shutdown after about 1 minute 20 seconds (not for some reason, it just happened right away, but it worked for me), the rest of the work is in accordance with a previously invented logic.
Then I will retreat. The fact is that I have been soldering since those times when the transistors MP39 and MP42 were used. It was soldered and written a lot. When the scheme I developed (and especially the program) starts working the first time - I feel discomfort, it rarely happens to me. A couple of hours were killed for testing, I did not find any bugs, it continued to work.
Assembled into a working version (LUT did not come in handy):


With the help of adhesive tape and someone's mother, all this was insulated and fixed in the case. As a result, the resulting instance does not externally differ from the original one, even the connection diagram has not changed (unless a couple of wires for the reed switch have been added):


The main thing is to check the operability after each step, we swam - we know.
I’ll miss the installation and other platitudes.
The wife took it without enthusiasm and called it “bullshit” (nonsense, she will appreciate it - but where to go).
Budget:
- DD - 250 p. (I did not find cheaper),
- reed switch - 38 p.,
- ATTiny2313 - 140 p. (the price is horseback, but you wanted it yesterday).

Thank you in advance for constructive criticism.

Read Next