Here's what I was thinking, I want to make something that would automatically switch on the light when I open the door. The first thing that came to mind: a sensor detects door opening, sends a digital signal to some kind of a switch, and there is light, simple. These days, the first thing that comes to mind is an Arduino but NO, that's just the lazy voice in my head telling me that it's just so easy to write a few lines of code. Using an Arduino in this kind of project would be such a shameful overkill that I'm not even gonna go there (and I think that we learn some useful basic electronics along the way)...
So, here's the way I built it.
Step 1: CHOOSING THE COMPONENTS
This was an easy one, there aren't a lot of things that can beat the availability, efficiency, price, and convenience of the ever so popular LED strips. I chose a white 12V one that was lying around and cut it to size(cut along the marked stripe with a scissor marking between two exposed copper pads). Light, DONE.
There are so many things you can choose from such as passive infrared(PIR), distance, light, laser... all of these could be implemented in such a way that would serve a purpose of detecting when the door opens but many of those would produce false alarms because they rely on parameters that could change without opening the door. The sensor that is most reliable in this case is a Hall Effect Sensor. Basically, a magnetic field detector. It converts presence of magnetic field into an electrical signal, meaning: magnet close to the sensor -> it outputs something OR magnet away from the sensor -> it outputs something else(opposite than the previous state). We'll go into detail about this sensor in the next step, but now let's cover the
In my case, I could've easily used a MOSFET or a beefy bipolar transistor(it would've been more reasonable and quieter) but I chose to use a relay because of one thing. This project becomes so much more versatile with a relay as a switch because it allows us to switch both DC or AC loads and loads with a much higher power consumption than a transistor could handle. Essentially, a relay is an electromechanical switch, meaning that it mechanically shorts or opens output terminals (like a standard switch) depending on the electrical signal we send it. And most importantly the circuit powering the relay is completely separate from the circuit that is switched on by the relay, meaning we can control high-voltage circuits (e.g. 220V AC) with low-voltage signals (e.g. 5V, 12V DC).
12V power supply, I'll use this to power both the circuit and the led strip. A bipolar junction transistor(NPN) to drive the relay, a diode and a couple of resistors.
Step 2: FIGURING OUT HOW THE SENSOR WORKS
So we decided to use a Hall Effect Sensor, you google it, find the part in your closest electronics store and buy it (pretty cheap). In my case, it was a TLE4905L by Infineon Technologies. I google it and find the Data Sheet straight from the manufacturer. There I find all sorts of useful information, such as pinout, maximum ratings, suggested use and so on.
First thing I'm interested in is the pinout and see that the left pin (facing the branded side of the sensor toward me) is used for supply voltage, the center pin is ground and the right pin is the output of the sensor. I hook it up like that on the breadboard and measure the voltage of the output pin with a magnet near and away and figure out two things: first, the sensor is working, it's giving out different voltages depending on the presence of the magnet, and second, I'm not using it right.
It outputs around 3V when the magnet is away regardless of my supply voltage and.. not right. I want an output of 12V as a logical HIGH. Then I look further down the Data Sheet and realize the output is an open collector of an NPN transistor and the way I'm testing it the output is just "floating", meaning it's not connected to anything so we have to do that manually. We do that by connecting the output pin of the sensor to the supply voltage with a "PULL-UP" resistor, in this case, a 1K Ohm resistor will do the job.
By inspecting the circuit description of the sensor we can figure this out: when the magnet is not near the circuit, the base voltage of the transistor is LOW (or simpler, the output transistor is "switched off") meaning that there is no current flowing through it so there's no voltage drop across our pull-up resistor and the output voltage is the supply voltage, and also, when there's a magnet near the base voltage of the transistor is HIGH (or simpler, the transistor is "switched on") so there's current flowing through it. I also believe that the output transistor is in saturated mode (collector-emitter junction acts like a short circuit) so it gives a fixed voltage, roughly 0V, on the output.
Another test with the multimeter proves that everything's working alright now. Additionally, you can visually inspect it by putting a single LED in series with the pull-up transistor and it should light up whenever you bring the magnet close to the sensor. Do this temporarily, don't keep it on there for the whole project.
Step 3: TRIGGERING THE RELAY
Now that we have a digital signal from the sensor (either HIGH or LOW voltage) we have to figure out how to use that to switch on the relay.
"Switching on" means letting current flow through the coil of the relay which generates a magnetic field that mechanically pushes the common terminal of the relay against the "Normally Open (NO)" terminal thus shorting those two terminals.
We will do this with an NPN transistor by connecting its base to the output of the sensor and it will act like a switch. When the magnet is near the sensor, it's output is LOW, or 0V, which is not enough to make current flow through the base-emitter junction of the transistor and thus not enough to let current flow through the coil of the relay meaning that the relay is OFF. In another case, when we move the magnet away from the sensor, it's output is HIGH, or 12V, which is more than enough to make current flow through the base-emitter junction of the transistor meaning that the transistor also let's current flow from it's collector to emitter switching ON the relay.
You shouldn't connect the output of the sensor directly to the base of the transistor, it needs to go across a resistor (Rb) so you limit the base current to the 1/β of the current you want to let through the collector. β is basically the current gain of the transistor and it can be found in the Data Sheet. Also, the base-emitter voltage must be over its voltage threshold (usually 0.7V). The amount of current to allow through the collector should be larger than the current the relay requires, which is around 50mA in my case(i'm using OMRON G2R-1 operating on 12V). Make sure that your transistor can handle that much current, in this case, I'm using a 2N3904 NPN transistor which can handle up to 200mA which is sufficient. This requires a bit of math which I will not go into detail but you can see it on the image.
Note the diode placement on the schematic. This diode is mandatory because of voltage spikes that the collapsing magnetic field of the relay coil generates at deactivation. If there was no diode, the transistor would be destroyed, and because there is, all the energy from the coil is dissipated through the diode and not through the transistor (this will not happen every time you switch it off, but eventually it will).
Now we wire it according to the schematic and we are pleased to hear that "click" of the relay (relay should be ON, or closed, when the magnet is not present, and OFF when there's a magnet near the sensor).
All that's left is to wire the led strip now, we will control the flow of current through the strip by attaching the +12V of the power supply to the Common terminal of the relay and, the + wire of the strip itself to the Normally Open terminal of the relay, and of course, - wire of the strip to ground. This way, we are closing the circuit with the relay and the led strip lights up when the magnet is away, vice versa, the relay disconnects the led strip from the supply when the magnet is present. Electronics- DONE(almost). Also, mind the polarity of the diode, the cathode (striped side on the diode itself) should be connected to the supply voltage.
Step 4: CUSTOM PCB
When we're sure that everything is working on the breadboard, we're ready to move our components onto a perfboard/PCB depending on your liking. Perfboard should be fairly easy because there aren't really that many components. There's one little problem though, I can't really fit in the pins of the relay into the perfboard because the spacing between them is not equal and they don't go through the board. There are ways around this, of course, but I wanted to make a custom PCB for this anyway let's get into that.
I won't go too much into detail here because, honestly, I'm still in the process of learning designing and developing custom PCBs but I'll go through what I did. We have to start off from designing the circuit board on the computer, I'll use Eagle. The advice I have for it: if anything is unclear, google it, you'll find an answer to any question you have about it.
I designed it in such way that I connect the sensor with a cable because you will probably want to move the sensor independently of the other components so I'll just put wire pads as connectors for the sensor on the PCB. Everything else will be directly soldered on the PCB. Make the traces of the relay's output a bit wider to allow a bigger current flow. We will connect the wires to the relay using a PBT Connector so we can wire all sorts of loads.
Once that is done, print out the circuit on a transparent A4 film a few times on the same sheet because we'll use the second part to overlay on top of another because the prints aren't always perfect and often have some spots that aren't printed properly. By overlaying two pieces you are minimizing the chance of those imperfections appearing on your finished PCB and you also make the print darker which is always wanted because it will be much harder for light to pass through. Next thing I did was taping that piece to a sheet of glass which will then be placed on top of the PCB because we want minimal distance between the print and the PCB. Next, turn off all of your lights(red light is ok because it doesn't affect the photoresist layer on the PCB) and place the PCB under the glass and shine a bright light into it. I used an ordinary CFL light bulb, although it works, it's not great. I've put it around 15cm above the PCB and let it sit like that for 9 minutes. After that, because we want to remove the layer of photoresist that was exposed to the light, we put the PCB in a NaOH (Sodium Hydroxide) developer. It's essentially a mix of NaOH crystals and water (experiment the ratio for best results with the light source you are using), and remember to always add crystals to the water and not water to the crystals. When that is done, wash it with water and dry. To etch to copper I'm using HCl (Hydrochloric acid) and 30% H202 (Hydrogen peroxide) of around 2:1 ratio. The etching should be done in a matter of minutes, if not, you did something wrong, most likely you didn't develop the board correctly and there is still some photoresist on the PCB. When that's done, we need to remove the layer of photoresist still present on the copper with acetone.
Now is the right time to check for any shorts with the multimeter because you don't want to find out that you have a short once you power it up and something gets destroyed because of that short. Now we can solder to it. Take your time here, apply a lot of flux (yes, it's messy but it will make soldering way easier and you can easily clean it afterward with alcohol). Then, one last inspection of shorts and wiring, also look out for bridges (unwanted connections made while soldering) and if you are confident enough that nothing will explode, power it up and test it out.
Step 5: FIN
So.. that's it, some kind of enclosure should be easy to find/3D print since it's really compact and it's ready to rock. Now that I'm looking back on the project I may have just made a glorified reed switch, but anyway I think it was useful because we learned a lot along the way and I hope you enjoyed it. I will include a few videos of how else it could be implemented. I would appreciate any feedback since this is my first Instructable.
Also, feel free to ask anything about the project and if you made it I would love to know how it turned out for you and how you used it since there are so many ways to implement it.