The Arduino Chronicles 3: Arduino Mega Basics/Resistors and LEDs
Note: For the rest of my stories I will be using an Arduino Mega 2560 Microcontroller Board Most Complete Starter Kit. However, you can easily translate the instructions to other boards and still learn about the parts without owning the materials.
Part 1: Installing and Setting up the Arduino IDE
Installation
Install the open-source Arduino Interactive Development Environment (IDE) from this website.
Open the IDE and Connect the Board
Open the Arduino IDE by clicking the desktop icon. Then connect the Arduino MEGA to your PC via a USB. You may need to use a dongle if you own a Macbook. Observe that the green “ON” button is lit up, indicating the board is connected to a power source.
You will now want to configure the IDE for your specific board and connection. First, click “Tools”, then “Board” then select “Arduino Mega 2560” or “Mega ADK”. Next, select “Tools”, then “Serial Port”. If there is only one, select that one. If there is more than one you will have to select it and experiment to see if it is the correct one. Here is how you can do that:
- Click “File”, then “Examples”, followed by “0.1Basics” and finally “Blink”.
- Upload “blink” to the Arduino by clicking the right arrow icon at the top. You should see a pair of LEDs on the board rapidly flashing. This indicates code downloading into your Arduino. After that, your code automatically starts. The LED labeled “L” near the USB connector should blink one time every two seconds.
- If this is not happening, select a different serial port and repeat the process above until you get the desired outcome.
Part 2: Resistors and LEDs
The whole point of this series is to go very into depth about how this all works. If you are already familiar with these hardware components feel free to skip this section.
Resistors
Voltage (V)= Current (I)* Resistance (R), V = IR
We will get into why this is, but for now, just keep this equation in mind.
Voltage is the PRESSURE from a power source that pushes charged electrons through a conductor, a material that allows electricity to flow through it. If you have ever taken a fluid dynamics course, the second law of thermodynamics is that energy flows from a higher to lower pressure. So once a power source such as a battery is connected to a conductor the voltage, or “potential difference”, between the poles of the battery forces electrons out of the negative end. It is measured in volts. Read more about voltage here.
Current is the RATE at which electrons flow past a certain point in an electrical circuit. In order for electrons to flow through a conductive material, there will need to be a load, or something that demands energy, attached: something with a positive charge that negative charge (i.e. electrons, since they are essentially the negatively charged part of an atom) are drawn to. The way that when you pull the negative end of a magnet away from the positive end they will be drawn towards each other. (Actually, an infinitesimally small amount of electrons will still flow, this is called leakage current, and it is not important for most practical applications, so for the purposes of this article don’t worry about it.)
Current is measured in Amperes (A). It is a number of electrons moving past a single point in a wire in one second. Specifically, one ampere is one coulomb, or 6.24 x 10^18 electrons, moving past a single point in a circuit in 1 second. Learn more about current here.
In summary, electrons flow through a conductive material when attached to a load. So where does resistance come into play?
We have talked about conductors or materials that allow electrons to flow through them easily. Insulators are materials that generally do not let electrons flow freely through them.
Resistance is measured in ohms. One ohm is the voltage required to make one ampere flow through a circuit. So a resistor is simply a component that makes it harder for electrons to flow through the circuit. If you cut one open you will see an insulating ceramic rod running through the middle with a copper wire wrapped around the outside. Learn more about resistors here.
So, if R is the load, voltage is equal to the resistance multiplied by the current. How many electrons can move along the material given the electrical potential difference in the power source, V=I*R.
If you want to know more about all of this look up Alternating Current (AC) versus Direct Current (DC)!
LEDs
First things first, an LED is a light-emitting diode. A diode is a simple semiconductor device. It consists of an N-type material bonded to a P-type material. A semiconductor is a material with a varying ability to conduct electrical current. Usually a poor conductor with another material added to it. A semiconductor with extra electrons is N-type, while one with extra holes (or essentially extra positive charge) is called P-type. When no voltage is being applied to the diode electrons from the N-type material fill holes in the P-type along the border between layers. This creates what is called a “depletion zone”.
This essentially makes the material an insulator. Electrons are not moving easily. However, if you connect the N-type side of the diode to the negative end of the circuit and the P-type to the positive end. The anode is the positive side and the cathode is the negative. This begins to dissipate the depletion zone until it is gone.
This has the effect of producing light. Without going into extreme detail, just know that atoms have electrons, and electrons are a certain distance from or arranged in a certain way around the nucleus of the atom. These are called orbitals, and different orbitals have different energy levels.
When electrons move to fill the P-type holes they move into a different (lower) orbital, so they release energy in the form of photons, some of which are visible to the human eye depending on their frequency.
To learn more about diodes see this website.
Part 3: Blinking LED
Set up
First, we will set up and run the program, then we will talk about what is actually happening.
- Connect a 250-ohm resistor (I used 220 ohms because it is close enough and does not have to be exact, find a resistor calculator here) between the anode (longer wire) of the LED and the +5V pin on the Arduino. I used an alligator clip for mine.
- Connect the short LED wire with IO/PWM pin 10.
- Modify the blink.ino code on line 10 so that the LED pin is now pin 10.
- Compile and upload. Verify your LED attached to pin 10 is blinking.
Code
As stated, the setup function runs one time. This is saying that pin #10 (the hole with a 10 next to it in the board) is outputting, as opposed to taking in data from something, a sensor is an example. This is basically saying that pin 10 will be outputting energy to the LED to light it. Followed by the loop, in which the comments should be clear as to what the functions' purposes are.
Explanation
The Arduino board has input/output pins. The moral of the story is that these pins either connect the LED to ground or to the +5V power supply, effectively turning on and off the LED.
You can find a great explanation of how this all works here and here if you are interested in how exactly the pins work on a circuit level.