Coding with Arduino: Basic Coding Techniques on the Arduino Platform

1. Introduction to Arduino

Arduino is an open-source microcontroller platform designed for electronics prototyping, robotics, and automation projects. It provides an easy-to-use environment for coding and hardware integration, making it popular among beginners and professionals alike.

Arduino boards can read inputs from various sensors, process data, and control actuators like LEDs, motors, and displays. The coding is done using the Arduino Integrated Development Environment (IDE), which supports C and C++ programming languages.


2. Getting Started with Arduino Coding

2.1. Setting Up the Arduino IDE

To start coding with Arduino, follow these steps:

  1. Download and Install Arduino IDE from Arduino’s official website.
  2. Connect the Arduino Board to your computer via USB cable.
  3. Select the Correct Board and Port in the Arduino IDE.
  4. Write or Upload Code and run it on the board.

2.2. Basic Structure of an Arduino Sketch

Arduino programs are called sketches and consist of two primary functions:

cppKopyalaDüzenlevoid setup() {
    // Code inside this function runs once when the board is powered on.
}

void loop() {
    // Code inside this function runs continuously.
}
  • setup() → Initializes pins, variables, and libraries.
  • loop() → Runs the main code repeatedly.

3. Basic Coding Techniques in Arduino

3.1. Digital Output (Controlling an LED)

Turning an LED on and off using a digital pin:

cppKopyalaDüzenleint ledPin = 13;  // Define the LED pin

void setup() {
    pinMode(ledPin, OUTPUT);  // Set pin as output
}

void loop() {
    digitalWrite(ledPin, HIGH);  // Turn LED on
    delay(1000);  // Wait for 1 second
    digitalWrite(ledPin, LOW);  // Turn LED off
    delay(1000);  // Wait for 1 second
}
  • pinMode(pin, mode) → Sets the pin as INPUT or OUTPUT.
  • digitalWrite(pin, value) → Sets HIGH (ON) or LOW (OFF).
  • delay(ms) → Pauses execution for the given milliseconds.

3.2. Digital Input (Reading a Button Press)

Reading input from a push button:

cppKopyalaDüzenleint buttonPin = 2;  // Define button pin
int ledPin = 13;    // Define LED pin
int buttonState = 0; 

void setup() {
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
        digitalWrite(ledPin, HIGH);
    } else {
        digitalWrite(ledPin, LOW);
    }
}
  • digitalRead(pin) → Reads the input state (HIGH/LOW) of a digital pin.

3.3. Analog Input (Reading a Sensor Value)

Using a potentiometer (variable resistor) to control an LED:

cppKopyalaDüzenleint sensorPin = A0;  // Analog pin
int ledPin = 9;  

void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);  // Start serial communication
}

void loop() {
    int sensorValue = analogRead(sensorPin);  // Read sensor value
    int brightness = map(sensorValue, 0, 1023, 0, 255);  // Scale value
    analogWrite(ledPin, brightness);  // Adjust LED brightness
    Serial.println(sensorValue);  // Print sensor value to Serial Monitor
    delay(100);
}
  • analogRead(pin) → Reads voltage levels from sensors (0-1023).
  • analogWrite(pin, value) → Sets a PWM (Pulse Width Modulation) value (0-255).
  • Serial.begin(baudRate) → Initializes serial communication.

3.4. Controlling a Servo Motor

Using a servo motor to rotate at different angles:

cppKopyalaDüzenle#include <Servo.h>

Servo myServo;  // Create a servo object

void setup() {
    myServo.attach(9);  // Attach the servo to pin 9
}

void loop() {
    myServo.write(0);   // Move to 0°
    delay(1000);
    myServo.write(90);  // Move to 90°
    delay(1000);
    myServo.write(180); // Move to 180°
    delay(1000);
}
  • Servo.write(angle) → Sets the servo position in degrees (0-180).

4. Conclusion

Arduino provides a user-friendly platform for learning coding and electronics. Understanding digital/analog input and output, sensor integration, and motor control allows users to build interactive projects like robots, automation systems, and IoT devices.

Arduino ile Kodlama: Arduino Platformunda Temel Kodlama Teknikleri

1. Arduino’ya Giriş

Arduino, elektronik prototipleme, robotik ve otomasyon projeleri için geliştirilmiş açık kaynaklı bir mikrodenetleyici platformudur. Kolay kullanım sunan bir ortam sağlayarak hem yeni başlayanlara hem de profesyonellere hitap eder.

Arduino kartları, sensörlerden veri okuyabilir, motorları kontrol edebilir ve çeşitli elektronik bileşenleri yönetebilir. Kodlama, Arduino IDE (Entegre Geliştirme Ortamı) üzerinden C ve C++ dilleri kullanılarak yapılır.


2. Arduino ile Kodlamaya Başlangıç

2.1. Arduino IDE Kurulumu

Arduino ile kodlamaya başlamak için:

  1. Arduino IDE’yi resmi web sitesinden indirin ve kurun.
  2. Arduino kartınızı USB kablo ile bilgisayarınıza bağlayın.
  3. Arduino IDE’den doğru kart ve portu seçin.
  4. Kod yazın ve karta yükleyin.

2.2. Arduino Sketch Yapısı

Arduino programları sketch olarak adlandırılır ve iki ana fonksiyon içerir:

cppKopyalaDüzenlevoid setup() {
    // Bu fonksiyon yalnızca bir kez çalışır.
}

void loop() {
    // Bu fonksiyon sürekli tekrar eder.
}
  • setup() → Başlangıç ayarlarını içerir.
  • loop() → Sürekli çalışan ana kod bloğudur.

3. Arduino’da Temel Kodlama Teknikleri

3.1. Dijital Çıkış (LED Kontrolü)

cppKopyalaDüzenleint ledPin = 13;

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

void loop() {
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
    delay(1000);
}

3.2. Dijital Giriş (Buton Okuma)

cppKopyalaDüzenleint buttonPin = 2;
int ledPin = 13;

void setup() {
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
}

void loop() {
    if (digitalRead(buttonPin) == HIGH) {
        digitalWrite(ledPin, HIGH);
    } else {
        digitalWrite(ledPin, LOW);
    }
}

3.3. Analog Giriş (Sensör Okuma)

cppKopyalaDüzenleint sensorPin = A0;
int ledPin = 9;

void setup() {
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
}

void loop() {
    int sensorValue = analogRead(sensorPin);
    analogWrite(ledPin, map(sensorValue, 0, 1023, 0, 255));
    Serial.println(sensorValue);
    delay(100);
}

4. Sonuç

Arduino, elektronik ve kodlama öğrenmek için güçlü bir platformdur. Dijital/analog giriş-çıkış, sensör entegrasyonu ve motor kontrolü gibi temel konularla projeler geliştirmek mümkündür.