Arduino Programming Basics
1 Arduino Program Structure
An Arduino program, or ”sketch,” consists of two primary functions:
• setup(): Runs once at the beginning and is used to initialize settings,
such as setting pin modes.
• loop(): Runs repeatedly and is where the main code logic goes.
Example
void setup() {
Serial.begin(9600); // Starts serial communication at 9600 baud
}
void loop() {
Serial.println("Hello,␣Arduino!"); // Prints message to Serial
Monitor
delay(1000); // Waits for 1 second
}
2 Variables and Data Types
Variables are containers for storing data values. Common data types include:
• int: Stores whole numbers.
• float: Stores decimal values.
• char: Stores a single character.
• boolean: Stores true or false.
1
Example
int ledPin = 13; // Integer to store the LED pin number
float temperature = 23.5; // Float for decimal values
char initial = ’A’; // Char for single character
boolean isOn = true; // Boolean for true/false
3 Pin Modes
The pinMode() function sets whether a pin will be used as an input or output.
Example
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT); // Sets pin 13 as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turns on the LED
delay(1000); // Waits for 1 second
digitalWrite(ledPin, LOW); // Turns off the LED
delay(1000); // Waits for 1 second
}
4 Digital Input and Output
• digitalWrite(): Sends a high (5V) or low (0V) signal to an output pin.
• digitalRead(): Reads the state of an input pin (either HIGH or LOW).
Example
int buttonPin = 2; // Button connected to pin 2
int ledPin = 13; // LED connected to pin 13
void setup() {
pinMode(buttonPin, INPUT); // Sets button pin as input
pinMode(ledPin, OUTPUT); // Sets LED pin as output
}
void loop() {
int buttonState = digitalRead(buttonPin); // Reads button state
2
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turns on LED if button is pressed
} else {
digitalWrite(ledPin, LOW); // Turns off LED if button is not
pressed
}
}
5 Analog Input and Output
• analogRead(pin): Reads an analog value (0-1023) from a specified pin
(A0 to A5).
• analogWrite(pin, value): Writes an analog-like output using PWM (0
to 255).
Example
int potPin = A0; // Potentiometer connected to pin A0
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
int sensorValue = analogRead(potPin); // Reads value from
potentiometer
Serial.println(sensorValue); // Prints value to Serial Monitor
delay(500); // Waits for half a second
}
6 Control Structures
if / else
Conditional statements for decision-making.
Example
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
3
void loop() {
int sensorValue = analogRead(A0);
if (sensorValue > 512) {
digitalWrite(ledPin, HIGH); // Turns on LED if sensorValue > 512
} else {
digitalWrite(ledPin, LOW); // Turns off LED otherwise
}
}
for loop
Repeats code a specific number of times.
Example
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 5; i++) { // Blinks LED 5 times
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
while loop
Repeats code while a condition is true.
Example
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
while (true) {
digitalWrite(ledPin, HIGH); // Turns on LED forever
4
}
}
7 Serial Communication
• Serial.begin(baud rate): Starts serial communication.
• Serial.print() and Serial.println(): Prints data to the Serial Mon-
itor.
Example
void setup() {
Serial.begin(9600); // Starts serial communication at 9600 baud
}
void loop() {
Serial.println("Hello,␣World!"); // Prints message to Serial Monitor
delay(1000); // Waits 1 second
}
8 Functions
Functions are blocks of code that perform a specific task and can be reused.
Example
void blinkLed(int pin, int delayTime) {
digitalWrite(pin, HIGH);
delay(delayTime);
digitalWrite(pin, LOW);
delay(delayTime);
}
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
blinkLed(ledPin, 500); // Calls blinkLed function with a 500 ms delay
}
5
9 Libraries
Libraries extend the functionality of Arduino, allowing you to control devices
like servos, sensors, etc.
Example
#include <Servo.h> // Includes the Servo library
Servo myServo; // Creates a Servo object
void setup() {
myServo.attach(9); // Attaches servo to pin 9
}
void loop() {
myServo.write(90); // Moves servo to 90 degrees
delay(1000);
myServo.write(0); // Moves servo to 0 degrees
delay(1000);
}
10 Delays and Timing
Using delay()
Pauses the program for a specified time in milliseconds.
Example
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // Turns on LED
delay(1000); // Waits for 1 second
digitalWrite(ledPin, LOW); // Turns off LED
delay(1000); // Waits for 1 second
}
6
Using millis()
Returns the time in milliseconds since the program started. Useful for non-
blocking delays.
Example
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
Serial.println("1␣second␣passed");
previousMillis = currentMillis;
}
}
11 Arrays
Arrays store multiple values under a single variable name.
Example
int ledPins[] = {2, 3, 4, 5}; // Array of LED pins
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT); // Sets each LED pin as output
}
}
void loop() {
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], HIGH); // Turns on each LED in sequence
delay(200);
digitalWrite(ledPins[i], LOW); // Turns off each LED
delay(200);
}
}