Deryoung Electronics
         arduino lab note
 
Welcome to ... Guest! Not yet Open Comming Soon
ไทย (Thai) | English
| Login

Play with LED (เล่นกับ LED)



   

การป้อน logic "1" ให้กับ Port input digital-9

สำหรับผู้เริ่มต้น...โปรแกรมข้างล่างนี้เป็นโปรแกรมที่เรียกว่าง่ายที่สุดในการทดสอบการทำงานของ Board ก็ดีหรือจะทดลองการสร้างสัญญาณดิจิตอล "ON","OFF" ในการใช้ควบคุมอุปกรณ์ต่างๆ ทางอิเล็กทรอนิกส์ได้อย่างสะดวกสบาย เนื่องจาก Arduino ได้ทำการปรับปรุงรูปแบบคำสั่งให้มีความกระทัดรัด สามารถเขียนและนำไปใช้งานได้ง่ายกว่า แต่ก็ยังคงความเป็นภาษาซีไว้

//*************************************
//Program:ONled.pde
//Crate by:Deryoung.com
//Resulte: Input Logig "1" to LED
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
void setup()
{
  pinMode(ledPin,OUTPUT);
}

void loop() 
{
  digitalWrite(ledPin,HIGH);
}


สิ่งทีต้องทำก่อน

  • ต้องมี LED ขนาดเล็ก 1 ตัว (สีใดก็ได้) แนะนำให้หา LED ที่ใช้กับไฟตรง (DC) 4.5-5.0 volts มาใช้ เพราะจะสะดวกกับบอร์ด Arduino โดยไม่ต้องต่อตัวต้านทาน เพิ่มเติมอีก หากหาไม่ได้ก็ให้ต่อ ตัวต้านทานขนาด 330-560 โอมห์ อนุกรมกับ LED ก่อน (ระวังหลอด LED เสียหายหากไม่ต่อตัวต้านทานดัีงกล่าว) โดยปกติแล้ว LED ต้องการกระแสเพียง 20mA เท่านั้น
  • เสียบ LED ขา อาโนด (Anode)(ขาที่ยาวที่สุด) เข้ากับขาบอร์ด Arduino Digital-9
  • LED ขา Cathod (K) (ขาที่เหลือ) ต่อเข้ากับ ไฟลบหรือ Ground
  • เปิดโปรแกรม Arduino ให้ copy โปรแกรมข้างต้น และให้ทำการ Save ไว้ในชื่อที่ต้องการ
  • คอมไพล์โปรแกรม การคอมไพล์ กด Ctrl+R
  • อัพโหลด โค๊ดให้แก่บอร์ด Arduino โดยการ กด Cttl+U
  • สังเกตุผลที่ LED

    หลังจาก Run โปรแกรมข้างบนแล้ว
    ผลคือ หลอด LED จะติดตลอด

สิ่งที่ควรหาความรู้เพิ่มเติม


รูปที่ 1. แสดงผลการรันของโปรแกรม ONled.pde

2. "blink" LED at digital 9

ตัวอย่างต่อไปนี้ จะเพิ่มเติมคำสั่ง delay() ลงไปและให้มีการป้อน input logic "HIGH" และ "LOW" สลับกันไปโดย การติด-ดับจะเร็วช้าขึ้นกับ คำสั่ง delay(มิลลิวินาที);

การต่อวงจรให้ใช้การต่อตามข้อ 1. ข้างต้นโดยไม่ต้องเปลี่ยนแปลงอะไรเพิ่มเติม

//*************************************
//Program:blinkLED.pde
//Crate by:Deryoung.com
//Resulte: brinking LED
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
void setup()
{
  pinMode(ledPin,OUTPUT);
}

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

ผลลัพธ์: LED จะติดดับห่างกันด้วยความเร็ว 50 มิลลิวินาที
ทดลองเปลี่ยนค่าความเร็วของคำสั่ง delay ดูแล้วสังเกตุผล

3. Learn Digital Read command   (LED OFF with input Logic "1")

  • หลังจากได้รู้จักคำสั่งในเขียน Port โดยมีการกำหนดให้ port นั้นๆ เป็น port output และป้อนให้เป็นสถานะ Logic = "HIGH" มาแล้ว คราวนี้ลองมารู้จักคำสั่ง digitalRead(pin) กัน
  • เราจะใช้การเชื่อมต่อแบบเดิมตามตัวอย่างข้างต้น แต่จะเพิ่ม Micro Switch แบบกดติด-ปล่อยดับ ลงไปที่ขา digital-7 ของ Arduino Board และอีกขาหนึ่งต่อเข้ากับไฟบวก (+)
  • หลัง run โปรแกรมแล้ว หลอด LED จะติดอยู่ตลอด ให้กด Micro Switch 1 ครั้งแล้วปล่อย ดูผลการทำงานของโปรแกรม
  • ที่เป็นเช่นนี้เพราะ Arduino board (CPU ATmega328)จะทำการตรวจสอบอยู่ตลอดเวลากับ Port digital-7 ว่ามีสถานะใดตามคำสั่งที่เราป้องลงไป และส่งผลให้ตัวแปร val
          val=digitalRead(microSwitch);
  • และเมื่อเรากด SW. ไฟเลีี้ยงขนาด 3.3V หรือ 5.0V ซึ่งก็คือสถานะ HIGH จะถูกป้อนเข้า digital-7, digital-7 ก็จะมีสถานะเป็น HIGH ทำให้เข้าเงื่่อนไขที่เราตั้งไว้ if (val==HIGH) { ให้ digital-9 เท่ากับ LOW } มีผลทำให้ LED ดับลง

 

//*************************************
//Program:offLEDwithSW.pde
//Crate by:Deryoung.com
//Resulte: LED will be "OFF" state when Microswitch is pressed.
//   Note: After press SW. logic "1" assign to digital-7
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
int microSwitch=7;
int val=0;

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

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

สิ่งที่ควรหาความรู้เพิ่มเติม

4. Learn Digital Read command   (LED OFF with input Logic "0")

  • ตัวอย่างนี้จะเหมือนกับตัวอย่างข้อ 3. แต่ต่างกันตรงที่เราจะป้องสัญญาณ logic "LOW" ให้แก่ digital-7
  • ตัวอย่างนี้จะต้องสั่งให้ LED มีสถานะ ON ไว้ก่อน เพราะจุดประสงค์คือต้องการให้ กด Switch แล้ว LED ดับ
  • การกด Switch นี้ จะเป็นการป้อนไฟลบให้แก่ digital-7
//*************************************
//Program:offLEDwithSW2.pde
//Crate by:Deryoung.com
//Resulte: LED will be "OFF" state when Microswitch is pressed.
//   Note: After press SW. logic "0" assign to digital-7
//Interface: LED --> digital-9 and GND.
//*************************************
int ledPin=9;
int microSwitch=7;
int val=0;

void setup()
{
  pinMode(ledPin,OUTPUT);
  pinMode(microSwitch,INPUT);
  digitalWrite(ledPin,HIGH); // defalut LED is "ON"
}

void loop() 
{
  val=digitalRead(microSwitch);
  if(val==LOW){ //<<--- connect digital-7 to GND for OFF LED
    digitalWrite(ledPin,LOW);
  }
  else {
    digitalWrite(ledPin,HIGH);
  }
}

ข้อสังเกตุ:

  1. กำหนดใหใช้ไฟเลี้ยงจาก USB
  2. คอมไพล์โปรแกรมและอัพโหลดลงบอร์ด Arduino
  3. กำหนดให้ ไม่ต้องเสียบสายใดที่ digital-7 และ กราวด์
  4. จากนั้นให้นำสายไฟเปล่า 1 เส้น ให้ปลายข้างหนึ่ง เสียบลงไปที่ digital-7 ก่อน .....สังเกตุการเปลี่ยนแปลงที่หลอด LED
  5. ทดลองตามข้อ 4.ซ้ำ
  6. จากนั้น เสียบปลายอีกข้างหนึ่งลงกราวด์ แล้วสังเกตุผล

5.LED Dimmer

การหรี่หลอดไฟ LED ในตัวอย่างต่อไปนี้ เราจะใช้ digital-9 เป็นพอร์ต Output ต่อกับตัวแสดงผล LED และใช้วอลลุ่ม (variable resistor) รับค่าจาก analog-0 โดยใช้คำสั่ง analogRead(pin) โดยแปลง (map) เป็นเลขค่าหนึ่ง (val) และส่งค่าของเลขดังกล่าวไปให้เขียนลงใน digital-9 ทำให้มีผล LED หรี่ลงและสว่างขึ้นตามการหมุนของวอลลุ่ม

//*************************************
//Program:LEDdimmer.pde
//Crate by:Deryoung.com
//Resulte: LED dimmer using variable Resister.
//Interface: LED --> digital-9 and GND.
//           VR1: Pin middle -->anaglog-0,
//                Pin left --> + 
//                Pin right --> Gnd.
//*************************************

int ledPin=9; //digital-9
int VR_inputPin=0;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop()
{
 int val=analogRead(VR_inputPin);
  val=map(val,0,1023,0,255);
  Serial.println(val);
  analogWrite(ledPin,val);
}


ข้อสังเกตุ:
สำหรับผู้เริ่มเล่น ควรให้ความสนใจกับความสามารถของแต่ละ Port และควรเรียนรู้ว่า Port ใดมีคุณสมบัติอย่างไร เช่น ควรต้องรู้ว่า digital-9,digital-10,digital-11,digital-9,digital-6,digital-5 และ digital-3 เป็น PWM (Pulse Width Modulation) digital output เป็นต้น.

นั่นจะเป็นผลลัพธ์ที่ได้แตกต่างกันกับ Port อื่นๆ ทำให้ผลของมันออกมา สามารถหรี่-เร่งความสว่างของ LED ได้อย่างนุ่มนวลกว่า

val
analogRead(VR_inputPin)
LED
0
.
.
100
.
.
254

0
.
.
500
.
.
1023
dark
.
.
medium
.
.
Highest bright

Figure 5. show dump screen from Serial Monitor

ตำถาม: จะเกิดอะไรขึ้นถ้าเปลี่ยนการเชื่อมต่อ LED จาก digital-9 เป็น digital-12

คำตอบ : ???

6. LDR detect the light

//*************************************
//Program:DetectLight.pde
//Crate by:Deryoung.com
//Resulte: Learning what happening when we pass over the LDR?
//Interface: LED --> digital-9 and GND.
//Important Note: Power positive(+) will be connect to LDR
//                Power negative(-) will be connect o Resistor (8.1K).
//                Midle pin (R and LDR) will be connect to analog-0 pin 
//  and Please open "Serial Monitor" and see value (val) on its.
//*************************************
int ledPin=9; //PWM
int VR_inputPin=0;
int threshold=100; // please try chang here!
void setup()
{
  pinMode(ledPin,OUTPUT);
  Serial.begin(57600);
}

void loop()
{
  int val=analogRead(VR_inputPin);
  val=map(val,0,1023,0,255);
  Serial.println(val);
 
  if(val<threshold) //you can change this value.
  {
     Serial.println("shadow");//can be delete.
     digitalWrite(ledPin,LOW);
     delay(1000); //LEis "off" 1 second
  }
  analogWrite(ledPin,val);
}


Figure 5.1 Simplest LDR circuit for detector.

This program is an example for learning how to detect the light with LDR and show output on 2 ways
1. Output on LED at connect to digital pin 9.
2. See the changing value on "Serial monitor" in Aduino API program.

this program we can change the line
.
.
int threshold=100; // please try chang here!
.
.

Note:
1. We can set threshold value from 0-255 for find the best sentivity. After you upload program you must open the "Serial Monitor" and set speed to 57600. and see the value on the screen that changing.
2.Pass your hand top over the LDR and see value on screen again.
3.Remember a value where your hand over the LDR ,
    bring this value replace value on "int threshold= ???"

4.the good result should display "shadow" when your hand top over the LDR.


val
Environment
107 (max)
.
.
26
day
.
.
night
Figure 5.2 value of "val" with the environment light

volt (out)
Environment
2.0v
.
.
~1.3v
.
.
0.3v
day
.
.
shadow
.
.
night
Figure5.3 value of volt when the shadow pass to LDR
compare between GND and "out" pin.

 

 

This website is personally and all reserved right of meterial, you can copy and use its with refer link to this site
Email contact : support@deryoung.com