This is the ultimate LED tutorial. This will give you the basic building blocks needed to do just about anything with LED's. Pictured are all the things you need for this whole tutorial, which consists of a couple different mini-projects. The pieces are:
a) Breadboard - If you're going to do more than one project ever in your life (you are.) then get the bigger one. It will make things much easier later.
b) Jumper Wires - again, you won't need all of these for this one project but it's nice to have short wires that aren't as flexible to make nice neat breadboard setups. Down the road, an organized breadboard can be the difference between an easily fixed problem and a computer being thrown out the window through pure frustration. That being said, you should also get.....
c) Flexible jumper wires - good to have, and so cheap.
d) Potentiometer - known as a "Pot" for short. voltage divider usually used for calibration or adjusting an analog input
e) LED's - one red, one green
f) Resistors - 1000 ohm, colored brown-black-red-gold
g) Arduino Uno - you can really use any Arduino but I have an Uno so all of these tutorials are built on the Uno.
Step 1: Turning on the LED
First, we're going to turn on the LED. Grab your Uno, and set it up the way it is in the picture. You'll need to run a jumper from pin 10 (yellow wire) to the breadboard. Use a 1K (1000 ohm) resistor to go to the anode, or positive end of the LED. LED's will only work if the anode and cathode are at high voltage and low voltage respectively. Run the other wire (pictured as green wire) to ground. Then upload the code.
About the Code:
The digitalWrite function sets the pin at either a LOW (low voltage) or HIGH (high voltage) state. This code will cause the LED to flash.
Step 2: Adjusting LED Brightness With a Potentiometer
For this, there actually is no code. First, hook the three pot pins into different rows of your breadboard. Attach the 5V line to the middle of the pot, then using a 1k resistor, tie the anode (long end) of the led to the side pin of the pot (doesn't matter which side). Then just take the cathode of the LED to ground. Now, when you turn the pot, you should see a varying brightness in the LED. This is due to the fact that the potentiometer is a voltage divider. Turning the knob one direction will increase the electrical resistance and turning the knob the other way will decrease it.
For extra credit: attach another LED to the un-used pin of the pot via a 1K resistor, then the other end of that LED to ground. As you turn the knob, one LED will get brighter while the other gets more dim.
Step 3: Using a Potentiometer to Trigger an LED
This example goes more in depth with inputs and outputs. You're going to send the potentiometer value to the Arduino by assigning analog pin A0 as an input. An analog input will send the Arduino an integer between 0 and 1024. In the code, an "if statement" is used to tell the Arduino that if the pot value gets above a certain value, then set the LED pin to a HIGH state (turn it on). Otherwise, it will turn off.
In this example, the LED is set up the same way as before. Use a jumper to go from pin 10 to a resistor, then the anode of the LED, then cathode to ground. The pot is also set up similarly. Take a side pin to 5 volts (in the picture, I have the red rail set as a 5 volt rail, and blue as ground), and the other side pin to ground. A jumper will go from the middle pin to analog pin A0.
Step 4: Switch Between a Red and Green LED Using a Potentiometer
This is simply an extension of the previous step. Just attach another LED in the same fashion as the first, but go from pin 9. Then, in the code, notice some extra if statements that now essentially tell the Arduino that above 1000 turn on one, and below 200 turn on the other.
These examples are simple, but are basic building blocks to understanding how the Arduino interacts with the environment. I plan on uploading more tutorials that will be demos for all the sensors that I can possibly get to work. For more information about me and my projects, as well as where to buy the books and associated parts, check my blog at www.thepartslist.com
Thanks! See you next time!
RGB leds to come.