Arduino Programming Language & Syntax

Types of Arduino boards

Introduction to Arduino Programming

Arduino programming is based on C/C++ language. It is used to control LEDs, sensors, motors, displays and IoT devices.

Every Arduino program contains two main functions:

  • setup() â†’ Runs only one time
  • loop() â†’ Runs continuously again and again

setup() and loop()

The setup() function is used for initialization.

The loop() function contains the main program logic.

void setup() {

  pinMode(2, OUTPUT);

}

void loop() {

  digitalWrite(2, HIGH);
  delay(1000);

  digitalWrite(2, LOW);
  delay(1000);

}

Variables in Arduino

Variables are used to store data values.

VariableDescription
intStores integer numbers
floatStores decimal values
charStores single character
StringStores text
boolStores true or false
int ledPin = 2;
float temperature = 25.5;
char grade = 'A';
String name = "ESP32";
bool state = true;

Data Types

Data types define the type of value stored in a variable.

  • int â†’ Whole numbers
  • float â†’ Decimal numbers
  • char â†’ Single characters
  • String â†’ Text data
  • bool â†’ True/False values

Conditional Statements

Conditions are used for decision making.

Common Conditions:

ifelseelse if

int temp = 35;

void setup() {

  Serial.begin(9600);

}

void loop() {

  if(temp > 30){

    Serial.println("Temperature High");

  }
  else{

    Serial.println("Temperature Normal");

  }

  delay(1000);

}

Loops in Arduino

Loops are used to repeat tasks multiple times.

  • for loop
  • while loop
void setup() {

  Serial.begin(9600);

}

void loop() {

  for(int i=1; i<=5; i++){

    Serial.println(i);

  }

  delay(2000);

}

Functions in Arduino

Functions are reusable blocks of code.

They help make programs cleaner and easier to understand.

void blinkLED(){

  digitalWrite(2, HIGH);
  delay(500);

  digitalWrite(2, LOW);
  delay(500);

}

void setup(){

  pinMode(2, OUTPUT);

}

void loop(){

  blinkLED();

}

Serial Monitor

Serial Monitor is used to display output messages and sensor values.

void setup(){

  Serial.begin(9600);

}

void loop(){

  Serial.println("Hello Arduino");

  delay(1000);

}

Open Serial Monitor from:

Tools > Serial Monitor

Important Arduino Functions

FunctionPurpose
pinMode()Set pin as INPUT or OUTPUT
digitalWrite()Write HIGH or LOW
digitalRead()Read digital input
analogRead()Read analog sensor value
delay()Pause program execution
Serial.println()Print output on serial monitor
Scroll to Top