To start programming, we opened one of the default examples included with the arduino IDE. Selecting the
Blink sketch from File > Examples > 1.Basics > Blink. Within the IDE, you can compile your code by clicking the
checkmark in the top left corner. This converts the sketch into C code, which is then compiled to assembly code to be run
on the arduino uno. At this point, any code which contains errors will fail to compile. Once you are ready to run the program
on the board, you can select the upload button (arrow next to compile button). This will compile the code and put the program
on the uno.
The code will automatically begin running if the board is powered on. If the board loses power, the program will remain in
memory, and will run again when the board is turned on. The board can either receive power from a DC power supply (9V battery) or
from the programing cable connected to the computer. Once the Blink sketch was downloaded onto the board, the onboard LED would
blink every second. Looking at the code for the Blink program, we can see how it operates on the arduino uno.
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
This code tells the arduino that the built in LED should act as an output. This allows you to write voltage values to the LED,
and turn it off/on.
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
This is the main function which blinks the LED. Because the LED was set as an output pin in the setup function, we are able to write
a HIGH
or LOW value to it. These variables are global variable defined for the arduino. They correspond to logical high and
low values. In between turning the LED on/off the program waits for 1 second so you can actually see the LED change.
The exact specification for these functions can be found in the Arduino Reference.
In order to modify the code to blink an external LED, we simply have to modify the I/O pin which we write to. Rather than using
the global variable LED_BUILTIN
, we passed the pin number we wanted to use to the necessary functions. Now, setup and loop
functions looked as below:
void setup() {
// initialize digital pin 11 as an output.
pinMode(11, OUTPUT);
}
void loop() {
digitalWrite(11, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(11, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
This completed the software necessary to make the system work, but we still had to wire the external LED to the digital pin. In order to prevent the pin
from sourcing too much current and possibly ruining the pin, we connected the LED in series with a 300 ohm resistor. The ground connection is then
taken from the arduino's ground. This is simply another pin on the board, and is necessary for the voltage on the digital output pin to have any meaning as
voltages are always from one location to another. Once we hooked up the entire circuit and downloaded the program to the uno, the external LED blinked every
second just like the onboard LED in the prior section.
Once we confirmed this worked, we adjusted the code and external circuit to test all of the digital pins on the board. In the code, we had to change
the pin number that we set as a digital output and wrote to. In the circuit, we had to change the pin that the LED was connected to to match the one set
in code.