- Find this text in the Blink code: “void setup()”
- Notice the open curly bracket, {, that follows that text and the closed curly bracket, }, a few lines below.
- Notice the line of code “pinMode(LED_BUILTIN, OUTPUT);” is written between the curly brackets.
STEM Connections
The blink code is divided into different sections. The first section of this code begins with the text void setup(). Setup is a piece of code that gets the physical Arduino board ready to run the rest of the program. Fragments of code like this that perform defined tasks are called functions. There are many different functions in the Arduino programming language for many different defined tasks. The setup function will only run once after each time the Arduino board is powered up or reset.
There are two curly brackets ({ and }) that come after the void setup() text. Everything in between those brackets gives information to the setup function. The setup function ends after the closed curly bracket “ } “. Notice the setup function begins after the text “void setup()” and ends after the text “pinMode(LED_BUILTIN, OUTPUT);”
“LED_BUILTIN” refers to pin 13 on the Arduino Uno board because there is also an LED built into the board that is associated with pin 13.
“pinMode” is a function that sets up pins to be in INPUT or OUTPUT mode. The Arduino is looking for two values in the parenthesis, (what pin are you addressing, and what mode is it in ). “(LED_BUILTIN, OUTPUT)” tells the Arduino that we will be using the built in LED associated with pin 13 as an output.
The function ‘pinMode(LED_BUILTIN, OUTPUT)’ does not appear
- In some versions of the Arduino software, the setup function contains the text “pinMode(13, OUTPUT);”. LED_BUILTIN is a variable name and it equals 13. Teacher notes
1. Some Arduino software versions write “pinMode (13, OUTPUT);“ and some write “ pinMode(LED_BUILTIN, OUTPUT);” LED_BUILTIN is a variable name that is set to equal 13. Humans decide which pins will control what.
2. Ask the students: Do you think it’s a coincidence that the red wire is plugged into pin 13 and the setup function sets up pin 13 to be an output?
- Answer: Not a coincidence. Any digital pin could have been used to control the LED so long as it's written in the setup and uploaded.
3. Ask the students: If I replace “LED_BUILTIN” or “13” to the number 9, and uploaded it, would my LED still blink?
- Answer: No. The hardware needs to match the software.
4. Ask the students: what would I need to change to make the LED blink again?
- Answer: Plug the red wire into 9.
NOTE: We go through these questions with the class, but don't actually change the code at this point. If you did have your students go through it, either change it back to 13 or keep in mind your change as the next steps will reference 13.