Arduino Code Structure
When you create a new code in Arduino IDE, you will see two functions: “setup ( )” and “loop ( )” . They construct the minimum code structure required to program the Arduino.
- The “setup” function always runs at the start of new code (when new code is loaded, or when the Arduino powers up). This is a good place to put initialization.
- The “loop” function is automatically called repeatedly after the setup routine is done. Both function are essential for an Arduino code.

“Blink” Example
Arduino IDE provides a ton of built-in example codes. Most third-party Arduino libraries also provide example codes. These codes are excellent templates to build your own code. You can find these example codes under Files-> Examples.

One of the simplest codes for the beginners is the “Blink” code. It is a built-in example that controls the on-board LED on Arduino board. Open the “Blink” code at Files-> Examples-> 01. Basic-> Blink. You will see the following code:

Connect your Arduino to your computer. Select correct port and board name on the IDE (Check Arduino board page if you don’t know how to do it). Upload your code by clicking .
If everything correct, you should see the LED label with “L” on your Arduino board is blinking:
Let’s take a look at code:
- Note how the two sections are used to initialize pins and to write high and low values to the LED pin, and delay. Note that the code does NOT have to tell the loop code to keep going over/over… it just does!
- we see that the code wants to change a pin number 13 to an output (Arduino pins default to inputs). This lets us command whether the line will be “on” (5 volts) or “off” (0 volts). We sometimes call this “high” or “low”, and the Arduino environment even defines the variables HIGH and LOW to allow you to set these variables. So, why not just write 5 and 0 for the voltage? Because some microprocessors use different voltages to represent “on” or “off”. Many are 3 volts or even 12 volts for “on”, and some are -5 volts or -12 volts for “off”. Additionally, we’ll later learn how to command a specific voltage, like 3 volts. So, Arduino will get confusing later to determine if this means “on” or “kind of on” for some microprocessors. Thus, the community likes the terminology “HIGH” and “LOW”
- Another important function is the command “delay”. This tells the Arduino to keep current voltage at all pins for a specified number of milliseconds, and there are 1000 milliseconds in one second. You can try to delete “delay” command and see what happens! The reason these are needed is that the microprocessor is EXTREMELY fast, so that it can loop through many millions of lines of code each second. If you don’t add delays where you want something to stay on, you might not see anything happen even though the pin was HIGH for a brief period. Your eye is only sensitive to things that last longer than about 1/20 of a second, so anything shorter than this just looks like a blur.
Need more help?
Now you have a basic idea about how to use Arduino, you are ready for the lab. If you need more help on Arduino coding, Arduino Language Reference provides a ton of useful documentations. If you would like to know more about Arduino, check Arduino official website for more fun projects!