GK SOLUTIONS
AI • IoT • Arduino • Projects & Tutorials
DEFEAT THE FEAR
← Back to Tutorials

🤖 Arduino Programming

Master microcontroller programming and embedded systems - from basic circuits to IoT and advanced sensors.

📑 Table of Contents

  1. What is Arduino?
  2. Setup & Installation
  3. Basic Syntax
  4. Digital I/O
  5. Analog I/O
  6. Sensors & Modules
  7. Communication
  8. Libraries
  9. Troubleshooting
  10. Projects

What is Arduino?

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's perfect for beginners to learn electronics and IoT, and powerful enough for advanced projects.

Why Arduino?

  • Easy to Learn: Simple programming language and clear documentation
  • Affordable: Low-cost boards and sensors
  • Open Source: Community-driven with thousands of libraries
  • Versatile: Can control lights, motors, sensors, and more
  • IoT Ready: Connect to WiFi and the internet

Setup & Installation

Hardware Needed:

  • Arduino Board (Uno, Mega, Nano, etc.)
  • USB Cable (to connect to computer)
  • Breadboard and jumper wires
  • LEDs, resistors, sensors (for projects)

Software Installation:

  1. Download Arduino IDE from arduino.cc
  2. Install the IDE for your operating system
  3. Connect your Arduino board via USB
  4. Select board type: Tools → Board → Arduino Uno
  5. Select COM port: Tools → Port → COM3 (or your port)

Basic Syntax

void setup() {
    // Code here runs once when Arduino starts
    Serial.begin(9600);  // Initialize serial communication
    pinMode(13, OUTPUT); // Set pin 13 as output
}

void loop() {
    // Code here runs repeatedly
    digitalWrite(13, HIGH);  // Turn LED on
    delay(1000);            // Wait 1 second
    
    digitalWrite(13, LOW);   // Turn LED off
    delay(1000);            // Wait 1 second
    
    // Print to serial monitor
    Serial.println("LED toggled");
}

Key Functions:

pinMode(pin, mode);        // Set pin as INPUT or OUTPUT
digitalWrite(pin, value);   // Set digital pin HIGH or LOW
digitalRead(pin);           // Read digital pin (HIGH/LOW)
delay(milliseconds);        // Wait for specified time
millis();                   // Get milliseconds since startup

Digital I/O

Controlling an LED:

const int LED_PIN = 13;

void setup() {
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    digitalWrite(LED_PIN, HIGH);  // Turn on
    delay(500);
    digitalWrite(LED_PIN, LOW);   // Turn off
    delay(500);
}

Reading a Button:

const int BUTTON_PIN = 2;
const int LED_PIN = 13;

void setup() {
    pinMode(BUTTON_PIN, INPUT);
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    int button_state = digitalRead(BUTTON_PIN);
    
    if (button_state == HIGH) {
        digitalWrite(LED_PIN, HIGH);
    } else {
        digitalWrite(LED_PIN, LOW);
    }
}

Analog I/O

Reading Analog Values:

const int SENSOR_PIN = A0;  // Analog pin

void setup() {
    Serial.begin(9600);
}

void loop() {
    int sensor_value = analogRead(SENSOR_PIN);  // 0-1023
    float voltage = sensor_value * (5.0 / 1023.0);
    
    Serial.print("Raw value: ");
    Serial.println(sensor_value);
    Serial.print("Voltage: ");
    Serial.println(voltage);
    
    delay(500);
}

PWM (Pulse Width Modulation):

const int LED_PIN = 3;  // Must be PWM pin (3, 5, 6, 9, 10, 11)

void setup() {
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    for (int brightness = 0; brightness <= 255; brightness++) {
        analogWrite(LED_PIN, brightness);
        delay(10);
    }
}

Sensors & Modules

Popular Sensors:

Temperature Sensor (DHT11)

Measures temperature and humidity

Ultrasonic Sensor

Measures distance using sound waves

Motion Sensor (PIR)

Detects movement

Light Sensor (LDR)

Measures light intensity

Communication

Serial Communication:

void setup() {
    Serial.begin(9600);  // 9600 baud rate
}

void loop() {
    // Send data
    Serial.print("Value: ");
    Serial.println(42);    // With newline
    
    // Receive data
    if (Serial.available() > 0) {
        char data = Serial.read();
        Serial.print("Received: ");
        Serial.println(data);
    }
    
    delay(1000);
}

I2C Communication:

#include 

void setup() {
    Wire.begin();  // Join I2C bus
    Serial.begin(9600);
}

void loop() {
    Wire.beginTransmission(0x68);  // Address
    Wire.write(0x00);              // Register
    Wire.endTransmission();
    
    delay(100);
}

Libraries

Installing Libraries:

  1. Open Arduino IDE
  2. Go to Sketch → Include Library → Manage Libraries
  3. Search for desired library
  4. Click Install

Popular Libraries:

Wire              // I2C communication
SoftwareSerial    // Software serial
Servo             // Servo motor control
LiquidCrystal     // LCD display
DHT               // Temperature/humidity sensors
NewPing           // Ultrasonic sensors
RF24              // Wireless communication

Troubleshooting

  • Port not showing: Install drivers for your board, check USB cable
  • Upload fails: Select correct board and COM port, check cable connection
  • No serial output: Check baud rate matches (usually 9600), check Serial Monitor
  • Incorrect readings: Verify wiring, check sensor specifications, add delay
  • Arduino not responding: Reset board (press reset button), reupload code

Projects & Practice

Beginner Projects:

  • Blinking LED
  • Button-controlled LED
  • Traffic Light System
  • Temperature Monitor
  • Simple Alarm System

Intermediate Projects:

  • Smart Home Automation
  • Weather Station
  • Line-following Robot
  • Obstacle Avoidance Robot
  • Plant Watering System

Advanced Projects:

  • IoT Smart House
  • Quadcopter Control
  • Wireless Sensor Network
  • Industrial Automation System
  • Environmental Monitoring System