Toggle Switch With Relay Using Arduino




About: An Electrical Engineering Teacher in Athens Greece. Most of these small projects here, are constructed for enhancing the learning of the use of Arduino as well as basic electricity and electronics for studen...

Trying to make things easier and clear for school students I made this little project.
The obove schematic shows the connections
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 know as 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 Led.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest



    Question 2 months ago

    Hi Panos, i want to make this for an battery powered project. Do you know how much current it draws? Να σαι καλά!