Arduino - Program Structure
Arduino - Modern Program Structure
Section titled “Arduino - Modern Program Structure”Welcome to the world of Arduino! In this chapter, we’ll explore the fundamental structure of an Arduino program, which is called a sketch. Understanding this structure is the first and most crucial step to programming a microcontroller.
An Arduino sketch is written in a language based on C++. However, the Arduino environment simplifies many complexities. This is achieved by the Arduino Core, a set of libraries that runs behind the scenes. It automatically includes necessary headers (like Arduino.h) and provides a simple, two-function structure that is perfect for embedded systems.
The Two Essential Functions: setup() and loop()
Section titled “The Two Essential Functions: setup() and loop()”Every Arduino sketch must contain two special functions. Think of them as the two primary acts of a play: setting the stage, and the performance itself.
void setup(): This is the ‘stage setup’. It runs only once when the Arduino board is powered on or reset.void loop(): This is the ‘performance’. Aftersetup()finishes, this function runs continuously in a loop, over and over, as long as the board has power.
The setup() Function
Section titled “The setup() Function”void setup() { // Initialization code goes here. // This runs once at startup or after reset.}- PURPOSE: The
setup()function is where you prepare your sketch to run. Use it to initialize variables, configure pin modes (input/output), and start communication libraries (likeSerialfor debugging). - EXECUTION: It is the first part of your code to execute after the board’s bootloader completes its own startup sequence.
The loop() Function
Section titled “The loop() Function”void loop() { // Main code to be executed repeatedly goes here.}- PURPOSE: The
loop()function is the core of your program. It’s where you’ll spend most of your time writing code that reads sensors, makes decisions, and controls actuators like LEDs and motors. - EXECUTION: This function cycles indefinitely, allowing your program to constantly react to its environment and update its state.
A Practical First Sketch: Blinking an LED
Section titled “A Practical First Sketch: Blinking an LED”The ‘Hello, World!’ of electronics is blinking an LED. It’s a simple yet powerful way to confirm your code is running on the hardware. Most Arduino boards have a built-in LED that you can control, typically accessible via the LED_BUILTIN constant.
// The 'Hello, World!' of microcontrollers: Blinking an LED.
// Using the built-in constant 'LED_BUILTIN' makes this sketch portable// across different types of Arduino boards.const int ledPin = LED_BUILTIN;
// The setup() function runs once when you press reset or power the board.void setup() { // Initialize the digital pin as an output. pinMode(ledPin, OUTPUT);}
// The loop() function runs over and over again forever.void loop() { digitalWrite(ledPin, HIGH); // Turn the LED on (HIGH is the voltage level) delay(1000); // Wait for a second (1000 milliseconds) digitalWrite(ledPin, LOW); // Turn the LED off by making the voltage LOW delay(1000); // Wait for another second}Development Best Practices
Section titled “Development Best Practices”- Use
constfor Constants: Declaring pin numbers and other fixed values as constants (e.g.,const int ledPin = LED_BUILTIN;) makes your code safer and easier to read. - Modern Development Environments: While the Arduino IDE is great for beginners, consider tools like VS Code with the PlatformIO extension. They offer advanced features like code completion, project management, and easier library integration.
- Version Control: As your projects grow, using a version control system like Git is essential. It allows you to track changes, collaborate with others, and revert to previous versions if something breaks.