Introduction to Arduino and Programming Logic Study Guide

Overview of the Arduino Platform and Programming Logic

Arduino is a comprehensive electronic prototyping platform consisting of both hardware (the physical board) and software, specifically the Arduino Integrated Development Environment (IDE). It is designed to facilitate the creation of projects involving automation, robotics, and control systems in a manner that is both simple and accessible for users. The equipment necessary for a standard setup includes a computer equipped with the Arduino IDE, an Arduino Uno board, a USB cable for programming, two buttons, five LEDs, two potentiometers, and a power supply providing either 5 V5\,V or 9 V9\,V.

The basic architecture of an Arduino program, often referred to as a sketch, is divided into two primary functions. The first is setup(), which is executed once when the board is powered on or reset; it is used to configure pin modes (INPUT or OUTPUT) and initialize communications, such as the Serial monitor. The second is loop(), which runs continuously after the setup() function concludes. This block contains the main logic of the project, executing instructions repeatedly in a sequential cycle.

Variable Declaration and Data Types

In the context of Arduino programming, a variable is defined as a reserved space in memory used to store a specific value, such as a number or a text string. A basic declaration follows a syntax where the data type is stated first, followed by the variable name and an assignment. For instance, the statement int led = 13; declares a variable named 'led' of the integer type and assigns it to pin 13. Selecting the correct data type is crucial for optimizing memory usage on the microcontroller.

The most commonly used data types in Arduino include:

  1. int: Used for integers (whole numbers). It typically covers a range from −32,768-32,768 to 32,76732,767. An example of this is int contador = 0;.

  2. float: Used for decimal numbers (floating-point). It covers an approximate range of ±3.4×1038\pm 3.4 \times 10^{38}. An example is float temperatura = 23.5;.

  3. char: Used to store a single character, such as 'A' or 'Z', enclosed in single quotes. An example is char letra = 'A';.

  4. boolean: A logical data type that can only hold one of two values: true or false. An example is boolean ligado = true;.

  5. String: Used for a sequence of characters or text strings. An example is String nome = "Arduino";.

Matrices and Vectors (Arrays)

A matrix or vector, commonly referred to as an array, is a collection of variables of the same data type stored in a contiguous sequence within the memory. This structure allows for efficient management of multiple related values. For example, a practical application in Arduino would be using a vector to store pin numbers for multiple LEDs and then using a loop to turn them on sequentially.

Mathematical, Relational, and Logical Operators

Operators are symbols employed to perform calculations, comparisons, and logical evaluations within the code.

Arithmetic operators include:

  • + (Addition): Returns the sum of two values (e.g., a + b).
  • - (Subtraction): Returns the difference between two values (e.g., a - b).
  • * (Multiplication): Returns the product of two values (e.g., a * b).
  • / (Division): Returns the quotient of two values (e.g., a / b).
  • % (Modulo): Returns the integer remainder of a division (e.g., a % b).

Relational operators compare two values and return a boolean result (true or false):

  • == (Equal to): Returns true if values are identical.
  • != (Different from): Returns true if values are not equal.
  • > (Greater than): Returns true if the left value is larger.
  • < (Less than): Returns true if the right value is larger.
  • >= (Greater than or equal to).
  • <= (Less than or equal to).

Logical operators are used to combine multiple conditions:

  • && (Logical AND): Returns true only if both conditions are true (e.g., a > 0 && b > 0).
  • || (Logical OR): Returns true if at least one of the conditions is true.
  • ! (Logical NOT): Inverts the logical state; for example, !(a > b) returns true if aa is not greater than bb.

Control Structures and Practical Implementation

Programming logic relies on decision-making and repetition structures to control the flow of the code. The if / else structure permits the execution of specific code blocks based on whether a condition is met. A practical example involves an analog sensor connected to an LED. In this scenario, the code reads a value from an analog pin (e.g., A0) and assigns it to a variable, such as valorSensor. If the read value is greater than a specific threshold (e.g., 500500), the LED is turned on using digitalWrite(led, HIGH); otherwise, it is turned off with digitalWrite(led, LOW). During this process, the Serial monitor can be used with Serial.begin(9600) to display the real-time sensor readings, such as "Valor do sensor: 760".

Repetition structures like the for loop are used to repeat a block of code a specific number of times. For example, to make an LED blink exactly five times, one would use: for (int i = 0; i < 5; i++) { ... }. Inside this loop, the LED is set to HIGH, followed by a delay(200) (200 milliseconds), then set to LOW, followed by another delay(200). After the loop finishes five iterations, a longer pause like delay(1000) might be used before the main loop() restarts the process.

Another structure is the while loop, which continues to execute as long as a certain condition remains true. A common application is waiting for a user action. For instance, while (digitalRead(botao) == LOW) { } creates a loop that does nothing (effectively pausing the program) until a button is pressed and the input changes to HIGH. Once the button is pressed, the program exits the while loop and proceeds to the next instruction, such as turning on an LED for one second.

Challenges and Review Tasks

The following section outlines a practical challenge and theoretical questions based on the ITEL 2025_2026 curriculum:

Challenge: Design a system that lights up five LEDs in sequence and immediately turns all of them off if a button is pressed.

Task 1: Define what a variable is and explain its purpose. Task 2: List the primary data types supported by Arduino. Task 3: Explain the fundamental difference between relational operators and logical operators. Task 4: Write a code snippet that activates an LED if an analog reading exceeds the value of 600600. Task 5: Create a vector (array) containing four values and implement a for loop to activate the corresponding LEDs.