Wassup ladies and gentlemen! This tutorial will teach you how to light up an LED with a switch using Arduino. Firstly, make sure your work environment isn't a mess (like mine). Also, you'll be playing with electricity, and, although these are very small voltages, be sure not to injure yourself. Secondly, get your materials. I have the ones I used in the picture:
- Some wires
- A resistor (I used a 10-ohm resistor, which is kinda low, but you can use anything)
- A breadboard
- An LED
- An Arduino Uno (I used a ripoff, but it works the same)
- A switch
- A computer with the Arduino IDE
Alrighty, now that's out of the way, we can get started!
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Giving Power to Your Breadboard
The first step is to hook up your power and ground pins. Take 1 wire and insert one end into the pin that says "5V" and the other end into the column under the "+" symbol on the breadboard. In the picture, I did this with the red wire. Take another wire and insert one end into the pin that says "GND" and the other end into the column under the "-" symbol on the breadboard. I did this part with the black wire. BOOM, that's all there is to it.
Step 2: Attaching the LED
Now that you have power and ground, it's time to hook up the LED. Take another wire and insert it into pin 7 (I put it in pin 7, but you can put it any digital pin you like). Take the other side of the cable and insert it into a hole on the breadboard. Next, take the 1st end of your resistor and place it into a hole in the same row as the row you inserted your wire. We'll use this resistor to prevent burning out the LED. Take the 2nd end of your resistor and place it into a different row. Finally, take the long end of your LED, place it in the same row as the row you placed the 2nd end of your resistor, and insert the shorter end of the LED into the column under the "-" symbol. BAM, You've set up your LED!
Step 3: Setting Up the Switch
Now that you have your LED in place, you have to set up the Switch on your breadboard. The switch should have 3 pins: the 2 pins on the end are the ground and power pins (either pin could be ground and power, it doesn't matter which). The middle pin is the data pin, and it's what we'll use to record the state of the switch. I've provided a picture to help you build it. The first step is to put your switch on your breadboard so that each of the 3 pins is in a different row. Next, hook up the left-most pin to a hole under "-" column and the right-most pin to a hole under the "+" column (as I said before, either pin could be ground or power. Therefore, you could do it reverse so that the left-most pin is connected to the "+" column and the right-most pin is connected to the "-" column). Finally, connect the middle pin of the switch to digital pin 8 on the Arduino (or any other digital pin). And that's it! You've completely set up your breadboard, and it's time to move onto the code!
Step 4: Coding It!
Before you start coding, make sure that under "Tools" that your board is set to "Arduino Uno" (or whatever board you are using). Once that's in place, first thing you gotta do is to make variables representing your switch and LED. Since I plugged my switch pin 8, I created a variable called "swich" ("switch" is a reserved word so you can't use it as a variable) and set it equal to 8. Similarly, since I plugged my LED into pin 7, I created a variable called "LED" and set it equal to 7.
Next, under the "setup" function, I made the switch an input and the LED as an output with the "pinMode()" function (I also started serial monitor communication, but that's not a necessary component of your code). The code under the "loop" function is the real meat of the program. It consists of an if-else statement, and if you don't know what it is, I definitely recommend looking at the Arduino reference page. Since I only want the LED to turn on if the switch is HIGH, in the conditional, I wrote "digitalRead(swich) == HIGH." This will check to see if the switch is HIGH. If it is, it will execute the statement under the "if": "digitalWrite(LED, HIGH)," which will make the LED turn on. If "digitalRead(swich) == HIGH" is false, meaning that the switch is not HIGH, it will move to the code under the "else": "digitalWrite(LED, LOW)," which will turn the LED off.
And there ya have it! The code for the switch and LED!
Step 5: What Should Happen
I included this video to show you have my switch and LED turned out. If you have any trouble, feel free to leave a comment on that video on Youtube, and I'll get back to ASAP.