- Before you begin: breadboarding circuits
- Making the circuit
- Writing the code: blinking Pins 3 and 4
- Our Blink2 code on GitHub
- Next Lesson
We are going to build two simple LED circuits:
- LED Circuit 1 will be the exact same as before with the LED anode facing Pin 3 and the cathode facing ground. When we drive Pin 3
HIGH(5V), the current will travel through the LED to
GND. In this circuit, Pin 3 is the current source.
- LED Circuit 2 is similar but different. Here, we’ll hook up a second LED with the anode facing away from Pin 4 (instead, towards 5V) and the cathode facing toward Pin 4. When we drive Pin 4
HIGH(5V), the LED will turn off because no voltage difference exists between the two ends of our circuit. However, if we drive Pin 4
LOW(0V), the LED will turn on. In this circuit, Pin 4 is the current sink.
Yes, this can be a bit confusing at first (“wait, the LED turns off when Pin 4 is
HIGH?!?!”). But you’ll gain understanding by completing this lesson. In the animation below, pay attention to the current direction in each circuit. Notice how they’re opposite!
Our materials are almost the same as before but this time, we are going to make two separate LED circuits (with the same components). So, we need two red LEDs and two 220Ω resistors. Now that we’re using more components, we’ll also need a breadboard—which will make it easier to make a clean, organized circuit.
|Breadboard||Arduino Uno, Leonardo, or similar||2 Red LEDs||2 220Ω Resistors|
We will increasingly be using our breadboards in these lessons so now is a good opportunity to revisit how to use them. If you’re unfamiliar please read our breadboarding guide and watch the following video:
Now wire up the second LED circuit. This time, however, connect the LED cathode (short leg) to Pin 4 and the resistor to the 5V rail.
Let’s write code to blink the LEDs hooked up to Pins 3 and 4.
Importantly, the Pin 3 circuit (LED Circuit 1 i) will turn on with
digitalWrite(3, HIGH) whereas the Pin 4 circuit (LED Circuit 2) will turn off with
digitalWrite(4, HIGH). Why? Recall that current always flows from high voltage potential to low voltage potential.
When Pin 3 is
HIGH (5V), there is a voltage difference between Pin 3 and
GND so current flows from Pin 3 to ground. When Pin 4 is
HIGH (5V), however, there is no voltage difference across the circuit (from Pin 4 to 5V) and thus, no current. This behavior is illustrated in the animation below.
Let’s write the code!
We did it! Now compile and upload the code.
And here’s a top-down video with the code window:
You can access our Blink2 code in our Arduino GitHub repository. It’s also displayed below:
In the next lesson, we will use a new component—an RGB LED—to output a variety of LED colors beyond just red and learn about the difference and how to use Common Anode vs. Common Cathode RGB LED designs.