Arduino Nano 33 IoT - Code Structure

To learn how to code for the Arduino Nano 33 IoT, you need to understand its code structure. This guide explains how the code for the Arduino Nano 33 IoT is organized.

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Optionally, DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter 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.
Additionally, some of these links are for products from our own brand, DIYables .

Basic Code Structure

The Arduino Nano 33 IoT sketch, also known as its code, is organized just like regular Arduino programs. It has two main sections: one to set things up and one that runs continuously.

Setup Code

  • The setup code is the code inside the setup() function.
  • The setup code runs as soon as the device is powered on or reset.
  • The setup code runs only one time.
  • The setup code is used to set up variables, define pin settings, and begin using libraries.

Loop Code

  • The loop code is the code inside the loop() function.
  • It runs right after the setup code.
  • It runs over and over again without stopping.
  • It handles the main job of the program.

Example

void setup() { // Initialize setup routines; executed once on boot: Serial.begin(9600); Serial.println("This is Arduino Nano 33 IoT setup code"); } void loop() { // Main loop routines; executed repeatedly: Serial.println("This is Arduino Nano 33 IoT loop code"); delay(1000); }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and send the code to your Arduino Nano 33 IoT board.
  • Open the Serial Monitor in the Arduino IDE.
How to open serial monitor on Arduino IDE
  • Check the result on the Serial Monitor.
COM6
Send
This is Arduino Nano 33 IoT setup code This is Arduino Nano 33 IoT loop code This is Arduino Nano 33 IoT loop code This is Arduino Nano 33 IoT loop code This is Arduino Nano 33 IoT loop code This is Arduino Nano 33 IoT loop code This is Arduino Nano 33 IoT loop code This is Arduino Nano 33 IoT loop code
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

On the Serial Monitor, you can see that "This is Arduino Nano 33 IoT the setup code" prints only once, while "This is Arduino Nano 33 IoT loop code" prints many times. This means the setup part of the Arduino Nano 33 IoT code runs only one time, and the loop part runs many times. The setup code always runs first.

※ NOTE THAT:

When coding for an Arduino Nano 33 IoT, you must include both the setup() and loop() functions. If you leave them out, an error will occur.

Other Parts

Besides the setup and loop code, an Arduino Nano 33 IoT sketch can also have parts like these:

  • Block comment: usually used to write some information about the author, the wiring instruction, the license ... Arduino Nano 33 IoT will ignore this part.
  • Libraries inclusion: is used to include libraries into the sketch.
  • Constant definition: used to define constant
  • Global variables declaration

For instance:

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-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 Arduino Nano 33 IoT setup code"); } void loop() { loop_count++; Serial.print("This is Arduino Nano 33 IoT 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 above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to compile and send the code to your Arduino Nano 33 IoT board.
  • Open the Serial Monitor in the Arduino IDE.
How to open serial monitor on Arduino IDE
  • Check the result on the Serial Monitor.
COM6
Send
This is Arduino Nano 33 IoT setup code This is Arduino Nano 33 IoT loop code, count: 1 This is Arduino Nano 33 IoT loop code, count: 2 This is Arduino Nano 33 IoT loop code, count: 3 This is Arduino Nano 33 IoT loop code, count: 4 This is Arduino Nano 33 IoT loop code, count: 5 This is Arduino Nano 33 IoT loop code, count: 6 This is Arduino Nano 33 IoT loop code, count: 7
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

We don't need to understand each line of code right now. We only need to know the overall structure. Each line will be explained in future 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!