Basic Arduino Commands

📌 Table of Contents

1. Introduction

Arduino is an open-source hardware and programming platform that is easy to learn and widely used in education, research, and robotics projects. Its strength lies in a simple programming language (based on C/C++) and a rich library ecosystem that makes controlling components straightforward.

2. Structure of an Arduino Program

Every Arduino program (called a sketch) contains at least two main functions:


void setup() {
  // Initialization: runs once when the board powers up or resets
}

void loop() {
  // Repeats continuously, contains the main logic
}
        

3. Basic Arduino Commands

a) Managing I/O Pins

pinMode(pin, mode): Declares the function of a pin


pinMode(13, OUTPUT); // Pin 13 set as output (LED control)
pinMode(7, INPUT);   // Pin 7 set as input (button)
pinMode(8, INPUT_PULLUP); // Pin 8 set as input with pull-up resistor
        

-----------------------------------------------------------------

digitalWrite(pin, value): Sends HIGH (5V) or LOW (0V) to a pin.


digitalWrite(13, HIGH); // Turn LED on
digitalWrite(13, LOW);  // Turn LED off
        

-----------------------------------------------------------------

digitalRead(pin): Reads the value from a pin (0 or 1).


int button = digitalRead(7);
if (button == HIGH) {
  // Do something when button is pressed
}
        

-----------------------------------------------------------------

analogRead(pin): Reads an analog value (0-1023) from analog pins (A0-A5).


int value = analogRead(A0); // Read light sensor
        

-----------------------------------------------------------------

analogWrite(pin, value): Outputs a PWM signal (0-255) to simulate analog output.


// → Used for controlling LED brightness or DC motor speed.
analogWrite(9, 128); // 50% duty cycle → medium LED brightness
        

-----------------------------------------------------------------

b) Serial Communication


Serial.begin(9600);
Serial.println("Hello Arduino!");
        

-----------------------------------------------------------------

c) Timing Functions


delay(1000); // wait 1 second