Introduction: Toggle Switch With Relay Using Arduino
Trying to make things easier and clear for school students I made this little project.
The obove schematic shows the connections
Parts:
1 - relay
2 - resistors 330Ohm
1- transistor NPN 2N2222
1- diode 1N4148
1 - pushbutton.
1 - LED
The button is connetcted internally with pullup resistor.
The diode is needed to stop the voltage (sometimes more then 4 times when the current is stopped from the coil) coming back from the coil of the relay. This is known as a "flywheel diode".
How it works: Here I used one button working as a toggle switch, which turns on and off instead of two buttons.
For a load on the relay I used just a single Led and a resistor.
Step 1: Finding Out the Coil and the Contacts
This part for most simular projects is neglected. Take you Ohmmeter and find where the coil terminals are. It will have a value more or less then 100Ohm. Find where the contacts without voltage on the coil are closed NC (normally closed) ~0Ohm. Do the same with the other contacts NO (normally open) open circuit resistance
Step 2: Transistor Connections
Looking at the picture and according to the schematic connect properly the transistor according
E (emitter), B (base), C (collector)
Step 3: The Code
/*********************
Simple toggle switch Created by: P.Agiakatsikas *********************/ int button = 8; int led = 13 int status = false; void setup(){ pinMode(led, OUTPUT); pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH } void loop(){ //a) if the button is not pressed the false status is reversed by !status and the LED turns on //b) if the button is pressed the true status is reveresed by !status and the LED turns off
if (digitalRead(button) == true) { status = !status; digitalWrite(led, status); } while(digitalRead(button) == true); delay(50); // keeps a small delay }
Step 4: Proteus Circuit
If you are using Proteus here is the file.
A very simple version of the toggle switch was in my previous post here
https://www.instructables.com/id/Most-Simplest-Toggle-Switch-With-Arduino/