This week's assignment, was to add an output device to a microcontroller board you've designed and program it to do something.
Last week I worked with a LDR sensors using my fabduino board, this week i decided to continue using the LDR plus a relay to control (turn on /off) devices with external power supplies, with the main objective to control a 12v dc motor, for being able to attach a mechanism later.
Searching for info about how to use relays with arduino i found the following info:
The circuit needs a switching transistor. A simple NPN model will do, However, when selecting your transistor you need to ensure that it can handle the current of the relay coil. The data sheet for your transistor will mention a value called Ic – collector current, IT MUST BE GRATER than the one of the relay coil. Also, when the coil switches off, all the electromagnetic radiation in the coil needs to go somewhere, otherwise it could pulse down into the transistor. Therefore a simple 1A power diode (such as a 1N4004) is placed across the coil, allowing current to flow through the coil and the diode as a loop until it dissipates. For powering the relay coil, if yor relay works on 5v (like mine) you can use the arduino 5v pin to power the circuit, if the coil works on grater voltages it will need its own power supply.
This is my final schematic:
Next I routed the board, this is the final version with SMD LDR, I did a previous version with a thru-hole LDR, I forgot to save the file, this one have 2 more terminals, one for being able to remove the LDR-pin wire instead soldering it to a pad and the other one is an extra GND.
This is my program for the "Fabduino" board:
A LDR senses the light, if the amount of light is below the programed range, the relay turns off the DC motor, and if the amount of light is over the programed range the relay turns on the motor thru the relay.
I also made some little modifications to the program for turning on 12V LED lights when is dark.
Finally here is a video to show the functioning of both programs: