MODULE 1:
[Link] to Arduino Programming and Structure of an
Arduino Sketch
Arduino uses a simplified version of C/C++ programming to control hardware.
An Arduino sketch is the name given to the program written in the Arduino IDE.
Structure of an Arduino Sketch
Every Arduino program has two mandatory functions:
(a) setup()
Runs only once when the board starts.
Used to initialize settings such as pin modes, serial communication, sensors etc.
Eg:
void setup() {
pinMode(13, OUTPUT); // set pin 13 as output
(b) loop()
Runs continuously in a loop.
Contains the main logic of the program.
Eg:
void loop() {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);}
2. Data Types in Arduino (int, char, float)
(a) int
Stores integer values (positive or negative).
Size: 2 bytes (on most Arduino boards)
Range: –32,768 to 32,767
Example:
Eg: int count = 10;
(b) char
Stores a single character or small integer.
Size: 1 byte
Range: –128 to 127
Example: char letter = 'A';
(c) float
Stores decimal values.
Size: 4 bytes
Range: ~3.4E–38 to 3.4E+38
Example:float temperature = 24.56;
3. Variables and Variable Declaration in Arduino
A variable is a reserved memory location used to store data during program execution.
Rules for declaring variables
Must start with a letter or underscore
Cannot start with a digit
No spaces allowed
Case sensitive
Examples:
int sensorValue; // declaration
float voltage = 0.0; // declaration + initialization
char grade = 'B';
4. Increment (++) and Decrement (--) Operators in Arduino
These are unary operators that increase or decrease a variable by 1.
Increment (++)
count++; // same as count = count + 1;
Decrement (--)
count--; // same as count = count - 1;
Pre vs Post increment & Decrement
Pre: ++a (increments first, then uses value)
Post: a++ (uses value first, then increments)
Pre: --a (decrements first, then uses value)
Post: a-- (uses value first, then decrements)
5. Relational and Equality Operators in Arduino
Used to compare two values. The result is either true (1) or false (0).
Operator Meaning
> greater than
< less than
>= greater than or equal to
<= less than or equal to
== equal to
!= not equal to
Example:
if (temperature > 30) {
digitalWrite(fan, HIGH);
}
6. Arithmetic and Logical Operators in Arduino
Arithmetic Operators:
Operator Operation
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder)
Examples:
int sum = a + b;
float ratio = x / y;
Logical Operators:
Operator Meaning
&& logical AND
`
! logical NOT
Example:
if (temp > 30 && humidity > 70) {
alarm = true;
}
7. The print() and delay() Functions in Arduino
(A) print() and println()
Used to display data on the Serial Monitor.
Before using print, we must start serial communication inside setup:
[Link](9600);
[Link]()- Prints on the same line.
[Link]("Temperature: ");
[Link](temp);
[Link]()- Prints and moves cursor to the next line.
[Link]("Reading complete");
(B) delay() Function
Pauses program execution for a specified number of milliseconds. Used to create timing control for
LED blinking, sensor reading intervals, etc.
Example:
delay(1000); // waits 1 second