วันพุธที่ 20 ธันวาคม พ.ศ. 2560

Code โปรเจคหุ่นยนต์ UNO + L298P บังคับด้วย Bluetooth



โปรเจค นี้จะเป็นการนำ Arduino UNO R3 และ L298P Motor Shield Board มาทำเป็น หุ่นยนต์ บังคับด้วย สมาร์ทโฟน แอนดรอยด์ ผ่านทาง Bluetooth HC-06 ซึ่งการใช้ 
L298P Motor Shield จะทำให้การประกอบหุ่นยนต์ สะดวกและง่ายขึ้น เมื่อเทียบกับการใช้ Motor Driver Module L298N



อุปกรณ์ที่ใช้



1. 2WD Smart Car Robot Chassis Kits

2. Arduino UNO R3 - Made in italy

3. L298P Motor Shield Board


4. Jumper (M2M) cable 20cm Male to Male

5. แจ๊กขั้วถ่าน 9 โวลต์ สำหรับ Ardiuno

6. สกรูหัวกลม+น็อตตัวเมีย ยาว 12 มม.

7. รางถ่าน 18650 แบบ 2 ก้อน

8. ถ่านชาร์จ 18650 Panasonic NCR18650B 3.7v  จำนวน 2 ก้อน

9. Jumper (F2M) cable 20cm Female to Male

10. Bluetooth HC-06 Slave





1. ประกอบหุ่นยนต์



เริ่มต้นด้วยการ ประกอบ หุ่นยนต์ UNO + L298P และ ทดสอบการเคลื่อนที่ของหุ่นยนต์ ตามลิงค์ บทความด้านล่าง

https://robotsiam.blogspot.com/2017/12/uno-l298p.html



2. ประกอบ Bluetooth HC-06



ใช้ จั้มเปอร์ ผู้-เมีย เชื่อมต่อ Bluetooth HC-06 เข้ากับ L298P ตามรูปด้านล่าง




HC-06 <-> L298P

VCC 
<-> 5V
GND 
<-> Gnd
TXD 
<-> TX
RXD 
<-> RX










3. การอัพโหลดโปรแกรม

ก่อน Upload ให้ถอดการเชื่อมต่อสาย TX และ RX ออกจาก L298P (ถ้าไม่ถอดออก อาจมีปัญหาในการ Upload)



ใช้สาย USB เชื่อมต่อระหว่าง คอมพิวเตอร์ กับ Arduino UNO R3



เปิดโปรแกรม Arduino (IDE) และ Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3




/*

    Bluetooth Robot with Arduino UNO + L298P
    For more details visit:
    https://robotsiam.blogspot.com/2017/12/uno-l298p-bluetooth.html

*/

#include <SoftwareSerial.h>

int incomingByte = 0;

/*-------definning Outputs------*/

int MA1 = 12;     // Motor A1
int MA2 =  3;     // Motor A2
int PWM_A =  10;   // Speed Motor A

int MB1 =  13;     // Motor B1
int MB2 =  8;     // Motor B2
int PWM_B =  11;  // Speed Motor B

int SPEED = 200;  // Speed PWM สามารถปรับความเร็วได้ถึง 0 - 255

void setup() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Motor A1
  pinMode(3, OUTPUT); //Motor A2
  pinMode(10, OUTPUT); //Speed PWM Motor A

  //Setup Channel B
  pinMode(13, OUTPUT);  //Motor B1
  pinMode(8, OUTPUT);  //Motor B2
  pinMode(11, OUTPUT); //Speed PWM Motor B

  Serial.begin(9600);
  Serial.println("Motor Ready");

}

void loop() {



  if (Serial.available() > 0) {
    incomingByte = Serial.read();
  }

  switch (incomingByte)
  {
    case 'S':
      // stop all motors
      {
        Stop(1);
        Serial.println("Stop\n");
        incomingByte = '*';
      }

      break;

    case 'F':
      // turn it on going forward
      {
        Forward(1);
        Serial.println("Forward\n");
        incomingByte = '*';
      }
      break;

    case 'B':
      // turn it on going backward
      {
        Backward(1);
        Serial.println("Backward\n");
        incomingByte = '*';
      }
      break;

    case 'R':
      // turn right
      {
        turnRight(1);
        Serial.println("Rotate Right\n");
        incomingByte = '*';
      }
      break;


    case 'L':
      // turn left
      {
        turnLeft(1);
        Serial.println("Rotate Left\n");
        incomingByte = '*';
      }
      break;

  }
}

void Forward(int time)
{
  digitalWrite(MA1, LOW);
  digitalWrite(MA2, HIGH);
  analogWrite(PWM_A, SPEED);

  digitalWrite(MB1, HIGH);
  digitalWrite(MB2, LOW);
  analogWrite(PWM_B, SPEED);

  delay(time);
}

void Backward(int time)
{
  digitalWrite(MA1, HIGH);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, SPEED);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, HIGH);
  analogWrite(PWM_B, SPEED);

  delay(time);
}

void turnLeft(int time)
{
  digitalWrite(MA1, HIGH);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, SPEED);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, LOW);
  analogWrite(PWM_B, 0);

  delay(time);
}

void turnRight(int time)
{
  digitalWrite(MA1, LOW);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, 0);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, HIGH);
  analogWrite(PWM_B, SPEED);

  delay(time);
}

void Stop(int time)
{
  digitalWrite(MA1, LOW);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, 0);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, LOW);
  analogWrite(PWM_B, 0);

  delay(time);

}



ไปที่ Tools > Board เลือกเป็น Arduino/Genuino UNO



ไปที่ Tools > Port แล้วเลือกพอร์ตที่ปรากฏ (กรณีใช้เครื่องคอมพิวเตอร์ที่มี COM Port มากกว่าหนึ่ง  ให้เลือกตัวอื่นที่ไม่ใช่ COM1)


ในตัวอย่างเลือกเป็น "COM14"

(ถ้ายังว่าง หรือ เป็น COM1 ให้ตรวจสอบการติดตั้งไดร์เวอร์ การต่อสาย USB ของ Arduino UNO)


(ถ้าไม่พบให้ทำการติดตั้ง
ไดร์เวอร์ก่อน) 
การติดตั้งโปรแกรม Arduino (IDE) และ การติดตั้งไดร์เวอร์



กดปุ่ม 
 เพื่ออัพโหลด


หากสามารถอัพโหลดโปรแกรมลงบอร์ดได้สำเร็จ จะแสดงคำว่า Done uploading. ที่แถบด้านล่าง


หลังจากนั้นจึงเชื่อมต่อสาย TX และ RX เข้ากับ L298P ดังเดิม





4. ติดตั้งแอพควบคุม


 เปิด  เพาเวอร์สวิตซ์ จ่ายไฟให้กับหุ่นยนต์

 ใช้ สมาร์ทโฟน Android  เปิดการเชื่อมต่อ บลูทูธ และ เชื่อมต่อกับ HC-06





ใส่รหัสผ่าน 1234 -> ตกลง



ใช้สมาร์ทโฟน Android ไปที่  Play Store

ค้นหา arduino bluetooth rc car เลือกแอพ ซ้ายมือด้านบนสุด ตามรูป





การติดตั้ง..เหมือนแอพทั่วๆไป

เมื่อเปิดขึ้นมา รูปวงกลมซ้ายมือ จะเป็นสีแดง


คลิกที่ ไอคอนเฟือง ขวามือสุด แล้ว เลือก Connect to car


เลือก HC-06 (ถ้าถามหา พาสเวิร์ด ให้คีย์ 1234)



สังเกตุ รูปวงกลมซ้ายมือเป็นสีเขียวแสดงว่า โปรแกรมสามารถใช้งานได้แล้ว





5. ทดสอบการทำงาน 

ใสถ่าน แบบ 18650 แรงดันไฟเฉลี่ย 3.7V  (3400 mAh)  จำนวน 2 ก้อน และ เปิด  เพาเวอร์สวิตซ์ จ่ายไฟให้กับหุ่นยนต์

เปิดแอพ Bluetooth RC Controller ทำตามขั้นตอนที่ 4 ให้  รูปวงกลมซ้ายมือเป็นสีเขียว



และสังเกต ไฟ LED สีแดง ของ Bluetooth HC-06 ที่กระพริบอยู่ จะ ติดค้างเป็นสีแดงตลอด แสดงว่าหุ่นยนต์พร้อมทำงานแล้วจึงทดลองควบคุมหุ่นยนต์ 




วีดีโอผลลัพธ์ โปรเจคหุ่นยนต์ UNO + L298P บังคับด้วย Bluetooth


วันอาทิตย์ที่ 5 พฤศจิกายน พ.ศ. 2560

Code โปรเจค หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion


โปรเจคนี้จะเป็นการใช้ Keyestudio PIR Motion Sensor + Arduino UNO มาทำเป็น หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion

Keyestudio PIR Motion Sensor เป็นโมดูลตรวจจับความเคลื่อนไหว ตัวโมดูลผลิตจาก Keyestudio ที่มีความเสถียรภาพในการตรวจจับ และมีประสิทธิ์ภาพในการทำงาน สามารถใช้งานได้ที่แรงดันไฟฟ้า 3.3 - 5V ให้กระแสเอาต์พุตที่ขา S (ขาสถานะเอาต์พุต) ที่ 100mA ทำงานแบบ Active HIGH มาพร้อมรูยึดน๊อตขนาด M3 จำนวน 1 ช่องกลางโมดูล ซึ่งเหมาะสำหรับมาทำเป็นหุ่นยนต์


โปรเจค หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion 
อุปกรณ์ที่ต้องใช้ก็คือ

     1. 2WD Smart Robot Car Chassis Kits

     2. Arduino UNO R3 - Made in italy

     3. Arduino Sensor Shield V5.0

     4. Motor Drive Module L298N

     5. สาย Jumper Female to Male ยาว 20cm.

     6. สาย Jumper Female to Female ยาว 20cm.

     7. รางถ่านแบบ 18650 ใส่ถ่าน 2 ก้อน

     8. แบตเตอรี่ลิเธียม 18650  จำนวน 2 ก้อน

     9. เสารองแผ่นพีซีบีแบบโลหะ ยาว 25 มม.

     10. สกรูหัวกลมน็อตตัวเมีย ขนาด 3มม ยาว12มม. จำนวน 2 ถุง
 
     11. สายไฟแดงดำ ขนาด 22AWG มาตรฐาน

    12. Keyestudio PIR Motion Sensor จำนวน 3 ชิ้น

    13. Mounting Bracket for HC-SR04 แบบสั้น จำนวน 3 ชิ้น


เริ่มต้นด้วยการ ประกอบ 2WD Smart Car Robot Chassis Kits



และประกอบ รางถ่านแบบ 18650 และ เชื่อมต่อ Motor Drive Module L298N  เข้ากับ Arduino Sensor Shield V 5.0 วงจรตามรูป


หมายเหตุ : ถ้ามี Jumper อยู่ที่ขา ENA และ ENB ของ บอร์ด L298N  ให้ถอดออก



การเชื่อมต่อระหว่าง บอร์ด Arduino Sensor Shield V5.0 กับ บอร์ด L298N  


*** VCC ของ  Sensor Shield V5.0   คือ  V ***



ประกอบ 2WD Smart Car Robot Chassis Kits และประกอบ รางถ่านแบบ 18650 และ เชื่อมต่อ 5V และ GND ของ Motor Drive Module L298N  เข้ากับ V และ G ของ Arduino Sensor Shield 






เชื่อมต่อการควบคุมมอเตอร์ ของ Motor Drive Module L298N  เข้ากับ  Arduino Sensor Shield






หลังจากนั้นให้ทดสอบเบื้องต้น ว่าการหมุนของล้อถูกต้องหรือไม่ โดย


เปิดโปรแกรม Arduino (IDE) และ Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3



// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 = backward)
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 = backward)
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

//This lets you run the loop a single time for testing
boolean run = true;

void setup() {
 pinMode(enableA, OUTPUT);
 pinMode(pinA1, OUTPUT);
 pinMode(pinA2, OUTPUT);

 pinMode(enableB, OUTPUT);
 pinMode(pinB1, OUTPUT);
 pinMode(pinB2, OUTPUT);
}
void loop() {
  if (run) {
    delay(2000);
    enableMotors();
    //Go forward
    forward(400);
    //Go backward
    backward(400);
    //Turn left
    turnLeft(400);
    coast(200);
    //Turn right
    turnRight(400);
    coast(200);
    //This stops the loop
    run = false; 
  }
}

//Define high-level H-bridge commands

void enableMotors()
{
 motorAOn();
 motorBOn();
}

void disableMotors()
{
 motorAOff();
 motorBOff();
}

void forward(int time)
{
 motorAForward();
 motorBForward();
 delay(time);
}

void backward(int time)
{
 motorABackward();
 motorBBackward();
 delay(time);
}

void turnLeft(int time)
{
 motorABackward();
 motorBForward();
 delay(time);
}

void turnRight(int time)
{
 motorAForward();
 motorBBackward();
 delay(time);
}

void coast(int time)
{
 motorACoast();
 motorBCoast();
 delay(time);
}

void brake(int time)
{
 motorABrake();
 motorBBrake();
 delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
 digitalWrite(enableA, HIGH);
}

void motorBOn()
{
 digitalWrite(enableB, HIGH);
}

 //disable motors
void motorAOff()
{
 digitalWrite(enableB, LOW);
}

void motorBOff()
{
 digitalWrite(enableA, LOW);
}

 //motor A controls
void motorAForward()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, LOW);
}

void motorABackward()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, LOW);
}

void motorABrake()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, HIGH);
}



ใส่ แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน ไปที่ รางถ่าน และ ตรวจสอบขั้วของแบตเตอรี่ ใส่ถุกต้องหรือไม่


โปรแกรมนี้จะทำงานเพียง 1 ครั้ง ถ้าต้องการทดลองใหม่ให้ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ เมื่อล้อหมุน ตรวจสอบการหมุน ขอล้อต่างๆถูกต้องหรือไม่ ถ้าต่อวงจรถูกต้อง ล้อ ทั้งสองข้างจะหมุนไปข้างหน้า 1ครั้ง กลับหลัง 1 ครั้ง และ เดินหน้าอีกหนึ่งครั้งแล้วจึงหยุด ถ้าไม่ถูกต้องให้แก้ไข เช่นการต่อขั้วของมอเตอร์ผิด เป็นต้น

ถ้าทุกอย่างถูกต้อง ทดลอง ยกลงวางที่พื้นแล้วทดสอบ อีกครั้ง ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ ถ้าทุกอย่างถูกต้อง รถจะเคลื่อนที่ไปข้างหน้า-ถอยหลัง แล้ว เลี้ยวซ้าย แล้ว จึงกลับสู่ตำแหน่งเดิม


ถ้าหุ่นยนต์วิ่งได้ถูกต้องแล้ว จึง  Upload โค้ด หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion


/*
   by : RobotSiam.com
*/

// Motor A 
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

//Motor B pins 
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

byte sensor[3] = {0, 0, 0};


void setup()
{
  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);


}

void loop()
{

  enableMotors();
  analogWrite(enableA, 200);  // ปรับค่าความเร็วของหุ่นยนต์
  analogWrite(enableB, 200);  // ปรับค่าความเร็วของหุ่นยนต์
  delay(700);

  sensor[0] = digitalRead(A3);
  sensor[1] = digitalRead(A4);
  sensor[2] = digitalRead(A5);


  if ((sensor[0] == 1) && (sensor[1] == 1) && (sensor[2] == 1) )
  {
    forward(100);
    coast(10);
  }
  else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 0) )
  {
    forward(180);
    coast(10);
  }
  else if ((sensor[0] == 0) && (sensor[1] == 0) && (sensor[2] == 1) )
  {
    turnRight(100);
    coast(10);
  }
  else if ((sensor[0] == 0) && (sensor[1] == 1) && (sensor[2] == 1) )
  {
    turnRight(50);
    coast(10);
  }
  else if ((sensor[0] == 1) && (sensor[1] == 0) && (sensor[2] == 0)  )
  {
    turnLeft(100);
    coast(10);
  }

  else if ((sensor[0] == 1) && (sensor[1] == 1) && (sensor[2] == 0)  )
  {
    turnLeft(50);
    coast(10);

  }
  else
  {
    forward(100);
    coast(10);
  }

}

//Define high-level H-bridge commands

void enableMotors()
{
  motorAOn();
  motorBOn();
}

void disableMotors()
{
  motorAOff();
  motorBOff();
}

void forward(int time)
{
  motorAForward();
  motorBForward();
  delay(time);
}

void backward(int time)
{
  motorABackward();
  motorBBackward();
  delay(time);
}

void turnLeft(int time)
{
  motorABackward();
  motorBForward();
  delay(time);
}

void turnRight(int time)
{
  motorAForward();
  motorBBackward();
  delay(time);
}

void coast(int time)
{
  motorACoast();
  motorBCoast();
  delay(time);
}

void brake(int time)
{
  motorABrake();
  motorBBrake();
  delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
  digitalWrite(enableA, HIGH);
}

void motorBOn()
{
  digitalWrite(enableB, HIGH);
}

//disable motors
void motorAOff()
{
  digitalWrite(enableB, LOW);
}

void motorBOff()
{
  digitalWrite(enableA, LOW);
}

//motor A controls
void motorAForward()
{
  digitalWrite(pinA1, HIGH);
  digitalWrite(pinA2, LOW);
}

void motorABackward()
{
  digitalWrite(pinA1, LOW);
  digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
  digitalWrite(pinB1, HIGH);
  digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
  digitalWrite(pinB1, LOW);
  digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
  digitalWrite(pinA1, LOW);
  digitalWrite(pinA2, LOW);
}

void motorABrake()
{
  digitalWrite(pinA1, HIGH);
  digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
  digitalWrite(pinB1, LOW);
  digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
  digitalWrite(pinB1, HIGH);
  digitalWrite(pinB2, HIGH);

}




จากนั้นประกอบ Keyestudio PIR Motion Sensor เข้ากับตัวหุ่นยนต์


Motion Sensor -> 
Arduino Sensor Shield 



- 
(ทั้ง3 ตัว) <-> G

+ (ทั้ง3 ตัว)<-> S


S (ตำแหน่งซ้าย) <-> A3


S (ตำแหน่งกลาง)<-> A4

S (ตำแหน่งขวา) <-> A5







ภาพรวมการต่อ หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion





จากนั้นทดสอบ หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion

วีดีโอผลลัพธ์การทำงานของ โปรเจค หุ่นยนต์ติดตามความเคลื่อนไหว PIR Motion


Code โปรเจค Arduino เปิดปิดไฟ ด้วย Wireless Joystick

PS2 Joystick Playstation Adapter for Arduino อะแดปเตอร์แปลงหัว PS2 เป็นขาต่อแบบ DIP สำหรับ Arduino คอนเนคเตอร์สำหรับแปลงจาก PS2 เป็นขา DI...