Arduino Nano - Code Structure

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Basic Structure

The Arduino Nano code consists of two sections: the setup code and the loop code. The setup code is executed once when the program starts. The loop code is executed continuously until the program ends.

Setup Code

  • Code in the setup() function is executed right after power-up or reset. It is only executed one time and is used for initializing variables, pin modes, and starting to use libraries, etc.

Loop Code

  • The loop() function is code that is executed immediately following the setup code. It is repeated indefinitely and is used for the primary purpose of the application.

Example

void setup() { // put your setup code here, to executed once: Serial.begin(9600); Serial.println("This is setup code"); } void loop() { // put your main code here, to run repeatedly: Serial.println("This is loop code"); delay(1000); }

Detailed Instructions

  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button to transfer it to the Arduino Nano.
  • Open the Serial Monitor.
  • Check the output on the Serial Monitor.
COM6
Send
This is setup code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code This is loop code
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

You can observe that “This is the setup code” is printed only once, however “This is loop code” is printed multiple times. This implies that the setup code is executed only once, while the loop code is executed repeatedly. The setup code is executed first.

※ NOTE THAT:

The setup() and loop() functions MUST be included in Arduino Nano code. Failing to do so will result in an error.

Optional Parts

In addition to the setup and loop code, an Arduino Nano sketch may include:

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-code-structure */ #include <Servo.h> #include <LiquidCrystal.h> #define MAX_COUNT 180 Servo servo; LiquidCrystal lcd(3, 4, 5, 6, 7, 8); int loop_count = 0; void setup() { Serial.begin(9600); lcd.begin(16, 2); servo.attach(9); Serial.println("This is setup code"); } void loop() { loop_count++; Serial.print("This is loop code, count: "); Serial.println(loop_count); lcd.print("Hello World!"); servo.write(loop_count); if(loop_count >= MAX_COUNT) loop_count = 0; delay(1000); }

Detailed Instructions

  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button on the Arduino IDE to compile and upload the code to the Arduino Nano.
  • Open the Serial Monitor.
  • Check out the output on the Serial Monitor.
COM6
Send
This is setup code This is loop code, count: 1 This is loop code, count: 2 This is loop code, count: 3 This is loop code, count: 4 This is loop code, count: 5 This is loop code, count: 6 This is loop code, count: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

We do not have to comprehend code line-by-line at present. We just need to be aware of the code structure. The line-by-line code will be elucidated in the upcoming tutorials.

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!