មេរៀនArduino



==========================================================================

មេរៀនទី ១: ផ្ដើមស្គាល់ Arduino

Arduino គឺជា Microcontroller Platform ដែលប្រើសម្រាប់បង្កើត Project អេឡិចត្រូនិក ដូចជា LED, Sensor, Motor និង Automation ផ្សេងៗ។

👉 Arduino ពេញនិយមក្នុងការសិក្សា ព្រោះវាងាយស្រួលប្រើ មាន Community ធំ និងមាន Library ជាច្រើន។

📌 Arduino UNO មានអ្វីខ្លះ?

  • Microcontroller ATmega328P
  • Digital Pins 14
  • Analog Pins 6
  • USB Port សម្រាប់ Upload Code
  • Power Jack

💡 ឧទាហរណ៍កូដដំបូង (Blink LED)


// Arduino Blink LED
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}
    
🔎 កូដនេះធ្វើឲ្យ LED នៅ Pin 13 ភ្លឺ 1 វិនាទី និងរលត់ 1 វិនាទី។
==========================================================================
📘 មេរៀនទី ២៖ ភ្លើង LED ON / OFF (Arduino)
🎯 គោលបំណង
• ស្គាល់របៀបភ្ជាប់ LED ជាមួយ Arduino
• សិក្សាកូដបើក និងបិទ LED
🔌 ឧបករណ៍ត្រូវការ
  • Arduino Uno
  • LED 1 ដុំ
  • Resistor 220Ω
  • Breadboard
  • Jumper wires
💻 កូដ Arduino

// Lesson 2: LED ON / OFF

int ledPin = 13;

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

void loop() {
  digitalWrite(ledPin, HIGH); // LED ON
  delay(1000);
  
  digitalWrite(ledPin, LOW);  // LED OFF
  delay(1000);
}
📝 សេចក្តីពន្យល់
pinMode កំណត់ Pin 13 ជា Output
digitalWrite(HIGH) បើក LED
digitalWrite(LOW) បិទ LED
delay(1000) ពន្យារពេល 1 វិនាទី
==========================================================================
មេរៀនទី ៣: Button Input ដើម្បីគ្រប់គ្រង

ក្នុងមេរៀននេះ អ្នកនឹងសិក្សាអំពីការប្រើប្រាស់ Button Input ដើម្បីបញ្ជា LED ឲ្យបើក និងបិទ ដោយប្រើ Arduino។

ឧបករណ៍ដែលត្រូវការ

  • Arduino UNO
  • Push Button
  • LED + Resistor
  • Breadboard & Jumper Wires

Arduino Code


// Lesson 3: Button Input Control
const int buttonPin = 2;
const int ledPin = 13;

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);
  }
}
      
ចំណាំ: ប៊ូតុងត្រូវភ្ជាប់ជាមួយ Resistor (Pull-down ឬ Pull-up) ដើម្បីជៀសវាងសញ្ញារញ្ជួយ (Floating).
==========================================================================
🚦 មេរៀនទី ៤: Traffic Light Project (LED និង Button)

មេរៀននេះបង្ហាញពីការបង្កើតប្រព័ន្ធភ្លើងចរាចរណ៍ (ក្រហម – លឿង – បៃតង) ដោយប្រើ Arduino, LED និង Button

🎯 គោលបំណង
• យល់ពីការប្រើ LED ច្រើនពណ៌
• សិក្សាពីការប្រើ Button ដើម្បីបញ្ជា
• អនុវត្ត Logic Traffic Light
🧰 ឧបករណ៍ប្រើប្រាស់
  • Arduino Uno
  • LED ក្រហម, លឿង, បៃតង
  • Button 1
  • Resistor 220Ω
  • Breadboard & Jumper wires
🔌 Pin Connection
LED ក្រហម → Pin 8
LED លឿង → Pin 9
LED បៃតង → Pin 10
Button → Pin 2
💻 Arduino Code

const int redLED = 8;
const int yellowLED = 9;
const int greenLED = 10;
const int buttonPin = 2;

void setup() {
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {
    digitalWrite(redLED, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(greenLED, LOW);
    delay(2000);

    digitalWrite(redLED, LOW);
    digitalWrite(yellowLED, HIGH);
    delay(1000);

    digitalWrite(yellowLED, LOW);
    digitalWrite(greenLED, HIGH);
    delay(2000);
  }
}
📌 សេចក្តីសន្និដ្ឋាន
Button នឹងធ្វើឲ្យប្រព័ន្ធ Traffic Light ចាប់ផ្តើមដំណើរការ ដូចភ្លើងចរាចរណ៍ពិតប្រាកដ។
==========================================================================

📘 មេរៀនទី ៥: LED Blinking Pattern និង Button Control (Advanced)

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីការបង្កើត Pattern ភ្លើង LED និងការប្រើ Button ដើម្បីប្ដូរល្បឿន និងរបៀបភ្លើង។

🔧 គោលបំណងមេរៀន

  • បង្កើត LED Blinking ច្រើន Pattern
  • ប្រើ Button ដើម្បីប្ដូរ Mode
  • យល់ដឹងពី Logic Advanced Control

💻 Arduino Code

// Lesson 4: Buzzer Control
const int buzzerPin = 8;

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

void loop() {
  digitalWrite(buzzerPin, HIGH); // Buzzer ON
  delay(500);
  digitalWrite(buzzerPin, LOW);  // Buzzer OFF
  delay(500);
}

📌 ពន្យល់កូដ

  • buzzerPin កំណត់ Pin ដែលភ្ជាប់ Buzzer
  • digitalWrite(HIGH) → Buzzer មានសម្លេង
  • digitalWrite(LOW) → បិទសម្លេង
  • delay(500) → កំណត់ចង្វាក់ Beep

🧪 លំហាត់អនុវត្ត

  • ប្តូរ delay 500ms → 200ms
  • បង្កើត Beep 3 ដង បន្ទាប់មកឈប់ 2 វិនាទី
  • បន្ថែម Button ដើម្បីបើក/បិទ Buzzer

✔ មេរៀននេះជាមូលដ្ឋានសម្រាប់ Alarm System និង Project Radar នៅពេលក្រោយ

==========================================================================

មេរៀនទី៥: ការគ្រប់គ្រង Servo Motor ដោយ Arduino

មេរៀននេះនឹងបង្ហាញពីរបៀបប្រើប្រាស់ Servo Motor ជាមួយ Arduino ដើម្បីបង្វិលមុំ 0°, 90° និង 180°។

គោលបំណង

  • យល់ពី Servo Motor និងការប្រើ Library
  • ភ្ជាប់ Servo Motor ជាមួយ Arduino
  • គ្រប់គ្រងមុំបង្វិលដោយ code

ឧបករណ៍ត្រូវការ

  • Arduino Uno
  • Servo Motor (SG90)
  • Jumper Wires

ការភ្ជាប់ Servo Motor

  • ខ្សែពណ៌ក្រហម → 5V Arduino
  • ខ្សែពណ៌ត្នោត/ខ្មៅ → GND
  • ខ្សែពណ៌លឿង/ទឹកក្រូច → Pin 9
📷 ដាក់ Diagram Servo Motor Connection នៅទីនេះ

Arduino Code

// Lesson 5: Servo Motor Control
#include <Servo.h>

Servo myServo;   // Create servo object

void setup() {
  myServo.attach(9);  // Servo connected to pin 9
}

void loop() {
  myServo.write(0);    // Rotate to 0 degree
  delay(1000);

  myServo.write(90);   // Rotate to 90 degree
  delay(1000);

  myServo.write(180);  // Rotate to 180 degree
  delay(1000);
}

ការពន្យល់កូដសង្ខេប

  • #include <Servo.h> → Library សម្រាប់ Servo
  • attach(9) → ភ្ជាប់ Servo ទៅ Pin 9
  • write(angle) → បញ្ជាមុំ Servo

✔ បន្ទាប់ពី Upload Code Servo Motor នឹងបង្វិល 0° → 90° → 180°

==========================================================================

មេរៀនទី៦: បញ្ជា DC Motor ដោយប្រើ L298N Motor Driver

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីការបញ្ជា DC Motor ដោយប្រើ L298N Motor Driver ជាមួយ Arduino។


ហេតុអ្វី DC Motor មិនអាចភ្ជាប់ទៅ Arduino ដោយផ្ទាល់បាន?

DC Motor មិនអាចភ្ជាប់ទៅ Arduino ដោយផ្ទាល់បានទេ ពីព្រោះ៖

  • DC Motor ប្រើ ចរន្ត (Current) ខ្ពស់ ដែល Arduino មិនអាចផ្គត់ផ្គង់បាន
  • Motor បង្កើត Voltage Spike អាចបំផ្លាញ Arduino
  • Arduino Output Pin ផ្តល់បានត្រឹម 5V / ~40mA ប៉ុណ្ណោះ

👉 ដូច្នេះយើងត្រូវប្រើ L298N Motor Driver ដើម្បីបញ្ជា Motor ដោយមានសុវត្ថិភាព។


ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino Uno
  • L298N Motor Driver
  • DC Motor
  • External Power Supply (6V–12V)
  • Jumper Wires

ការតភ្ជាប់ (Wiring)

  • IN1 → Arduino Pin 8
  • IN2 → Arduino Pin 9
  • ENA → Arduino Pin 10 (PWM)
  • OUT1 / OUT2 → DC Motor
  • 12V → Power Supply +
  • GND → Arduino GND

📌 អ្នកអាចបន្ថែម Diagram រូបភាពនៅទីនេះ


Arduino Code: បញ្ជា DC Motor (Forward / Stop)

// DC Motor Control using L298N

const int IN1 = 8;
const int IN2 = 9;
const int ENA = 10;

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);
}

void loop() {
  // Motor Forward
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  analogWrite(ENA, 200); // Speed Control
  delay(3000);

  // Motor Stop
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  delay(2000);
}

សរុប

  • DC Motor ត្រូវការចរន្តខ្ពស់ → មិនអាចភ្ជាប់ Arduino ដោយផ្ទាល់
  • L298N ជួយបញ្ជា Direction និង Speed
  • ENA (PWM) ប្រើសម្រាប់កំណត់ល្បឿន Motor

✅ មេរៀនទី៦ ត្រូវបានបញ្ចប់ដោយជោគជ័យ

==========================================================================

មេរៀនទី៧៖ Ultrasonic Sensor HC-SR04

មេរៀននេះនឹងបង្ហាញពីរបៀបប្រើប្រាស់ Ultrasonic Sensor HC-SR04 ដើម្បីវាស់ចម្ងាយ ដោយប្រើ Arduino និងបង្ហាញលទ្ធផលតាម Serial Monitor។


🎯 គោលបំណង

  • យល់ពីគោលការណ៍ដំណើរការរបស់ Ultrasonic Sensor
  • ភ្ជាប់ HC-SR04 ជាមួយ Arduino
  • វាស់ចម្ងាយ (cm)

🧰 ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino Uno
  • Ultrasonic Sensor HC-SR04
  • Jumper Wires

🔌 ការភ្ជាប់ (Wiring)

HC-SR04Arduino
VCC5V
GNDGND
TrigPin 9
EchoPin 10

📐 Diagram

👉 ដាក់រូប Diagram HC-SR04 នៅទីនេះ

💻 Arduino Code

// Ultrasonic Sensor HC-SR04
const int trigPin = 9;
const int echoPin = 10;

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);
}

🧠 សេចក្តីពន្យល់កូដសង្ខេប

  • trigPin បញ្ជូនសញ្ញាសំឡេង Ultrasonic
  • echoPin ទទួលសញ្ញាត្រឡប់
  • pulseIn() វាស់ពេលវេលាសញ្ញាត្រឡប់
  • រូបមន្តចម្ងាយ = (duration × 0.034) ÷ 2

✔ មេរៀននេះជាមូលដ្ឋានសម្រាប់ Radar System, Parking Sensor និង Smart Robot

==========================================================================

📘 មេរៀនទី៨: LCD 1602 I2C Display

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីការប្រើប្រាស់ LCD 1602 I2C ជាមួយ Arduino ដើម្បីបង្ហាញអក្សរ និងលេខ ដោយប្រើខ្សែត្រឹមតែ 2 (SDA, SCL)។


🎯 គោលបំណង

  • ស្គាល់ LCD 1602 I2C Module
  • ភ្ជាប់ LCD ជាមួយ Arduino
  • បង្ហាញអក្សរ និងលេខ

🧰 ឧបករណ៍ប្រើប្រាស់

  • Arduino Uno
  • LCD 1602 I2C
  • Jumper Wires

🔌 Wiring (ការភ្ជាប់)

LCD I2C Arduino Uno
VCC 5V
GND GND
SDA A4
SCL A5

💻 Arduino Code (LCD 1602 I2C)

// LCD 1602 I2C Example
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Hello Arduino");
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print("Time: ");
  lcd.print(millis() / 1000);
  delay(500);
}

📝 សេចក្ដីពន្យល់ខ្លី

  • 0x27 គឺជា I2C Address (អាចខុសលើ module ខ្លះ)
  • lcd.init() ចាប់ផ្តើម LCD
  • lcd.backlight() បើកភ្លើង LCD
  • setCursor(col, row) កំណត់ទីតាំងអក្សរ

✅ មេរៀននេះជាមូលដ្ឋានសំខាន់សម្រាប់ Project ដែលត្រូវការបង្ហាញទិន្នន័យ

==========================================================================

មេរៀនទី៩: DHT11 Temperature & Humidity Sensor

មេរៀននេះនឹងបង្ហាញពីការប្រើប្រាស់ DHT11 Sensor ដើម្បីវាស់សីតុណ្ហភាព (Temperature) និងសំណើមខ្យល់ (Humidity) ដោយប្រើ Arduino និងបង្ហាញលទ្ធផលតាម Serial Monitor។


🎯 គោលបំណងមេរៀន

  • ស្គាល់ DHT11 Temperature & Humidity Sensor
  • ភ្ជាប់ DHT11 ជាមួយ Arduino
  • អាន Temperature និង Humidity

🧰 ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino Uno
  • DHT11 Sensor (3 pin ឬ 4 pin)
  • Jumper Wires

🔌 ការភ្ជាប់ DHT11

  • VCC → 5V (Arduino)
  • GND → GND
  • DATA → Digital Pin 2
👉 ដាក់ Diagram / Image ការភ្ជាប់ DHT11 នៅទីនេះ

💻 Arduino Code (DHT11)

// DHT11 Temperature & Humidity Sensor
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("DHT11 Sensor Started");
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  delay(2000);
}

📊 លទ្ធផល (Output)

  • Humidity (%) បង្ហាញក្នុង Serial Monitor
  • Temperature (°C) បង្ហាញរៀងរាល់ 2 វិនាទី

📌 ចំណាំសំខាន់

  • ត្រូវ Install DHT Sensor Library ក្នុង Arduino IDE
  • DHT11 អានទិន្នន័យបានរៀងរាល់ ~2 វិនាទី
  • អាចប្ដូរទៅ DHT22 ដោយផ្លាស់ប្តូរ DHTTYPE

✅ បញ្ចប់មេរៀនទី៩: DHT11 Temperature & Humidity Sensor

==========================================================================

មេរៀនទី១០ — គម្រោងរួម: Security System Arduino + Sensor + Alarm

ក្នុងមេរៀននេះ អ្នកនឹងបង្កើតប្រព័ន្ធសុវត្ថិភាព (Security System) ដោយប្រើ Arduino រួមជាមួយ PIR Motion Sensor, LED និង Buzzer (Alarm)។


🎯 គោលបំណង

  • យល់ពីការប្រើ PIR Motion Sensor
  • បង្កើត Alarm (Buzzer)
  • រួមបញ្ចូល Sensor + LED + Alarm ជា Project ពេញលេញ

🧰 ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino Uno
  • PIR Motion Sensor
  • LED + Resistor 220Ω
  • Buzzer
  • Jumper Wires

🔌 Wiring Diagram

👉 PIR OUT → Pin 2
👉 LED → Pin 8 (via resistor)
👉 Buzzer → Pin 9
👉 VCC → 5V, GND → GND

🧠 Logic ការដំណើរការ

  1. PIR Sensor រកឃើញចលនា
  2. LED បើក
  3. Buzzer បន្លឺសំឡេង Alarm
  4. គ្មានចលនា → LED និង Buzzer បិទ

💻 Arduino Code

// Security System with PIR Sensor, LED and Buzzer

const int pirPin = 2;
const int ledPin = 8;
const int buzzerPin = 9;

int motionState = 0;

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

void loop() {
  motionState = digitalRead(pirPin);

  if (motionState == HIGH) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    Serial.println("⚠ Motion Detected! Alarm ON");
    delay(500);
  } 
  else {
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
    Serial.println("No Motion");
    delay(500);
  }
}

🧪 លទ្ធផលដែលទទួលបាន

  • មានចលនា → LED ភ្លឺ + Buzzer បន្លឺ
  • គ្មានចលនា → System ស្ងៀម
  • អាចពង្រីកបន្ថែមទៅ SMS / IoT / LCD

✅ អ្នកបានបញ្ចប់ Arduino Security System Project ដោយជោគជ័យ!

==========================================================================

មេរៀនទី១០ — គម្រោងរួម: Security System Arduino + Sensor + Alarm

គម្រោងនេះបង្ហាញពីការបង្កើតប្រព័ន្ធសុវត្ថិភាព (Security System) ដោយប្រើ PIR Motion Sensor សម្រាប់ចាប់ចលនា, LED សម្រាប់សញ្ញាព្រមាន និង Buzzer សម្រាប់សំឡេង Alarm។


🎯 គោលបំណង

  • យល់ពីការប្រើ PIR Motion Sensor
  • បង្កើត Alarm ដោយ Buzzer
  • រួមបញ្ចូល Sensor + Output Devices

🧰 ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino Uno
  • PIR Motion Sensor
  • Buzzer
  • LED + Resistor 220Ω
  • Jumper Wires

🔌 ការភ្ជាប់ឧបករណ៍

  • PIR OUT → Pin 2
  • LED → Pin 8
  • Buzzer → Pin 9
  • VCC → 5V, GND → GND
📷 ដាក់ Diagram ឬ Wiring Image នៅទីនេះ

💻 Arduino Code (Security System)

// Arduino Security System

const int pirPin = 2;
const int ledPin = 8;
const int buzzerPin = 9;

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

void loop() {
  int motion = digitalRead(pirPin);

  if(motion == HIGH) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
    Serial.println("⚠ Motion Detected!");
    delay(1000);
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
    Serial.println("No Motion");
    delay(500);
  }
}

📌 លទ្ធផល

  • ពេលមានចលនា → LED ភ្លឺ + Buzzer រោទ៍
  • ពេលគ្មានចលនា → System នៅស្ងៀម

🚀 អភិវឌ្ឍបន្ថែម

  • បន្ថែម LCD Display
  • បន្ថែម GSM / SMS Alert
  • បន្ថែម Keypad ដើម្បី Arm/Disarm

🎉 អ្នកបានបញ្ចប់គម្រោង Security System Arduino ដោយជោគជ័យ!

==========================================================================

មេរៀនទី ១១: បញ្ជា Relay Module (គ្រប់គ្រងភ្លើង/ម៉ូទ័រ 220V) ជាមួយ Arduino UNO

មេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **Relay Module** ដើម្បីគ្រប់គ្រង **Load AC 220V** ឬម៉ូទ័រ ដោយប្រើ **Arduino UNO**។ ចំណាំ៖ ប្រុងប្រយ័ត្នខ្ពស់ ពីអគ្គិសនី AC 220V។

Components

  • Arduino UNO
  • Relay Module 1 Channel (5V)
  • LED (Optional for testing)
  • Resistor 220Ω
  • AC Load (Light / Fan)
  • Button (Optional)

Circuit Diagram

👉 Insert Diagram / Image of Relay Connection Here

Arduino Code

// Relay Module Control Example
const int relayPin = 8;
const int buttonPin = 2;
int buttonState = 0;

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up
  digitalWrite(relayPin, LOW); // Relay OFF initially
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if(buttonState == LOW) { // Button pressed
    digitalWrite(relayPin, HIGH); // Turn ON load
  } else {
    digitalWrite(relayPin, LOW);  // Turn OFF load
  }
}

Safety Notes

  • ប្រុងប្រយ័ត្នខ្ពស់ ពីអគ្គិសនី AC 220V
  • ប្រើ **Relay Module** ដែលមាន optocoupler សម្រាប់ isolation
  • កុំប៉ះអគ្គិសនី AC នៅពេល Arduino connected

Steps

  1. ភ្ជាប់ Relay IN Pin → Arduino Pin 8
  2. VCC → 5V, GND → GND
  3. ភ្ជាប់ AC Load តាម Normally Open (NO) និង Common (COM) នៃ Relay
  4. Upload Arduino code
  5. ចុច Button ដើម្បី switch Load ON/OFF

Summary

មេរៀននេះបង្ហាញពីការគ្រប់គ្រង **Load AC / Motor** ដោយប្រើ **Arduino + Relay Module**។ សូមប្រុងប្រយ័ត្នខ្ពស់ពេលប្រើប្រាស់អគ្គិសនី AC និងត្រូវមានអ្នកពិសេស supervise ប្រសិនបើអ្នកជាអ្នកចាប់ផ្តើម។

==========================================================================

មេរៀនទី ១២: Sensor សំណើមដី (Soil Moisture) + Relay Pump

មេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **Soil Moisture Sensor** ដើម្បី detect សំណើមដី និងគ្រប់គ្រង **Relay + Water Pump** ដោយ Arduino។

Components

  • Arduino Uno
  • Soil Moisture Sensor
  • Relay Module
  • Water Pump
  • Resistors, Jumper Wires

Wiring Diagram

👉 បង្ហាញរូប diagram នៅទីនេះ

Arduino Code

// Soil Moisture Sensor + Relay Pump
const int sensorPin = A0;
const int relayPin = 8;
int sensorValue = 0;
void setup() {
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Pump OFF initially
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.print("Soil Moisture: ");
  Serial.println(sensorValue);

  if(sensorValue < 400) { // Dry soil threshold
    digitalWrite(relayPin, HIGH); // Turn ON Pump
  } else {
    digitalWrite(relayPin, LOW); // Turn OFF Pump
  }
  delay(1000);
}

Explanation

  • sensorPin: Soil Moisture analog input
  • relayPin: Digital output to control pump
  • Sensor value < threshold → Pump ON, otherwise OFF
  • Serial Monitor to check soil moisture value

Tips

  • អាចផ្លាស់ប្តូរ threshold ឲ្យត្រូវនឹងប្រភេទដី
  • ប្រើ relay 5V និង external power source សម្រាប់ pump ទៅកាន់សុវត្ថិភាព
==========================================================================

មេរៀនទី ១៣: IR Remote Control (បញ្ជាទូរទស្សន៍) ជាមួយ Arduino Uno

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **IR Remote** ដើម្បីគ្រប់គ្រង LED ឬ device ផ្សេងៗ ជាមួយ Arduino Uno។ អ្នកនឹងទទួលបានបទពិសោធន៍ក្នុងការទាញ **IR code** និងប្រើ **IR Library**។

Components

  • Arduino Uno
  • IR Receiver Module (TSOP38238)
  • IR Remote
  • LED + 220Ω Resistor
  • Jumper wires & Breadboard

Hardware Connection

  • IR Receiver OUT → Arduino Pin 11
  • IR Receiver VCC → 5V
  • IR Receiver GND → GND
  • LED → Pin 8 + Resistor 220Ω → GND

Arduino Code

// IR Remote Control Example
#include <IRremote.h>

const int recvPin = 11;
const int ledPin = 8;

IRrecv irrecv(recvPin);
decode_results results;

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

void loop() {
  if(irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    switch(results.value) {
      case 0xFFA25D: // Example button code
        digitalWrite(ledPin, HIGH);
        break;
      case 0xFFE21D: // Example button code
        digitalWrite(ledPin, LOW);
        break;
    }
    irrecv.resume();
  }
}

Steps to Test

  1. Connect Arduino and IR receiver as per hardware connection.
  2. Upload the code to Arduino.
  3. Open Serial Monitor (9600 baud) to check IR codes when pressing remote buttons.
  4. Press the remote button configured in switch case to turn LED ON/OFF.

Tips

  • ប្រើ Serial Monitor ដើម្បីទាញ IR code របស់ button មុនប្រើក្នុង switch case។
  • អាចគ្រប់គ្រង device ផ្សេងៗ ដូចជា fan, motor, relay ដោយបម្លែង LED logic ទៅ device control។
==========================================================================

មេរៀនទី១៤: Bluetooth HC-05 Control (Android App)

មេរៀននេះ អ្នកនឹងរៀនពីរបៀប **គ្រប់គ្រង LED ឬ Device** តាម **Bluetooth HC-05** និង **Android App**។ អ្នកអាចប្រើ App ដូចជា Arduino Bluetooth Controller ដើម្បីបញ្ជា Arduino តាម Bluetooth។

Components

  • Arduino Uno
  • Bluetooth HC-05 Module
  • LED + Resistor 220Ω
  • Android Phone with Bluetooth App

Wiring

  • HC-05 TX → Arduino RX (Pin 0)
  • HC-05 RX → Arduino TX (Pin 1) (use voltage divider 5V→3.3V)
  • HC-05 VCC → 5V, GND → GND
  • LED → Pin 8 + GND (resistor)

Arduino Code Example

// Bluetooth HC-05 Control LED
char btData;
const int ledPin = 8;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); // HC-05 default baud rate
}

void loop() {
  if (Serial.available()) {
    btData = Serial.read();

    if(btData == '1') {
      digitalWrite(ledPin, HIGH); // Turn LED ON
    } else if(btData == '0') {
      digitalWrite(ledPin, LOW);  // Turn LED OFF
    }
  }
}

Usage

  1. Upload code to Arduino Uno.
  2. Pair Android phone with HC-05 (default password: 1234 or 0000).
  3. Open Bluetooth Controller App, connect to HC-05.
  4. Send '1' to turn LED ON, '0' to turn LED OFF.

Tips

  • ប្រើ resistor នៅ LED ដើម្បីការពារ LED
  • ប្រសិនបើ HC-05 RX ការទទួលអាចមិនបានស្ថិតស្ថេរ សាកល្បង voltage divider (2 resistors)
  • អាចបង្កើត Button ជាច្រើននៅ App ដើម្បីគ្រប់ Device ពី Arduino
==========================================================================

មេរៀនទី១៥: PIR Motion Sensor (ប្រព័ន្ធចាប់ចលនា)

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **PIR Motion Sensor** ដើម្បីចាប់ចលនាក្នុងបរិវេណ និងបញ្ជា LED ឬ Buzzer ដើម្បីឲ្យមានសញ្ញា។

គោលបំណង

  • រៀនប្រើ PIR Sensor ជា Digital Input
  • គ្រប់គ្រង LED ឬ Buzzer ដោយចលនា
  • ទទួលបានបទពិសោធន៍ក្នុងការបង្កើត Motion Alarm System

Components / Hardware

  • Arduino Uno
  • PIR Motion Sensor
  • LED និង Resistor 220Ω
  • Buzzer (Optional)
  • Jumper Wires
  • Breadboard

Wiring Diagram

👉 បង្ហាញ diagram របស់ PIR Sensor + LED + Buzzer នៅទីនេះ

Arduino Code Example

// PIR Motion Sensor with LED
const int pirPin = 2;
const int ledPin = 8;
const int buzzerPin = 9;

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

void loop() {
  int motion = digitalRead(pirPin);
  Serial.println(motion);
  
  if(motion == HIGH) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(buzzerPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzerPin, LOW);
  }
  delay(200);
}
==========================================================================

មេរៀនទី១៦: Flame Sensor (ចាប់អគ្គិភ័យ) សម្រាប់ Arduino Uno

មេរៀននេះបង្ហាញពីរបៀបប្រើ Flame Sensor ដើម្បីចាប់អគ្គិភ័យ និងបើក LED / Buzzer។

Hardware Connection

  • Flame Sensor OUT → Digital Pin 2
  • LED → Pin 8 (ជាច្រៀងប្រើ resistor 220Ω)
  • Buzzer → Pin 9
  • VCC & GND → 5V / GND

Arduino Code

// Flame Sensor Example
const int flamePin = 2;
const int ledPin = 8;
const int buzzer = 9;

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

void loop() {
  int flameState = digitalRead(flamePin);
  Serial.println(flameState);
  
  if(flameState == HIGH) {
    digitalWrite(ledPin, HIGH);
    digitalWrite(buzzer, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
    digitalWrite(buzzer, LOW);
  }
  delay(200);
}

==========================================================================

មេរៀនទី១៧: Potentiometer Control

មេរៀននេះបង្ហាញពីរបៀបប្រើ Potentiometer ដើម្បីគ្រប់គ្រង LED brightness ឬ Servo position។

Hardware Connection

  • Potentiometer → Middle pin → A0, Side pins → 5V & GND
  • LED → Pin 9 (PWM output) ជាមួយ resistor 220Ω

Arduino Code

// Potentiometer Example
const int potPin = A0;
const int ledPin = 9;

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

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

មេរៀនទី១៨: Arduino + OLED Display (I2C)

មេរៀននេះនឹងបង្ហាញពីរបៀបបង្កើតការបង្ហាញអក្សរ និងលេខលើ **OLED Display (I2C)** ជាមួយ Arduino Uno

Components

  • Arduino Uno
  • OLED Display 0.96" I2C (128x64)
  • Jumper Wires

Wiring

  • OLED VCC → 5V
  • OLED GND → GND
  • OLED SCL → A5
  • OLED SDA → A4

Arduino Code

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Setup OLED
void setup() {
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("Hello Arduino!");
  display.display();
}

void loop() {
  display.clearDisplay();
  display.setCursor(0,0);
  display.println("Counter:");
  display.println(millis()/1000);
  display.display();
  delay(1000);
}

==========================================================================

មេរៀនទី១៩: Line Tracking Sensor

មេរៀននេះសម្រាប់ **Line Tracking Sensor** ដើម្បី detect path និងប្រើសម្រាប់ robot line follower

Components

  • Arduino Uno
  • Line Tracking Sensor Module
  • Jumper Wires

Wiring

  • VCC → 5V
  • GND → GND
  • OUT → Digital Pin 2

Arduino Code

// Line Tracking Sensor Example
const int sensorPin = 2;
const int ledPin = 13;

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

void loop() {
  int sensorValue = digitalRead(sensorPin);
  if(sensorValue == HIGH){
    digitalWrite(ledPin, HIGH);
    Serial.println("Line detected");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("No line");
  }
  delay(100);
} 
==========================================================================-

មេរៀនទី ២០: Project — Smart Car (Full Code)

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីការបង្កើត **Smart Car** ដែលគ្រប់គ្រងបានដោយ Arduino និង Motor Driver (L298N)។ Car អាចចេញចូល និងបត់បាន។

Components

  • Arduino Uno / Nano
  • Motor Driver L298N
  • DC Motors + Wheels
  • Chassis
  • Battery Pack
  • Ultrasonic Sensor (Optional)

Full Code

// Smart Car Full Code
const int enA = 5;
const int enB = 6;
const int in1 = 7;
const int in2 = 8;
const int in3 = 9;
const int in4 = 10;

void setup() {
  pinMode(enA, OUTPUT);
  pinMode(enB, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
}

void loop() {
  // Forward
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
  analogWrite(enA,200);
  analogWrite(enB,200);
  delay(2000);

  // Backward
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
  delay(2000);

  // Turn Left
  digitalWrite(in1,LOW);
  digitalWrite(in2,HIGH);
  digitalWrite(in3,HIGH);
  digitalWrite(in4,LOW);
  delay(1000);

  // Turn Right
  digitalWrite(in1,HIGH);
  digitalWrite(in2,LOW);
  digitalWrite(in3,LOW);
  digitalWrite(in4,HIGH);
  delay(1000);
}
==========================================================================

មេរៀនទី២១: RFID Card System (Door Access Control)

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **RFID Module (RC522)** ដើម្បីគ្រប់គ្រងការចូល/ចេញតាម Card Access និងបើក LED ឬ Servo ដើម្បី simulate Door Lock.

Components Required

  • Arduino Uno
  • RFID RC522 Module
  • LED / Buzzer
  • Servo Motor (optional for door lock)
  • Jumper wires
  • Breadboard

Hardware Connection

  • RFID SDA → Pin 10
  • RFID SCK → Pin 13
  • RFID MOSI → Pin 11
  • RFID MISO → Pin 12
  • RFID RST → Pin 9
  • LED → Pin 8 + Resistor 220Ω
  • Servo → Pin 6 (optional)

Arduino Code Example

// RFID Door Access Control
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>

#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);
Servo doorServo;

const int ledPin = 8;

void setup() {
  Serial.begin(9600);
  SPI.begin();
  mfrc522.PCD_Init();
  pinMode(ledPin, OUTPUT);
  doorServo.attach(6);
  doorServo.write(0); // Lock position
}

void loop() {
  if( ! mfrc522.PICC_IsNewCardPresent()) return;
  if( ! mfrc522.PICC_ReadCardSerial()) return;

  Serial.print("Card UID: ");
  for(byte i = 0; i < mfrc522.uid.size; i++) {
    Serial.print(mfrc522.uid.uidByte[i] << " ");
  }
  Serial.println();

  if(mfrc522.uid.uidByte[0]==0x12 && mfrc522.uid.uidByte[1]==0x34 && mfrc522.uid.uidByte[2]==0x56 && mfrc522.uid.uidByte[3]==0x78){
    digitalWrite(ledPin, HIGH);
    doorServo.write(90); // Unlock
    delay(3000);
    doorServo.write(0);  // Lock back
    digitalWrite(ledPin, LOW);
  } else {
    Serial.println("Access Denied");
    digitalWrite(ledPin, LOW);
  }

  mfrc522.PICC_HaltA();
}
==========================================================================

មេរៀនទី ២២: Arduino + ESP8266 WiFi (IoT Control)

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបភ្ជាប់ Arduino ជាមួយ ESP8266 ដើម្បីបញ្ជាឧបករណ៍តាម WiFi (IoT Control)។

គោលបំណង

  • Connect Arduino to ESP8266 WiFi Module
  • Control LED or Device via Internet
  • Send / Receive Data via HTTP

Components

  • Arduino UNO / Nano
  • ESP8266 WiFi Module (ESP-01)
  • LED + 220Ω Resistor
  • Jumper Wires
  • USB Power / Arduino

Arduino + ESP8266 Code

// Arduino + ESP8266 WiFi Control
#include <SoftwareSerial.h>

SoftwareSerial esp(2,3); // RX, TX
const int ledPin = 8;
String command;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Serial.begin(9600);
  esp.begin(115200);
}

void loop() {
  if(esp.available()) {
    command = esp.readString();
    command.trim();
    if(command == "ON") digitalWrite(ledPin,HIGH);
    else if(command == "OFF") digitalWrite(ledPin,LOW);
  }

  if(Serial.available()) {
    String cmd = Serial.readString();
    esp.println(cmd);
  }
}

Connection Diagram

  • ESP8266 TX → Arduino Pin 2 (RX)
  • ESP8266 RX → Arduino Pin 3 (TX) via voltage divider 3.3V
  • ESP8266 VCC → 3.3V
  • ESP8266 GND → GND
  • LED → Arduino Pin 8 + 220Ω resistor

ការពិពណ៌នា

បន្ទាប់ពីភ្ជាប់ Arduino និង ESP8266 អ្នកអាចប្រើ Serial Monitor ឬ Web Server ដើម្បីបញ្ជា LED ON/OFF តាម WiFi។ នេះជាចំណុចចាប់ផ្តើមសម្រាប់ IoT Project ដោយប្រើ Arduino និង ESP8266។

==========================================================================

មេរៀនទី ២៣: GPS Module (Neo-6M)

មេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **GPS Module (Neo-6M)** ជាមួយ Arduino ដើម្បីទាញ **Latitude, Longitude និង Time**។

Components

  • Arduino Uno / Nano
  • GPS Module Neo-6M
  • Jumper Wires

Wiring

  • GPS VCC → 5V
  • GPS GND → GND
  • GPS TX → Arduino Pin 4 (SoftwareSerial RX)
  • GPS RX → Arduino Pin 3 (SoftwareSerial TX)

Arduino Code

// GPS Neo-6M Example
#include <SoftwareSerial.h>
#include <TinyGPS++.h>

SoftwareSerial gpsSerial(3, 4); // RX, TX
TinyGPSPlus gps;

void setup() {
  Serial.begin(9600);
  gpsSerial.begin(9600);
  Serial.println("GPS Module Test");
}

void loop() {
  while(gpsSerial.available() > 0) {
    gps.encode(gpsSerial.read());
    if(gps.location.isUpdated()) {
      Serial.print("Latitude: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("Longitude: ");
      Serial.println(gps.location.lng(), 6);
      Serial.print("Altitude: ");
      Serial.println(gps.altitude.meters());
      Serial.print("Speed: ");
      Serial.println(gps.speed.kmph());
      Serial.print("Date: ");
      Serial.print(gps.date.day()); Serial.print("/"); Serial.print(gps.date.month()); Serial.print("/"); Serial.println(gps.date.year());
      Serial.print("Time: ");
      Serial.print(gps.time.hour()); Serial.print(":"); Serial.print(gps.time.minute()); Serial.print(":"); Serial.println(gps.time.second());
      Serial.println("---------------------");
    }
  }
}
==========================================================================

មេរៀនទី២៤: Voice Recognition Module V3

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **Voice Recognition Module V3** ជាមួយ Arduino ដើម្បីគ្រប់គ្រង LED ឬ actuators ផ្សេងៗ តាមពាក្យសំឡេងដែលបានផ្ទុក។

Components

  • Arduino UNO
  • Voice Recognition Module V3
  • LED + Resistor 220Ω
  • Jumper wires

Wiring

  • VCC → 5V Arduino
  • GND → GND Arduino
  • RX → TX Arduino
  • TX → RX Arduino
  • LED → Pin 8 + Resistor

Arduino Code Example

// Voice Recognition Module V3 Example
#include <SoftwareSerial.h>
#include <VoiceRecognitionV3.h>

SoftwareSerial mySerial(2, 3); // RX, TX
VR myVR(mySerial);

uint8_t records[7]; // Save recognized commands
uint8_t buf[64];

const int ledPin = 8;

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  pinMode(ledPin, OUTPUT);
  
  if(myVR.begin()) {
    Serial.println("VR Module ready.");
  } else {
    Serial.println("VR Module not detected.");
    while(1);
  }
}

void loop() {
  int ret = myVR.recognize(buf, 50);
  if(ret >= 0) {
    Serial.print("Command ID: ");
    Serial.println(buf[1]);
    
    switch(buf[1]) {
      case 0: digitalWrite(ledPin, HIGH); break;
      case 1: digitalWrite(ledPin, LOW); break;
      default: break;
    }
  }
}

Step by Step Instructions

  1. Connect components according to Wiring section.
  2. Upload the Arduino code.
  3. Open Serial Monitor at 9600 baud.
  4. Speak the trained commands to control the LED.

Tips

  • Train commands using Voice Recognition Module software.
  • Use short, clear words for best recognition.
  • Ensure RX/TX connections match Arduino pins.
==========================================================================

មេរៀនទី ២៥: Camera OV7670 (Basic Capture)

មេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ **Camera OV7670** ដើម្បី capture image ជាមួយ Arduino. យើងនឹងសាកល្បង basic setup និង capture frame ដំបូង។

Components Needed

  • Arduino Uno / Mega
  • Camera OV7670 module (without FIFO)
  • Jumper wires
  • Breadboard

Wiring

  • VCC → 3.3V
  • GND → GND
  • SDA → A4 (Arduino Uno)
  • SCL → A5 (Arduino Uno)
  • XCLK, PCLK, HREF, VSYNC → Digital pins as per library

Arduino Code (Basic Capture)

// OV7670 Basic Capture Example
#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Serial.println("OV7670 Camera Init");
  // TODO: Initialize OV7670 registers
}

void loop() {
  // TODO: Capture basic frame
  Serial.println("Capture Frame...");
  delay(1000);
}

Next Steps

  • Integrate OV7670 with TFT / LCD display
  • Store captured images to SD card
  • Experiment with resolution & timing
==========================================================================

មេរៀនទី ២៦: Real-Time Clock (RTC DS3231)

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបប្រើ RTC DS3231 ជាមួយ Arduino ដើម្បីបង្ហាញថ្ងៃ ខែ ឆ្នាំ និងម៉ោងពេញលេញនៅលើ Serial Monitor ឬ LCD។

Components

  • Arduino Uno / Nano
  • RTC DS3231 Module
  • Jumper wires
  • Optional: LCD 16x2

Wiring Diagram

  • DS3231 VCC → 5V
  • DS3231 GND → GND
  • DS3231 SDA → A4 (Arduino Uno)
  • DS3231 SCL → A5 (Arduino Uno)

Arduino Code

// RTC DS3231 Example
#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc;

void setup() {
  Serial.begin(9600);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, setting time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  delay(1000);
}

Explanation

  • តភ្ជាប់ RTC DS3231 ដោយប្រើ I2C (SDA/SCL)
  • សរសេរកូដប្រើ RTClib library
  • អាចបង្ហាញនៅលើ Serial Monitor ឬ LCD 16x2
  • ដំណើរការប្រក្រតីមិនបាត់ power memory

Optional: Display on LCD

អ្នកអាចប្រើ library LiquidCrystal_I2C ដើម្បីបង្ហាញ Date/Time នៅលើ LCD 16x2 ដូចជា៖

#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>

RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.init();
  lcd.backlight();
  if(!rtc.begin()) { while(1); }
}

void loop() {
  DateTime now = rtc.now();
  lcd.setCursor(0,0);
  lcd.print(now.hour(),DEC); lcd.print(':'); lcd.print(now.minute(),DEC);
  lcd.setCursor(0,1);
  lcd.print(now.year(),DEC); lcd.print('/'); lcd.print(now.month(),DEC); lcd.print('/'); lcd.print(now.day(),DEC);
  delay(1000);
}
==========================================================================

មេរៀនទី២៧: Battery Monitoring (Voltage Divider)

មេរៀននេះបង្ហាញពីរបៀប វាស់វ៉ុលថ្ម (Battery Voltage) ដោយប្រើ Voltage Divider និង Arduino UNO ដើម្បីការពារមិនឲ្យវ៉ុលខ្ពស់ ប៉ះពាល់ដល់ Analog Pin។

🔌 ការភ្ជាប់ Voltage Divider

  • R1 = 10kΩ (ភ្ជាប់ពី +Battery ទៅ A0)
  • R2 = 10kΩ (ភ្ជាប់ពី A0 ទៅ GND)
  • A0 → Analog Read Voltage
// YouTube Channel -> Khmer PC Knowledge
// Lesson 27: Battery Monitoring (Voltage Divider)

const int batteryPin = A0;
float R1 = 10000.0; // 10k ohm
float R2 = 10000.0; // 10k ohm
float referenceVoltage = 5.0;

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

void loop() {
  int sensorValue = analogRead(batteryPin);
  float voltage = (sensorValue * referenceVoltage) / 1023.0;
  
  // Voltage Divider Formula
  float batteryVoltage = voltage * ( (R1 + R2) / R2 );

  Serial.print("Battery Voltage: ");
  Serial.print(batteryVoltage);
  Serial.println(" V");

  delay(1000);
}

📌 ចំណាំ

• Voltage Divider ជួយបន្ថយវ៉ុលថ្មមុនចូល Arduino
• អ្នកអាចកែ R1 និង R2 ដើម្បីវាស់វ៉ុលខ្ពស់ជាងនេះ
• សមស្របសម្រាប់ Battery 7.4V, 9V, 12V

==========================================================================
មេរៀនទី២៨: Obstacle Avoidance Robot (Full Autonomous Car)

មេរៀននេះបង្ហាញពីការបង្កើត រថយន្តឆ្លាតវៃស្វ័យប្រវត្តិ ដែលអាចប្រើ Ultrasonic Sensor ដើម្បីរកឃើញឧបសគ្គ ហើយបញ្ជា Motor ឲ្យបត់ឆ្វេង/ស្តាំដោយស្វ័យប្រវត្តិ។

📦 ឧបករណ៍ប្រើប្រាស់

  • Arduino UNO
  • Ultrasonic Sensor (HC-SR04)
  • Motor Driver (L298N / L293D)
  • DC Motors + Wheel
  • Battery Pack

🔌 គំរូកូដ Arduino


// Obstacle Avoidance Robot - Full Autonomous

#define trigPin 9
#define echoPin 10

#define motorA1 2
#define motorA2 3
#define motorB1 4
#define motorB2 5

long duration;
int distance;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
  pinMode(motorB1, OUTPUT);
  pinMode(motorB2, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  distance = getDistance();
  Serial.println(distance);

  if (distance > 25) {
    moveForward();
  } else {
    stopCar();
    delay(300);
    turnRight();
    delay(400);
  }
}

int getDistance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}

void moveForward() {
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, HIGH);
  digitalWrite(motorB2, LOW);
}

void turnRight() {
  digitalWrite(motorA1, HIGH);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, HIGH);
}

void stopCar() {
  digitalWrite(motorA1, LOW);
  digitalWrite(motorA2, LOW);
  digitalWrite(motorB1, LOW);
  digitalWrite(motorB2, LOW);
}
==========================================================================
មេរៀនទី២៩: Smart Home Full System

📌 សេចក្ដីណែនាំ

មេរៀននេះបង្ហាញពីការបង្កើត Smart Home Full System ដោយប្រើ Arduino/ESP, Sensor និង Module ផ្សេងៗ ដើម្បីគ្រប់គ្រងផ្ទះឆ្លាតវៃតាមរយៈ Automation។

🧰 ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino UNO ឬ ESP8266 / ESP32
  • Relay Module
  • DHT11 / DHT22 Sensor
  • PIR Motion Sensor
  • Light Bulb / Fan
  • WiFi Connection
💡 ចំណាំ: Smart Home អាចគ្រប់គ្រងភ្លើង, កង្ហារ, និង Sensor ដោយស្វ័យប្រវត្តិ ឬតាម Internet។

💻 Arduino Code (Smart Home)


#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11
#define RELAY1 8
#define PIR 7

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(RELAY1, OUTPUT);
  pinMode(PIR, INPUT);
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float temp = dht.readTemperature();
  int motion = digitalRead(PIR);

  if (motion == HIGH) {
    digitalWrite(RELAY1, HIGH);
  } else {
    digitalWrite(RELAY1, LOW);
  }

  Serial.print("Temperature: ");
  Serial.println(temp);
  delay(1000);
}
      

✅ សេចក្ដីសន្និដ្ឋាន

Smart Home Full System ជួយអោយផ្ទះមានភាពឆ្លាតវៃ សុវត្ថិភាព និងសន្សំថាមពល។ អ្នកអាចបន្ថែម Mobile App ឬ Web Dashboard នៅមេរៀនបន្ទាប់។

==========================================================================
មេរៀនទី៣០: Final Mega Project — Smart Security Robot
គោលបំណងមេរៀន:
• រចនារ៉ូបូតសុវត្ថិភាពឆ្លាតវៃ (Smart Security Robot)
• ប្រើ Sonar Sensor, PIR, Flame/Gas Sensor, Buzzer និង Servo Motor
• បង្ហាញ Alarm និងចលនារ៉ូបូតដោយស្វ័យប្រវត្តិ

📌 សម្ភារៈប្រើប្រាស់

  • Arduino UNO
  • Ultrasonic Sensor (HC-SR04)
  • PIR Motion Sensor
  • Gas / Flame Sensor
  • Servo Motor
  • Buzzer & LEDs

💻 Source Code (Arduino)


#include <Servo.h>

#define trigPin 10
#define echoPin 11
#define pirPin 2
#define gasPin A0
#define buzzer 6
#define ledRed 5
#define ledGreen 4

Servo securityServo;

long duration;
int distance;
int gasValue;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(pirPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);

  securityServo.attach(9);
  securityServo.write(90);

  Serial.begin(9600);
}

void loop() {
  // Ultrasonic Distance
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  // Gas Sensor
  gasValue = analogRead(gasPin);

  // PIR Motion
  int motion = digitalRead(pirPin);

  if (distance < 30 || motion == HIGH || gasValue > 400) {
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledGreen, LOW);
    securityServo.write(0);   // Lock / Alert
  } else {
    digitalWrite(buzzer, LOW);
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, HIGH);
    securityServo.write(90);  // Normal
  }

  delay(300);
}
    
📢 សេចក្តីសន្និដ្ឋាន:
Project នេះបង្ហាញពីការបញ្ចូល Sensor ជាច្រើនក្នុងប្រព័ន្ធតែមួយ ដើម្បីបង្កើត Smart Security Robot ដែលអាចប្រើបានពិតប្រាកដ។
==========================================================================
មេរៀនទី៣១: PID Motor Control (Robot Stability Control)
📘 PID Control ជាអ្វី?

PID (Proportional – Integral – Derivative) គឺជាវិធីសាស្រ្តគ្រប់គ្រង ដែលប្រើសម្រាប់រក្សាស្ថេរភាពរបស់ម៉ូទ័រ ឬ រ៉ូបូត ដោយកែសម្រួលល្បឿនម៉ូទ័រតាមកំហុស (Error)។

PID = P + I + D
• P: កែតាម Error បច្ចុប្បន្ន
• I: កែតាម Error សរុបអតីតកាល
• D: កែតាមល្បឿនបម្លាស់ប្តូរ Error
🔧 PID Formula
Output = (Kp * error) + (Ki * integral) + (Kd * derivative)
🤖 Arduino PID Motor Control Code
int motorPin = 9;

float Kp = 2.0;
float Ki = 0.5;
float Kd = 1.0;

float setPoint = 0;
float input, output;
float error, lastError = 0;
float integral = 0;

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

void loop() {
  input = readSensor();      // Read angle or position
  error = setPoint - input;

  integral += error;
  float derivative = error - lastError;

  output = (Kp * error) + (Ki * integral) + (Kd * derivative);
  output = constrain(output, 0, 255);

  analogWrite(motorPin, output);

  lastError = error;
  delay(10);
}

float readSensor() {
  return analogRead(A0);
}
✅ PID ប្រើសម្រាប់អ្វី?
  • រក្សាស្ថេរភាព Robot Balance
  • គ្រប់គ្រងល្បឿន DC Motor
  • Line Following Robot
  • Self-Balancing Robot
==========================================================================

មេរៀនទី៣២: Encoder Motor Reading (Measure Speed & Distance)

Encoder Motor គឺជាម៉ូទ័រដែលមាន Encoder ភ្ជាប់មកជាមួយ ដើម្បីរាប់ចំនួន Pulse សម្រាប់វាស់ ល្បឿន (Speed) និង ចម្ងាយ (Distance) នៃការបង្វិល។

គោលបំណងមេរៀន:
- ស្គាល់ Encoder Motor និង Pulse
- រាប់ Pulse ដោយ Arduino
- គណនាល្បឿន (RPM / Speed)
- គណនាចម្ងាយ (Distance)

1. ឧបករណ៍ដែលត្រូវប្រើ

  • Arduino UNO
  • DC Motor + Encoder
  • Motor Driver (L298N / L293D)
  • Jumper Wires
  • Power Supply

2. ការភ្ជាប់ Encoder Motor

Encoder Pin Arduino Pin
VCC 5V
GND GND
OUT A Pin 2 (Interrupt)
OUT B Pin 3

3. គោលការណ៍ដំណើរការ

Encoder នឹងបញ្ចេញ Pulse រាល់ពេលម៉ូទ័របង្វិល។ Arduino រាប់ Pulse ទាំងនេះ ដើម្បីគណនា:

  • Speed: Pulse ក្នុង 1 វិនាទី
  • Distance: Pulse × ចម្ងាយក្នុង 1 ជំហាន

4. Arduino Code (Measure Speed & Distance)


// Encoder Motor Reading - Speed & Distance
volatile long encoderCount = 0;

const int encoderPinA = 2; // Interrupt pin
const int pulsesPerRevolution = 20; // Encoder PPR
const float wheelDiameter = 6.5; // cm

unsigned long lastTime = 0;
long lastCount = 0;

void setup() {
  Serial.begin(9600);
  pinMode(encoderPinA, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), countEncoder, RISING);
}

void loop() {
  if (millis() - lastTime >= 1000) {
    long count = encoderCount - lastCount;
    lastCount = encoderCount;
    lastTime = millis();

    float rpm = (count * 60.0) / pulsesPerRevolution;
    float distance = (encoderCount / (float)pulsesPerRevolution) 
                     * (3.1416 * wheelDiameter);

    Serial.print("Speed (RPM): ");
    Serial.print(rpm);
    Serial.print(" | Distance (cm): ");
    Serial.println(distance);
  }
}

void countEncoder() {
  encoderCount++;
}

5. សេចក្ដីពន្យល់កូដ

  • encoderCount – រាប់ Pulse ពី Encoder
  • attachInterrupt() – រាប់ Pulse ដោយ Interrupt
  • RPM – គណនាពី Pulse ក្នុង 1 វិនាទី
  • Distance – គណនាពីចំនួនបង្វិល × បរិមាត្រកង់
==========================================================================
មេរៀនទី៣៣: Compass (HMC5883L / QMC5883L)
📌 គោលបំណងមេរៀន
• ស្គាល់ Compass Module (HMC5883L / QMC5883L)
• រៀនភ្ជាប់ Compass ជាមួយ Arduino
• អានទិសដៅ (Heading) ជាដឺក្រេ

🔌 ការភ្ជាប់ Compass ជាមួយ Arduino UNO

  • VCC → 5V
  • GND → GND
  • SDA → A4
  • SCL → A5

💻 Arduino Code (QMC5883L)


// Compass QMC5883L Example
#include <Wire.h>
#include <QMC5883LCompass.h>

QMC5883LCompass compass;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  compass.init();
}

void loop() {
  compass.read();

  int azimuth = compass.getAzimuth();

  Serial.print("Heading: ");
  Serial.print(azimuth);
  Serial.println("°");

  delay(500);
}
📐 ការបកស្រាយ
Azimuth គឺជាមុំទិសដៅ (0° – 360°)
• 0° = ជើង, 90° = កើត, 180° = ត្បូង, 270° = លិច

🧪 សកម្មភាពអនុវត្ត

  • បង្វិល Compass ជុំវិញ
  • មើលតម្លៃ Heading នៅ Serial Monitor
  • សាកល្បងបន្ថែម LCD ឬ OLED សម្រាប់បង្ហាញទិស
==========================================================================
មេរៀនទី៣៤: Power Management (5V, 9V, 12V, 18650 Battery)
មេរៀននេះពន្យល់អំពីការគ្រប់គ្រងថាមពល (Power Management) សម្រាប់ Project Arduino / Microcontroller ដោយប្រើ 5V, 9V, 12V Adapter និង ថ្ម 18650 ដើម្បីឲ្យឧបករណ៍ដំណើរការបានមានសុវត្ថិភាព និងមានស្ថេរភាព។

// Power Management Example
// 5V, 9V, 12V & 18650 Battery

const int power5V  = 5;
const int power9V  = 9;
const int power12V = 12;

float batteryVoltage = 3.7; // 18650 Battery

void setup() {
  Serial.begin(9600);
  Serial.println("Power Management System Ready");
}

void loop() {
  Serial.print("Battery Voltage: ");
  Serial.println(batteryVoltage);

  if(batteryVoltage < 3.3) {
    Serial.println("Warning: Low Battery!");
  }

  delay(2000);
}
==========================================================================
មេរៀនទី៣៥: Wireless RC Robot Using NRF24L01
📌 សេចក្តីណែនាំ

មេរៀននេះបង្ហាញពីការបង្កើត Wireless RC Robot ដោយប្រើម៉ូឌុល NRF24L01 សម្រាប់បញ្ជាពីចម្ងាយ រវាង Arduino (Controller) និង Arduino (Robot)។

🧰 ឧបករណ៍ដែលត្រូវប្រើ
  • Arduino UNO x2
  • NRF24L01 Module x2
  • L298N / L293D Motor Driver
  • DC Motors + Robot Chassis
  • Joystick Module
  • Battery & Jumper Wires
🔌 កូដ Arduino (Controller)
Controller Code (NRF24L01)

// Wireless RC Robot - Controller
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);
const byte address[6] = "00001";

int joyX = A0;
int joyY = A1;

struct Data {
  int x;
  int y;
};

Data data;

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  data.x = analogRead(joyX);
  data.y = analogRead(joyY);
  radio.write(&data, sizeof(Data));
}
🤖 កូដ Arduino (Robot)
Robot Code (NRF24L01)

// Wireless RC Robot - Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);
const byte address[6] = "00001";

struct Data {
  int x;
  int y;
};

Data data;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(&data, sizeof(Data));
    Serial.print("X: ");
    Serial.print(data.x);
    Serial.print(" Y: ");
    Serial.println(data.y);
  }
}
==========================================================================
📱 មេរៀនទី៣៦: Android App Control (MIT App Inventor)

📘 សេចក្ដីផ្តើម

មេរៀននេះនឹងបង្ហាញពីរបៀបបង្កើត Android App ដោយប្រើ MIT App Inventor ដើម្បីបញ្ជាឧបករណ៍ Arduino ឬ Microcontroller តាមរយៈ Bluetooth

👉 MIT App Inventor គឺជា Platform Drag & Drop ដែលមិនចាំបាច់សរសេរកូដស្មុគស្មាញ។

🧰 ឧបករណ៍ដែលត្រូវការ

  • Android Phone
  • MIT App Inventor (ai2.appinventor.mit.edu)
  • Arduino UNO
  • Bluetooth Module (HC-05 / HC-06)
  • USB Cable

💻 Arduino Code (Bluetooth Control)

char data;

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

void loop() {
  if (Serial.available()) {
    data = Serial.read();
    if (data == '1') {
      digitalWrite(13, HIGH);
    }
    if (data == '0') {
      digitalWrite(13, LOW);
    }
  }
}
      

📱 ជំហានបង្កើត App ក្នុង MIT App Inventor

  1. បង្កើត Project ថ្មី
  2. បន្ថែម Button (ON / OFF)
  3. បន្ថែម BluetoothClient
  4. កំណត់ Blocks សម្រាប់ Send Text "1" និង "0"
⚠️ ត្រូវ Pair Bluetooth Module ជាមួយ Android Phone ជាមុនសិន។

==========================================================================

មេរៀនទី៣៧: Robot Mapping with Ultrasonic (Basic SLAM Concept)

ក្នុងមេរៀននេះ យើងនឹងរៀនពីវិធីបង្កើត Robot Mapping ដោយប្រើ Ultrasonic Sensor ដើម្បីចាប់ចម្ងាយ និងគណនាទីតាំងដោយ Basic SLAM Concept

Hardware Required:

  • Arduino UNO / Mega
  • Ultrasonic Sensor (HC-SR04)
  • Servo Motor
  • Wheels / Robot Chassis
  • Breadboard & Jumper Wires

Circuit Connection:

  • HC-SR04 Trig → Arduino Pin 9
  • HC-SR04 Echo → Arduino Pin 10
  • Servo Signal → Arduino Pin 6
  • VCC & GND properly connected

Arduino Code:

#include 

const int trigPin = 9;
const int echoPin = 10;
Servo myServo;

long duration;
int distance;

void setup() {
  Serial.begin(9600);
  myServo.attach(6);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  for(int angle = 0; angle <= 180; angle += 30) {
    myServo.write(angle);
    delay(500);
    distance = readUltrasonic();
    Serial.print("Angle: "); Serial.print(angle);
    Serial.print(" Distance: "); Serial.println(distance);
  }
}

int readUltrasonic() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2;
}
==========================================================================

មេរៀនទី ៣៨: Line Following with PID (Robotics Competition)

ក្នុងមេរៀននេះ យើងនឹងរៀនពីការត្រួតបន្ទាត់សំរាប់រថយន្តរ៉ូបូតប្រើ PID Control ដើម្បីធានាថារថយន្តអាចបន្តផ្លូវបានយ៉ាងត្រឹមត្រូវក្នុងប្រកួត Robotics។

ខ្លឹមសារ៖

  • ការប្រើប្រាស់ Sensor Detect ផ្លូវ
  • ការគណនាកម្លាំង PID (Proportional, Integral, Derivative)
  • ការគ្រប់គ្រង Motor Speed ដោយ PID

Code Example (Arduino):

#include 

// Define Motors
AF_DCMotor motorLeft(1);
AF_DCMotor motorRight(2);

// PID variables
double Kp = 0.8;
double Ki = 0.1;
double Kd = 0.05;

int lastError = 0;
int integral = 0;

void setup() {
  Serial.begin(9600);
  pinMode(A0, INPUT); // Left sensor
  pinMode(A1, INPUT); // Right sensor
}

void loop() {
  int leftSensor = analogRead(A0);
  int rightSensor = analogRead(A1);

  int error = leftSensor - rightSensor;
  integral += error;
  int derivative = error - lastError;

  int output = Kp*error + Ki*integral + Kd*derivative;

  motorLeft.setSpeed(constrain(150 - output, 0, 255));
  motorRight.setSpeed(constrain(150 + output, 0, 255));

  motorLeft.run(FORWARD);
  motorRight.run(FORWARD);

  lastError = error;
  delay(10);
}
==========================================================================

មេរៀនទី ៣៩: Obstacle Avoidance AI Mode (Decision Tree Logic)

ក្នុងមេរៀននេះ យើងនឹងបង្ហាញរបៀបប្រើ Decision Tree Logic ដើម្បីធ្វើឲ្យរ៉ូបូតជៀសវាងឧបសគ្គដោយស្វ័យប្រវត្តិ។

Components Required:

  • Arduino UNO / Microcontroller
  • Ultrasonic Sensor (HC-SR04)
  • Motor Driver (L298N)
  • DC Motors + Wheels
  • Battery

Wiring:

  • HC-SR04 VCC -> 5V, GND -> GND, Trig -> D9, Echo -> D10
  • Motor Driver IN1, IN2, IN3, IN4 -> Arduino D3, D4, D5, D6
  • Motor Power -> Battery, GND -> Common GND

Code Example:

// Arduino Obstacle Avoidance with Decision Tree
#include 

#define TRIG_PIN 9
#define ECHO_PIN 10
#define MAX_DISTANCE 100

NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  delay(50);
  int distance = sonar.ping_cm();
  Serial.println(distance);

  if(distance > 20 || distance == 0) {
    moveForward();
  } else {
    // Decision Tree Logic
    int choice = random(0,2);
    if(choice == 0) turnLeft();
    else turnRight();
  }
}

void moveForward(){
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);
}

void turnLeft(){
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,HIGH);
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);
  delay(300);
}

void turnRight(){
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,HIGH);
  delay(300);
}

==========================================================================

មេរៀនទី ៤០: Smart Robot (Multisensor Integration)

ក្នុងមេរៀននេះ យើងនឹងបង្ហាញរបៀបភ្ជាប់ឧបករណ៍ច្រើនជាមួយរ៉ូបូតដើម្បីធ្វើការសម្រេចចិត្តស្វ័យប្រវត្តិ។

Components Required:

  • Arduino UNO / Microcontroller
  • Ultrasonic Sensor HC-SR04
  • IR Sensor
  • Temperature Sensor (DHT11)
  • Motor Driver + DC Motors
  • Buzzer + LED

Code Example:

// Arduino Smart Robot with Multisensor
#include 
#include 

#define TRIG_PIN 9
#define ECHO_PIN 10
#define MAX_DISTANCE 100
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);

#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define IR_PIN 7
#define BUZZER 8

#define IN1 3
#define IN2 4
#define IN3 5
#define IN4 6

void setup() {
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(IR_PIN, INPUT);
  pinMode(BUZZER, OUTPUT);
  dht.begin();
  Serial.begin(9600);
}

void loop() {
  int distance = sonar.ping_cm();
  int irStatus = digitalRead(IR_PIN);
  float temp = dht.readTemperature();

  if(distance < 20 || irStatus == HIGH || temp > 35){
    stopRobot();
    digitalWrite(BUZZER,HIGH);
  } else {
    moveForward();
    digitalWrite(BUZZER,LOW);
  }
  delay(200);
}

void moveForward(){
  digitalWrite(IN1,HIGH);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,HIGH);
  digitalWrite(IN4,LOW);
}

void stopRobot(){
  digitalWrite(IN1,LOW);
  digitalWrite(IN2,LOW);
  digitalWrite(IN3,LOW);
  digitalWrite(IN4,LOW);
}
==========================================================================

មេរៀនទី៤១: Gyroscope + Accelerometer (MPU6050) — Robot Balancing

ក្នុងមេរៀននេះ យើងនឹងរៀនពីរបៀបប្រើ MPU6050 ដើម្បីបញ្ចូល Gyroscope និង Accelerometer សម្រាប់កង់រៀន Robot Balancing។

ឧបករណ៍ដែលត្រូវការ:

  • Arduino UNO / Nano
  • MPU6050 Module
  • Motor Driver (L298N)
  • DC Motors + Wheels
  • Battery 7.4V
  • Jumper Wires

របៀបភ្ជាប់:

  • MPU6050 VCC → 5V, GND → GND
  • MPU6050 SDA → A4, SCL → A5
  • Motors → L298N Output
  • L298N IN1, IN2, IN3, IN4 → Arduino Digital Pins

Code Arduino:

#include 
#include 

MPU6050 mpu;
int motorPin1 = 3;
int motorPin2 = 5;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  mpu.initialize();
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
}

void loop() {
  int16_t ax, ay, az;
  int16_t gx, gy, gz;

  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  float angle = atan2(ay, az) * 180 / PI;
  Serial.println(angle);

  if(angle > 5){
    analogWrite(motorPin1, 150);
    analogWrite(motorPin2, 0);
  } else if(angle < -5){
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 150);
  } else {
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 0);
  }

  delay(10);
}
  
==========================================================================

មេរៀនទី ៤២: 2-Wheel Self Balancing Robot (PID + MPU6050)

ក្នុងមេរៀននេះ យើងនឹងរៀនពីរបៀបបង្កើត **2-Wheel Self Balancing Robot** ដោយប្រើ **PID Control** និង **MPU6050 Gyro + Accelerometer**។ វាពេញនិយមសម្រាប់រៀនពី robotics និង control system។

ឧបករណ៍ដែលត្រូវការ:

  • Arduino Uno / Nano
  • MPU6050 Gyro + Accelerometer
  • Motor Driver (L298N / L293D)
  • 2 DC Motors with wheels
  • Battery Pack (6V-12V)
  • Chassis for 2-wheel robot

Wiring Diagram:

  • MPU6050 SDA → A4, SCL → A5
  • Motor Driver IN1, IN2 → Arduino pins 3, 4
  • Motor Driver IN3, IN4 → Arduino pins 5, 6
  • Motor Driver VCC → Battery+, GND → Battery-

Arduino Code:

#include 
#include 

MPU6050 mpu;

float setPoint = 0; // target angle
float input, output;
float Kp = 30, Ki = 1, Kd = 0.8;
float lastInput = 0;
float integral = 0;

void setup() {
  Wire.begin();
  Serial.begin(9600);
  mpu.initialize();
  // motor pins
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {
  int16_t ax, ay, az, gx, gy, gz;
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  float angle = atan2(ax, az) * 180 / PI;
  
  input = angle;
  float error = setPoint - input;
  integral += error;
  float derivative = input - lastInput;
  
  output = Kp * error + Ki * integral - Kd * derivative;
  
  lastInput = input;
  
  // Motor control
  if(output > 0){
    analogWrite(3, output);
    analogWrite(4, 0);
    analogWrite(5, output);
    analogWrite(6, 0);
  } else {
    analogWrite(3, 0);
    analogWrite(4, -output);
    analogWrite(5, 0);
    analogWrite(6, -output);
  }
  
  delay(10);
}

បញ្ជាក់សំខាន់:

  • ប្ដូរ Kp, Ki, Kd ដើម្បីបង្កើនស្ថិរភាព robot
  • ប្រើ battery ដើម្បីផ្គត់ផ្គង់ motor ដាច់ពី Arduino
  • ឧបករណ៍ chassis និង wheel alignment ត្រូវតែសម្រួលអោយស្អាត
==========================================================================

មេរៀនទី៤៣: IoT Cloud (Blynk / Thingspeak / MQTT)

ក្នុងមេរៀននេះ យើងនឹងរៀនពីរបៀបភ្ជាប់ឧបករណ៍ IoT ទៅ Cloud platform ផ្សេងៗ ដូចជា Blynk, Thingspeak, និង MQTT ដើម្បីអាចបញ្ជូន និងទទួលទិន្នន័យពីឧបករណ៍បានពីចម្ងាយ។

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

// Replace with your network credentials
char ssid[] = "YourWiFi";
char pass[] = "YourPassword";

// Blynk Auth Token
char auth[] = "YourAuthToken";

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
}

void loop() {
  Blynk.run();
}
    

ចំណាំ៖ អ្នកអាចផ្លាស់ប្តូរ WiFi SSID, Password, និង Blynk Auth Token ជាមួយព័ត៌មានផ្ទាល់ខ្លួនរបស់អ្នក។ នេះគឺជាគំរូសម្រាប់ ESP32, តែអ្នកអាចប្រើជាមួយ ESP8266 ដោយផ្លាស់ប្តូរទីតាំង library ។

==========================================================================

មេរៀនទី៤៤: Data Logging With SD Card Module

នៅមេរៀននេះ យើងនឹងរៀនពីរបៀបប្រើ SD Card Module ដើម្បីបង្វិលទិន្នន័យពី Arduino ឬ Microcontroller ផ្ទុកក្នុង SD Card។

ឧបករណ៍ដែលត្រូវការ:

  • Arduino Uno / Nano
  • SD Card Module
  • SD Card (FAT32 formatted)
  • Jumper wires

ការតភ្ជាប់ឧបករណ៍:

  • CS → Pin 10 (Arduino)
  • SCK → Pin 13 (Arduino)
  • MOSI → Pin 11 (Arduino)
  • MISO → Pin 12 (Arduino)
  • VCC → 5V
  • GND → GND

Code Arduino

// Include the SD library
#include 
#include 

const int chipSelect = 10;

void setup() {
  Serial.begin(9600);
  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("Card initialized.");
  
  // Create a file
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.println("Hello SD Card!");
    dataFile.close();
    Serial.println("Data written successfully.");
  } else {
    Serial.println("Error opening datalog.txt");
  }
}

void loop() {
  // Nothing to do here
}

បន្ទាប់ពី upload code និង SD Card ទៅ Arduino អ្នកនឹងឃើញ file datalog.txt ត្រូវបានបង្កើត និងបញ្ចូលទិន្នន័យ!

==========================================================================

មេរៀនទី ៤៥: Fingerprint Sensor (Biometric Security)

នៅមេរៀននេះ យើងនឹងរៀនពីការភ្ជាប់និងប្រើប្រាស់ Fingerprint Sensor ជាមួយ Arduino ដើម្បីបង្កើតប្រព័ន្ធសន្តិសុខ Biometric។

ឧបករណ៍ចាំបាច់:

  • Arduino UNO
  • Fingerprint Sensor (e.g., R307, GT-511C3)
  • Jumper Wires
  • Breadboard
  • USB Cable

របៀបភ្ជាប់ឧបករណ៍:

  • VCC → 5V Arduino
  • GND → GND Arduino
  • TX → Pin 2 Arduino
  • RX → Pin 3 Arduino

Code Example:

#include 
#include 

SoftwareSerial mySerial(2, 3); // RX, TX
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup() {
  Serial.begin(9600);
  while (!Serial); 
  Serial.println("Fingerprint Sensor initializing...");
  
  if (finger.begin()) {
    Serial.println("Sensor found!");
  } else {
    Serial.println("Sensor not found. Check connections.");
    while (1);
  }
}

void loop() {
  Serial.println("Place your finger on the sensor...");
  int id = getFingerprintID();
  if (id != -1) {
    Serial.print("Fingerprint ID: "); Serial.println(id);
  }
  delay(2000);
}

int getFingerprintID() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK) return -1;
  
  p = finger.image2Tz();
  if (p != FINGERPRINT_OK) return -1;
  
  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK) return -1;
  
  return finger.fingerID;
}

សេចក្តីណែនាំបន្ថែម:

  • ត្រូវតែប្រើ SoftwareSerial ដើម្បីភ្ជាប់ Fingerprint Sensor ជាមួយ Arduino UNO។
  • ពិនិត្យការភ្ជាប់សាកល្បងមុន Upload Code។
  • អ្នកអាចបង្កើតប្រព័ន្ធសន្តិសុខ Biometric ដោយបន្ថែម RelayLED សម្រាប់បង្ហាញស្ថានភាព។
==========================================================================

មេរៀនទី៤៦: Long Range Communication (LoRa SX1278)

ការណែនាំ៖ ការប្រើប្រាស់ Module LoRa SX1278 សម្រាប់ទំនាក់ទំនងឆ្ងាយខ្ពស់។

អំពី LoRa SX1278:

  • Frequency: 433/868/915 MHz
  • Range: ទៅបាន 2-5 km ក្នុងស្ថានភាពក្រៅផ្ទះ
  • Module supports point-to-point និង point-to-multipoint communication

Hardware Connection Example (Arduino UNO):

  • MOSI -> D11
  • MISO -> D12
  • SCK -> D13
  • NSS -> D10
  • RESET -> D9
  • DIO0 -> D2
  • VCC -> 3.3V
  • GND -> GND

Sample Arduino Code:

#include 
#include 

#define SS 10
#define RST 9
#define DIO0 2

void setup() {
  Serial.begin(9600);
  while (!Serial);

  LoRa.setPins(SS, RST, DIO0);

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initializing OK!");
}

void loop() {
  Serial.println("Sending packet...");
  LoRa.beginPacket();
  LoRa.print("Hello LoRa");
  LoRa.endPacket();
  delay(2000);
}
==========================================================================

មេរៀនទី ៤៨: AI Camera (ESP32-CAM)

ក្នុងមេរៀននេះ យើងនឹងរៀនពីរបៀបប្រើ ESP32-CAM ជាមួយ AI ដើម្បីចាប់រូបភាព និងស្គាល់វត្ថុដោយប្រើ MicroPython / Arduino IDE។

ឧបករណ៍ដែលត្រូវការ:

  • ESP32-CAM Module
  • FTDI Programmer
  • Jumper wires
  • Computer និង Arduino IDE

កូដសម្រាប់ ESP32-CAM (Arduino IDE)

#include "esp_camera.h"
#include 

const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";

// Camera Pin Configuration
#define PWDN_GPIO_NUM    32
#define RESET_GPIO_NUM   -1
#define XCLK_GPIO_NUM     0
#define SIOD_GPIO_NUM    26
#define SIOC_GPIO_NUM    27
#define Y9_GPIO_NUM      35
#define Y8_GPIO_NUM      34
#define Y7_GPIO_NUM      39
#define Y6_GPIO_NUM      36
#define Y5_GPIO_NUM      21
#define Y4_GPIO_NUM      19
#define Y3_GPIO_NUM      18
#define Y2_GPIO_NUM       5
#define VSYNC_GPIO_NUM   25
#define HREF_GPIO_NUM    23
#define PCLK_GPIO_NUM    22

void startCameraServer();

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  if(psramFound()){
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

  // Camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  startCameraServer();

  Serial.println("Camera Ready! Go to: http://"+WiFi.localIP().toString());
}

void loop() {
  // put your main code here, to run repeatedly:
}

#include 
WebServer server(80);

void handle_jpg_stream(void) {
  WiFiClient client = server.client();
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: multipart/x-mixed-replace; boundary=frame");
  client.println();
  while (1) {
    // Capture frame
    camera_fb_t * fb = esp_camera_fb_get();
    if (!fb) {
      Serial.println("Camera capture failed");
      continue;
    }
    client.printf("--frame\r\nContent-Type: image/jpeg\r\nContent-Length: %u\r\n\r\n", fb->len);
    client.write(fb->buf, fb->len);
    client.println();
    esp_camera_fb_return(fb);
    delay(100);
  }
}

void startCameraServer(){
  server.on("/", HTTP_GET, [](){
    server.send(200, "text/html", "

ESP32-CAM AI Camera

"); }); server.on("/stream", HTTP_GET, handle_jpg_stream); server.begin(); }
==========================================================================

មេរៀនទី៤៩: Smart Energy Monitoring (Current Sensor ACS712)

ក្នុងមេរៀននេះ យើងនឹងរៀនពីរបៀបប្រើ Current Sensor ACS712 ដើម្បីវាស់ថាមពលអគ្គិសនី និងតាមដានប្រើប្រាស់ថាមពលដោយ Arduino ឬ ESP32 ។

#include <Arduino.h>

const int currentPin = A0; // ACS712 sensor pin
float voltage = 0.0;
float current = 0.0;
float currentOffset = 2.5; // Adjust based on your module

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

void loop() {
  voltage = analogRead(currentPin) * (5.0 / 1023.0);
  current = (voltage - currentOffset) / 0.185; // ACS712 5A version sensitivity
  Serial.print("Current: ");
  Serial.print(current);
  Serial.println(" A");
  delay(1000);
}
    
==========================================================================

មេរៀនទី៥០: Full Autonomous Robot (AI + Sensors Integration)

មេរៀននេះស្វែងយល់ពីរបៀបបង្កើត Autonomous Robot ដែលប្រើប្រាស់ AI និង Sensors Integration ដើម្បីឲ្យវាអាចស្វែងរកផ្លូវ និងជៀសវាងឧបសគ្គដោយឯករាជ្យ។

បំពាក់ និងឧបករណ៍ដែលត្រូវការសម្រាប់ប្រើក្នុង Project:

  • Arduino UNO / Mega / ESP32
  • Motor Driver (L298N)
  • Ultrasonic Sensor HC-SR04
  • IR Sensor / Line Follower Sensor
  • AI Module / OpenCV Camera (optional)
  • Buzzer និង LED សំរាប់សញ្ញា
  • Battery Pack / Power Supply

ការតភ្ជាប់ឧបករណ៍ (Circuit Diagram):

សូមភ្ជាប់សៀគ្វីដូចខាងក្រោម៖

  • Ultrasonic Sensor -> Trig & Echo to Arduino Digital Pins
  • Motor Driver -> Motors + Arduino PWM Pins
  • Line Follower Sensor -> Arduino Analog/Digital Pins
  • AI Camera -> Arduino / Raspberry Pi Integration

Code សម្រាប់ Robot Autonomous:

// Full Autonomous Robot AI + Sensors Integration
#include 
#include 

#define TRIG_PIN 9
#define ECHO_PIN 10
#define LEFT_MOTOR 5
#define RIGHT_MOTOR 6

Ultrasonic ultrasonic(TRIG_PIN, ECHO_PIN);

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

void loop() {
  long distance = ultrasonic.read();
  Serial.println(distance);

  if(distance < 20) {
    // Stop or turn
    analogWrite(LEFT_MOTOR, 0);
    analogWrite(RIGHT_MOTOR, 0);
    delay(500);
    analogWrite(LEFT_MOTOR, 150);
    analogWrite(RIGHT_MOTOR, 0);
    delay(300);
  } else {
    // Move forward
    analogWrite(LEFT_MOTOR, 200);
    analogWrite(RIGHT_MOTOR, 200);
  }
}

ការពន្យល់ Code:

Code ខាងលើសម្រាប់ប្រើ Ultrasonic Sensor ដើម្បីឆែកចម្ងាយ ហើយបើមានឧបសគ្គ < 20cm Robot នឹងបញ្ឈប់ និងបង្វិល។ Motor ត្រូវបានគ្រប់គ្រងដោយ PWM ដើម្បីបញ្ជារថ្មីលឿន/យឺត។ អ្នកអាចបន្ថែម AI Camera ឬ IR Sensors ដើម្បីធ្វើឲ្យ Robot ឆ្លាតវៃជាងនេះ។

==========================================================================

មេរៀន: បង្ហាញសីតុណ្ហភាព និងសំណើមលើ LCD និង Serial Monitor

ក្នុងមេរៀននេះ យើងនឹងប្រើ DHT11 sensor ដើម្បីបង្ហាញសីតុណ្ហភាព និងសំណើម (Humidity) លើ LCD 16x2 I2C និង Serial Monitor

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");

  lcd.setCursor(0,0);
  lcd.print("Temp: "); 
  lcd.print(t); 
  lcd.print(" C");
  
  lcd.setCursor(0,1);
  lcd.print("Humidity: "); 
  lcd.print(h); 
  lcd.print(" %");

  delay(2000);
}
  
==========================================================================

Radar Detection System ដោយប្រើ Arduino + Processing

ក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបបង្កើត Radar Detection System ដោយប្រើ Arduino និង Processing ដែលអាចស្គាល់វត្ថុក្នុងជុំវិញប្រព័ន្ធ និងបង្ហាញលទ្ធផលជាក្រាហ្វិកលើកុំព្យូទ័រ។

  • ភ្ជាប់ឧបករណ៍ជាមួយ Arduino (Ultrasonic Sensor, Servo Motor, LEDs, Buzzer)
  • សរសេរកូដ Arduino ដើម្បីប្រមូលទិន្នន័យចម្ងាយ
  • បង្ហាញលទ្ធផល Radar ក្នុង Processing IDE
  • ប្រើប្រព័ន្ធសម្រាប់ស្គាល់វត្ថុ និងបង្ហាញលទ្ធផលលើកុំព្យូទ័រ

Step 1: Hardware Connection

ភ្ជាប់ឧបករណ៍ដូចខាងក្រោម៖

  • Ultrasonic Sensor: Trig → Pin 9, Echo → Pin 10
  • Servo Motor: Signal → Pin 3
  • LEDs: Green → Pin 4, Red → Pin 5
  • Buzzer: Pin 6
  • Power & GND connect as usual

Tip: អាចបញ្ចូល Diagram រូបភាពជាក់ស្តែងនៅទីនេះ

Step 2: Arduino Code

#include <Servo.h>

const int trigPin = 9;
const int echoPin = 10;
const int greenLED = 4;
const int redLED = 5;
const int buzzer = 6;

Servo myServo;
long duration;
int distance;

void setup() {
  Serial.begin(9600);
  myServo.attach(3);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop() {
  for(int angle=0; angle<=180; angle+=5){
    myServo.write(angle);
    delay(200);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2;

    Serial.print(angle);
    Serial.print(",");
    Serial.println(distance);

    if(distance > 0 && distance < 30){
      digitalWrite(redLED, HIGH);
      digitalWrite(greenLED, LOW);
      digitalWrite(buzzer, HIGH);
    } else {
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
      digitalWrite(buzzer, LOW);
    }
  }
  for(int angle=180; angle>=0; angle-=5){
    myServo.write(angle);
    delay(200);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance = duration * 0.034 / 2;

    Serial.print(angle);
    Serial.print(",");
    Serial.println(distance);

    if(distance > 0 && distance < 30){
      digitalWrite(redLED, HIGH);
      digitalWrite(greenLED, LOW);
      digitalWrite(buzzer, HIGH);
    } else {
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
      digitalWrite(buzzer, LOW);
    }
  }
}

Step 3: Processing Code (Radar Visualization)

import processing.serial.*;

Serial myPort;
float angle, distance;

void setup() {
  size(400, 400);
  myPort = new Serial(this, "COM3", 9600); // Change COM3 to your Arduino port
  background(0);
}

void draw() {
  background(0);
  translate(width/2, height/2);
  stroke(0, 255, 0);
  noFill();
  ellipse(0, 0, 350, 350); // Radar circle

  while(myPort.available() > 0){
    String data = myPort.readStringUntil('\n');
    if(data != null){
      data = trim(data);
      String[] parts = split(data, ',');
      if(parts.length == 2){
        angle = float(parts[0]);
        distance = float(parts[1]);
        if(distance > 0){
          float r = map(distance, 0, 100, 0, 175);
          float x = r * cos(radians(angle));
          float y = r * sin(radians(angle));
          fill(255, 0, 0);
          noStroke();
          ellipse(x, y, 8, 8); // Object dot
        }
      }
    }
  }
}

Step 4: Run the System

  1. Upload Arduino code to your board.
  2. Open Processing sketch and select the correct COM port.
  3. Observe the radar screen displaying detected objects in real-time.
  4. LED និង Buzzer នឹងបើកសញ្ញាពេលវត្ថុស្ថិតនៅចម្ងាយ < 30cm។

Tip: អ្នកអាចបន្ថែម Diagram និង វីដេអូ demonstration ដើម្បីធ្វើឲ្យមេរៀនមានភាពច្បាស់លាស់។

==========================================================================

==========================================================================

==========================================================================

==========================================================================

==========================================================================

==========================================================================

==========================================================================

==========================================================================


Post a Comment

0 Comments