Structure of Arduino Programming
Page 1: Introduction to Structure of Arduino Programming
Arduino programming plays a vital role in the development of embedded systems and IoT applications. As
Arduino boards are commonly used microcontrollers for prototyping, understanding their programming
structure is crucial. The Arduino sketch structure ensures that hardware components function as expected
through software control.
The Arduino development environment simplifies programming with a clear structure that separates initial
setup and continuous task execution, enabling users to easily interface sensors, actuators, and other
modules. This structure is consistent across all Arduino projects, promoting both modularity and ease of
debugging.
Page 2: Basic Structure of an Arduino Program
The core of every Arduino sketch (program) includes two primary functions: setup() and loop(). These
functions form the foundation of Arduino code execution.
1. void setup()
Introduction:
The setup() function is executed only once when the Arduino board is powered on or reset. It is primarily
used to initialize pin modes, configure peripherals, and set up communication protocols like Serial.
Example:
void setup() {
pinMode(13, OUTPUT); // Set pin 13 as output
Serial.begin(9600); // Initialize serial monitor
2. void loop()
Introduction:
After setup() completes, the loop() function is executed repeatedly in an infinite loop. This function contains
Structure of Arduino Programming
the main logic that needs to run continuously, such as reading sensor data and controlling outputs.
Example:
void loop() {
digitalWrite(13, HIGH); // Turn on LED
delay(1000); // Wait 1 second
digitalWrite(13, LOW); // Turn off LED
delay(1000); // Wait 1 second
Page 3: Pin Configuration and Built-in Functions
Understanding how to configure pins and use built-in functions is fundamental to Arduino programming.
These allow communication between the microcontroller and external devices.
Pin Configuration
Introduction:
Arduino pins must be set as either input or output using the pinMode() function. Once configured, pins can
read sensor data or control devices like LEDs and motors.
Key Functions:
- pinMode(pin, mode) - sets pin as INPUT or OUTPUT
- digitalWrite(pin, value) - sets pin HIGH or LOW
- digitalRead(pin) - reads digital input value
Example:
pinMode(7, INPUT);
int state = digitalRead(7);
Timing Functions
Introduction:
Structure of Arduino Programming
Timing is crucial in embedded systems. Arduino provides built-in delay and time functions to control
execution flow.
- delay(ms) - pauses the program for specified milliseconds
- millis() - returns time since the board was powered on
Example:
unsigned long now = millis();
if (now - previousTime > 1000) {
// do something every second
Page 4: Variables, Data Types, and Control Structures
Variables and Data Types
Introduction:
Variables store information for computation and control. Arduino supports standard C++ types like int, float,
char, and boolean. Constants can be defined using const or #define.
Example:
const int ledPin = 9;
int sensorValue;
Control Structures
Introduction:
Control structures determine program logic. These include conditional checks (if, else) and loops (for, while,
do-while), which are essential for decision-making and task repetition.
Example:
if (sensorValue > 500) {
digitalWrite(ledPin, HIGH);
Structure of Arduino Programming
} else {
digitalWrite(ledPin, LOW);
Loop Example:
for (int i = 0; i < 5; i++) {
blinkLED();
User-Defined Functions
Introduction:
Functions break code into reusable blocks. This improves readability and debugging.
Example:
void blinkLED() {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
Page 5: Execution Flow and Final Example
Program Flow
Introduction:
The execution of an Arduino sketch follows a predictable pattern:
1. Power ON or reset
2. setup() runs once
3. loop() runs repeatedly
4. Arduino continues executing loop() as long as it has power
Structure of Arduino Programming
Final Example: Automatic Light Control
const int lightSensor = A0;
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
void loop() {
int value = analogRead(lightSensor);
Serial.println(value);
if (value < 300) {
digitalWrite(ledPin, HIGH); // Turn on light
} else {
digitalWrite(ledPin, LOW); // Turn off light
delay(1000);
Conclusion:
The structure of Arduino programming is simple, consistent, and easy to learn. By separating initialization and
logic into setup() and loop(), developers can focus on controlling hardware reliably and repeatedly. This clear
separation of tasks makes Arduino ideal for embedded systems and IoT applications.