You are on page 1of 4

Lesson 2 – External LED Blink Sketch – Written by Seven Vinton

For this sketch you are going to use the same code as you did in Lesson 1, but this time you are going to
connect an external LED to the Arduino.
If you look at the circuit above, you will see that there is a resistor connected in series with the LED.
This is there to stop too much current flowing through the LED, which is a good thing, because too much
current flowing through our LED would burn it out. The LED alone offers almost no resistance, and if you
remember the lesson on Ohm’s Law from page one you will realise that I = V/R, and if R = 0 then the
current will spike and burn out the LED. This is also not good for the I/O pins on the Arduino, so it is always
best to use a resistor.
The recommended amperage for these LEDs is around 20 milliamps. Try to work out using Ohm’s Law what
resistor we would need to bring the current down to around 15 milliamps.
I = V/R
If we were to use a 330 ohm resistor would that do it? I = 5/330
Actually, in the circuit the voltage is distributed across the components, so the LED is using up around 2
volts which means to work out the resistor value we would use the actual voltage across the resistor rather
than the input voltage Vi – Therefore the formula would be R = Vi(input voltage) – Vf(LED voltage drop)
over I(current); making it approximately 3 volts (5-2) over 15 milliamps(safe zone for LED) which equals 200
ohms. As long as you use a resistor high enough to do the job then that is fine. Most Arduino enthusiasts
use either a 220 ohm, 330 ohm, or 470 ohm resistor.

This is the schematic of the circuit for this lesson.


You can see that we have a 330 ohm resistor and an LED.
The positive end of the LED is connected to pin 13 of the
Arduino via the resistor, and the negative end of the LED is connected to ground.

The LED
This is what the LED looks like in both schematic and physical appearance:
The LED has two terminals: anode and cathode.
The anode should always be connected to the positive side of the circuit, and the cathode to the negative
side.
Current can only flow one way through the LED, so it acts like a one-way valve. When current is flowing
through the LED it lights up. You can learn more about LEDs here: http://electronicsclub.info/leds.htm

The Resistor
The resistor is made of a material that is a poor conductor and therefore it restricts the flow of electrical
current.
The coloured bands on the resistor let us know what its ohm rating is (see the chart below).
You can learn more about resistors here: http://electronicsclub.info/resistors.htm

Instructions: Unplug your Arduino from the USB port! Remember to wire the circuit
before you power up the Arduino!
Set up the circuit on page 4. Plug your Arduino into the USB port, load the ‘Blink’ sketch
to your Arduino and watch what happens.
Lesson 2b – Adding a button with your LED – Written by Seven Vinton
Please note: The learning in this section of lesson two is a little more advanced and may be difficult for some
learners.

There are many occasions in electronic and microcontroller circuits where you may want to add a button. The
scenario might be that you wish to add a ‘reset’ button, create a circuit which checks the status of a button (for user
input), and/or you may wish to add several buttons which each have specific outputs attached to them.

Buttons are one very simple way of including user input into your Arduino project system. In later lessons we will
look at how you can include inputs using other types of sensors.

The simplest way to include buttons as part of your circuit is to connect the button or buttons in series with an LED
or other output device, like the setup in this circuit (below):

Connect up this circuit and observe what


happens when:

1. You press only one button


2. You press both buttons together

In the above circuit we have two buttons connected in series with our LED and power source (+5V from Arduino).
This circuit is what is known as an ‘AND Gate’, because both button A and button B need to be pressed to allow
current to flow through and light the LED. If only one of the buttons is pressed, then the LED will not light. The
pseudo code for this system would be something like this:

If button A == on && button B == on, then LED = on, Else LED = off.

This above circuit is fine if we only want to turn an LED on which is closely connected to our circuit, but what if we
want to only have our LED light up only under certain conditions, or what if we want to light an LED that is connected
to our circuit over Bluetooth or Wi-Fi?

For these more complicated circuits we will need to build in buttons as logic inputs, and write code to check the
status of these buttons and then perform the specified functions.

For these circuits to work properly it is not just a matter of connecting one end of the switch to a digital pin and the
other side of the switch to either +5V or GND. If we set up our circuits in this way, such as the two examples below,
the voltage would randomly switch between HIGH and LOW; This is what is known as ‘floating’ voltage.

To ensure that our button input pin is either HIGH or LOW we need to either pull the pin to +5V or to GND.
The circuit that we use for this is called a ‘pull-up resistor circuit’ (We will only deal with the ‘pull-up circuit here, and
look at a ‘pull-down’ circuit in a later lesson), and in schematic view, it looks like this:

Here when the button is open the input pin is HIGH because +5V is flowing
from VCC through R1 (you would usually use a 10K resistor here) to the
input pin. When the switch is closed the current is pulled away from the
input pin to GND, and therefore the voltage at the input pin reads as LOW.
You must always have a suitable resistor in this circuit, otherwise what we
have is a short from VCC to ground, which is very bad for our Arduino.
More information: https://learn.sparkfun.com/tutorials/pull-up-resistors/

The circuit diagram for our two buttons will look like this:

And the code that we need to load onto our Arduino board (‘Two_Buttons’ in the sketches folder) looks like this:
const int ledPin = 9; // choose the pin for the LED
const int button1 = 2; // choose the input pin (for 1st pushbutton)
const int button2 = 3; // choose the input pin (for 2nd pushbutton)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(button1, INPUT); // declare pushbutton as input
pinMode(button1, INPUT); // declare pushbutton as input Here we see the code for the checking of
} the button states:
void loop(){ if (valA == LOW && valB == LOW )
int valA = digitalRead(button1); // read button state
int valB = digitalRead(button2); // read button state
if (valA == LOW && valB == LOW ) // check if the input is HIGH for both buttons - this can be changed to ||(or) or !(not)
{
digitalWrite(ledPin, HIGH); // turn LED on if both switches are pressed
}
else
{
digitalWrite(ledPin, LOW); // turn LED off
}
}

Set up this circuit (use a 10k resistor with each button) and load the code; then observe what happens when you
push the buttons one at a time and then both together. You will be able to apply this circuit setup to other circuits in
the following lessons.

You might also like