Arduino with an external Switch
This is part of the starter blog for Arduino basics. Check out here to see all of them.
In many situations we need an external switch to control something in the circuit. Say you make an RC car, of course you will need to control it via a remote! which will have buttons on it. So today we are gonna light an LED with a switch as a basic. Once you go through it you can extend the same concept to any control any component say motor, relay and so on. Lets get started!
~What you need~
You are going to need the following parts for building the circuit. Of course your PC is needed as well.
- Arduino UNO
- Switch (may be toggle or button)
- 10k resistor
- Connecting wires.
Following is an image of all the parts.
~What to do~
Now lets get to the circuit. Below is the circuit diagram of the project. Its pretty much self explanatory, but I’ll still guide you through the steps. Keep reading.
- First connect one end of the 10k resistor to gnd(ground) pin and the other end to pin 6.
- Now connect the switch between 5V pin and pin 6.
- Take an LED and connect its -ve terminal to gnd pin.
- Connect the other terminal to pin 12 via the 1k resistor.
Type the following code in the IDE and upload it.
const int switch_pin = 6;
int state = 0;
state = digitalRead(switch_pin);
That’s it you are done! Now toggle the switch and see the LED switch on and off :-)! Enjoy!!
Now you must be thinking what now. Of course you can improve on the above circuit and add some stuff, but why not move on? to some other project? Check out the following links if you wish.