Write and Upload the Blinking LED Sketch
In order to execute the sketch for this exercise, launch Arduino IDE then
Click File -> New.
A new sketch editing window will appear. Copy the whole block of code below and paste it into the new sketch window, overwriting the empty setup()
and loop()
that was pre-created. Ensure that the board selected is Arduino Uno and the right COM port is chosen according to “Board Selection” chapter.
void setup() {
pinMode(3, OUTPUT);
}
void loop() {
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
delay(1000);
}
Click Upload, and the Arduino IDE will prompt you to Save sketch folder as. Enter Excercise_1
into the filename field and click save. Once saved, the Arduino IDE will start to compile the sketch. After the compilation is completed, the Arduino IDE will start to upload the compiled sketch into the STEMTera™ Breadboard. During this stage both yellow LEDs on the STEMTera™ Breadboard will start blinking indicating the Arduino IDE is uploading the sketch to the STEMTera™ Breadboard.
Understanding the Blinking LED Sketch
From the circuit, we know that PIN3 of the STEMTera™ Breadboard is connected to the 330Ω resistor and the Red LED. In order to turn on the LED, we need to instruct the STEMTera™ Breadboard to set PIN3 to OUTPUT in the setup()
function.
pinMode(3, OUTPUT);
The pinMode()
function shown above takes two parameters. The first parameter is the pin number and the second parameter is the mode of the pin. The above code instructs the STEMTera™ Breadboard to set PIN3 as OUTPUT.
Once PIN3 is configured as OUTPUT, we can then instruct the STEMTera™ Breadboard to either set PIN3 HIGH or LOW.
digitalWrite(3, HIGH);
The digitalWrite()
function shown above takes two parameters. The first parameter is the pin number and the second parameter is HIGH or LOW. When a PIN is set to HIGH, the PIN will OUTPUT 5V. When a PIN is set to LOW, the PIN will OUTPUT 0V (no voltage and connected to GROUND).
In order to set an ON time or OFF time, we can add a delay in between ON and OFF using the following line of code after setting a PIN HIGH or LOW.
delay(1000);
This line of code instruct the STEMTera™ Breadboard to delay for 1000 ms (milliseconds).
When ON, DELAY, OFF, DELAY are executed in a loop, the result of this exercise is the LED blinking in 1000ms ON and 1000ms OFF pattern.