Essentials

Started by tha, November 13, 2023, 09:48:47 AM

Previous topic - Next topic

tha

https://docs.arduino.cc/built-in-examples/basics/AnalogReadSerial

Analog Read Serial

อ่าน a potentiometer, พิมพ์สถานะของมันออกไปยัง the Arduino Serial Monitor.

LAST REVISION: 31/10/2566 21:11

ตัวอย่างนี้แสดงให้คุณเห็นวิธีการอ่าน analog input จากโลกทางกายภาพโดยการใช้  a potentiometer. A potentiometer เป็น a simple mechanical device ที่จัดให้มีความเปลี่ยนแปลงกันในหมู่ของ resistance เมื่อแกนของมันถูกหมุน. ด้วยการส่งแรงดันไฟฟ้าผ่าน a potentiometer และเข้าสู่ an analog input บน board ของคุณ, คุณสามารถวัดปริมาณความต้านทานที่เกิดจาก a potentiometer (หรือเรียกสั้น ๆ ว่าพ็อท) เป็น an analog value. ในตัวอย่างนี้ คุณจะเฝ้าดูสถานะของ potentiometer ของคุณหลังจากสร้าง serial communication ระหว่าง Arduino ของคุณและ computer ของคุณที่ใช้ the Arduino Software (IDE).

tha

Hardware Required

  •  Arduino Board
  •  10k ohm Potentiometer

Circuit

ต่อสายไฟสามเส้นจากโพเทนชิออมิเตอร์เข้ากับบอร์ดของคุณ อันแรกไปจากพินด้านนอกตัวใดตัวหนึ่งของโพเทนชิออมิเตอร์ไปที่กราวด์ ส่วนที่สองต่อจากพินด้านนอกอีกอันของโพเทนชิออมิเตอร์ไปยัง 5 โวลต์ สายที่สามไปจากพินกลางของโพเทนชิออมิเตอร์ไปยัง the analog pin A0.



ด้วยการหมุนแกนของโพเทนชิออมิเตอร์ คุณจะเปลี่ยนปริมาณความต้านทานที่ด้านใดด้านหนึ่งของ the wiper, ซึ่งถูกเชื่อมต่อกับ the center pin ของโพเทนชิออมิเตอร์ สิ่งนี้จะเปลี่ยนแรงดันไฟฟ้าที่ the center pin. เมื่อความต้านทานระหว่าง the center และด้านข้างที่เชื่อมต่อกับ 5 โวลต์ใกล้กับศูนย์ (และความต้านทานอีกด้านหนึ่งใกล้กับ 10k โอห์ม) แรงดันไฟฟ้าที่ the center pin จะเข้าใกล้ 5 โวลต์ เมื่อความต้านทานหมุนกลับ แรงดันไฟฟ้าที่ the center pin จะใกล้ 0 โวลต์หรือกราวด์ แรงดันไฟฟ้านี้คือแรงดัน the analog voltage ที่คุณกำลังอ่านเป็น an input.

บอร์ด Arduino มีวงจรอยู่ภายในที่เรียกว่า an analog-to-digital converter หรือ ADC ซึ่งจะอ่านแรงดันไฟฟ้าที่เปลี่ยนแปลงนี้และแปลงมันเป็นตัวเลขระหว่าง 0 ถึง 1,023 เมื่อเพลาถูกหมุนไปจนสุดในทิศทางเดียว จะมีแรงดันไฟฟ้า 0 โวลต์ ไปที่พิน โดยค่าอินพุตเป็น 0 เมื่อหมุนเพลาไปในทิศทางตรงกันข้ามจนสุด จะมีไฟ 5 โวลต์ไปที่พิน และค่าอินพุตเป็น 1023 ในระหว่างนั้น analogRead() จะส่งคืนตัวเลขระหว่าง 0 และ 1,023 ที่เป็นสัดส่วนกับปริมาณแรงดันไฟฟ้าที่กำลังปรากฎกับพิน

Schematic


tha

Code

ใน the sketch ข้างล่าง, สิ่งเดียวที่คุณทำใน the setup function คือเริ่มต้น serial communications, ที่ 9600 bits of data ต่อ second, ระหว่าง your board และ your computer ด้วยคำสั่ง:

Serial.begin(9600);

ถัดไป, ใน the main loop ของ your code, คุณจำเป็นต้องสร้างตัวแปรเพื่อเก็บ the resistance value (ซึ่งจะอยู่ระหว่าง 0 และ 1023, เหมาะสมคือ an int datatype) ที่มาจาก your potentiometer:

int sensorValue = analogRead(A0);

สุดท้ายนี้ คุณจำเป็นต้องพิมพ์ข้อมูลนี้ไปยัง your serial monitor window. คุณสามารถทำสิ่งนี้ด้วยคำสั่ง Serial.println() ในโค้ดบรรทัดสุดท้าย:

Serial.println(sensorValue);

ตอนนี้ เมื่อคุณเปิด your Serial Monitor ใน the Arduino Software (IDE) (โดยการคลิ๊ก the icon ที่ดูเหมือน a lens, บนด้านขวา, ใน the green top bar หรือโดยใช้ the keyboard shortcut Ctrl+Shift+M), คุณจะเห็น ตัวเลขที่ไหลต่อเนื่องกันช่วงจาก 0-1023 ซึ่งสัมพันธ์กับตำแหน่งของ the pot. เมื่อคุณหมุนโพเทนชิออมิเตอร์ ตัวเลขเหล่านี้จะตอบสนองเกือบจะในทันที

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);  // delay in between reads for stability
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.


tha

https://docs.arduino.cc/built-in-examples/basics/BareMinimum

Bare Minimum code needed

The bare minimum ของโค้ดที่จำเป็นเพื่อสตาร์ท an Arduino sketch.

LAST REVISION: 31/10/2566 21:11

ตัวอย่างนี้มีโค้ดขั้นต่ำสุดที่คุณต้องการสำหรับ a sketch เพื่อคอมไพล์อย่างถูกต้องบน Arduino Software (IDE): the setup() method และ the loop() method.

Hardware Required

  •  Arduino Board

Circuit

มีเพียง Arduino Board ของคุณถูกต้องการสำหรัตัวอย่างนี้.



Code

The setup() function ถูกเรียกเมื่อ a sketch สตาร์ท. ใช้มันเพื่อเริ่มต้น variables, pin modes, สตาร์ทการใช้ libraries, ฯลฯ. The setup function จะรันเพียงครั้งเดียวเท่านั้น, หลังจากแต่ละ powerup หรือการรีเซ็ตของ the board.

หลังจากสร้าง a setup() function แล้ว, the loop() function จะดำเนินการอย่างเที่ยงตรงตามชื่อของมันแนะนำ, และวนซ้ำอย่างต่อเนื่อง, เพื่อให้โปรแกรมของคุณเปลี่ยนแปลงและตอบสนองในขณะที่มันทำงาน. Code ใน the loop() section ของ sketch ของคุณถูกใช้เพื่อควบคุมบอร์ดอย่างแข็งขัน.

โค้ดด้านล่างนี้ไม่ได้ทำอะไรเลยจริงๆ แต่มีโครงสร้างของโค้ดที่มีประโยชน์สำหรับการคัดลอกและวางเพื่อให้คุณเริ่ม sketch ของคุณเองได้ นอกจากนี้ยังแสดงวิธี comments ในโค้ดของคุณอีกด้วย

บรรทัดใดๆ ที่ขึ้นต้นด้วยเครื่องหมายทับ 2 ตัว (//) จะไม่ถูกอ่านโดยคอมไพลเลอร์ ดังนั้นคุณจึงสามารถเขียนสิ่งที่คุณต้องการหลังจากนั้นได้ The two slashes อาจใส่หลัง functional code เพื่อเก็บความคิดเห็นไว้ในบรรทัดเดียวกัน การแสดงความคิดเห็นโค้ดของคุณในลักษณะนี้อาจมีประโยชน์อย่างยิ่งในการอธิบายทั้งตัวคุณเองและผู้อื่นว่าโปรแกรมของคุณทำงานอย่างไรทีละขั้นตอน

void setup() {
  // put your setup code here, to run once:
}

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

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/28 by SM

tha

https://docs.arduino.cc/built-in-examples/basics/Blink

Blink

เปิดและปิด LED ทุกวินาที.

LAST REVISION: 31/10/2566 21:11

ตัวอย่างนี้แสดงสิ่งที่ง่ายที่สุดที่คุณสามารถทำได้กับ Arduino เพื่อดูเอาท์พุตทางกายภาพ นั่นคือ การกะพริบไฟ LED บนบอร์ด

Hardware Required

  •  Arduino Board

ให้เลือกได้

  •  LED
  •  220 ohm resistor

Circuit

ตัวอย่างนี้ใช้ LED ในตัวที่บอร์ด Arduino ส่วนใหญ่มี LED นี้เชื่อมต่อกับ a digital pin และหมายเลขอาจแตกต่างกันไปตามประเภทของบอร์ดแต่ละประเภท เพื่อให้ชีวิตของคุณง่ายขึ้น เรามีค่าคงที่ที่ระบุไว้ใน board descriptor file ทุกไฟล์ ค่าคงที่นี้คือ LED_BUILTIN และช่วยให้คุณควบคุม the built-in LED ได้อย่างง่ายดาย นี่คือความสอดคล้องระหว่างค่าคงที่กับพินดิจิทัล

  •  D13 - 101
  •  D13 - Due
  •  D1 - Gemma
  •  D13 - Intel Edison
  •  D13 - Intel Galileo Gen2
  •  D13 - Leonardo and Micro
  •  D13 - LilyPad
  •  D13 - LilyPad USB
  •  D13 - MEGA2560
  •  D13 - Mini
  •  D6 - MKR1000
  •  D13 - Nano
  •  D13 - Pro
  •  D13 - Pro Mini
  •  D13 - UNO
  •  D13 - Yún
  •  D13 - Zero

หากคุณต้องการให้ไฟ LED ภายนอกสว่างขึ้นด้วย sketch นี้ คุณจะต้องสร้างวงจรนี้ โดยที่คุณเชื่อมต่อปลายด้านหนึ่งของตัวต้านทานเข้ากับ the digital pin ที่ตรงกันกับค่าคงที่ LED_BUILTIN เชื่อมต่อขายาวของ LED (ขาบวกเรียกว่า the anode) เข้ากับปลายอีกด้านของตัวต้านทาน เชื่อมต่อขาสั้นของ LED (ขาลบเรียกว่าแคโทด) เข้ากับ GND ในแผนภาพด้านล่าง เราแสดงบอร์ด UNO ที่มี D13 เป็น the LED_BUILTIN value.

ค่าของตัวต้านทานต่ออนุกรมกับ LED อาจมีค่าแตกต่างจาก 220 โอห์ม ไฟ LED จะสว่างขึ้นด้วยค่าสูงถึง 1K โอห์ม



Schematic


tha

Code

หลังจากที่คุณสร้างวงจรแล้ว ให้เสียบบอร์ด Arduino เข้ากับคอมพิวเตอร์ของคุณ ให้เริ่มซอฟต์แวร์ Arduino (IDE) แล้วป้อนโค้ดด้านล่าง คุณยังอาจโหลดมันได้จากเมนู File/Examples/01.Basics/Blink สิ่งแรกที่คุณทำคือเริ่มต้นพิน LED_BUILTIN เป็นพินเอาท์พุตด้วยบรรทัดนี้

pinMode(LED_BUILTIN, OUTPUT);

ใน the main loop, คุณเปิดไฟ the LED ด้วยบรรทัดนี้:

digitalWrite(LED_BUILTIN, HIGH);

นี้จะจ่ายไฟ 5 volts ให้กับ the LED anode. ซึ่งสร้างความต่างแรงดันไฟฟ้าคร่อมพินของ LED และสว่างขึ้น จากนั้นคุณปิดมันด้วยบรรทัด:

digitalWrite(LED_BUILTIN, LOW);

นั่นจะนำพิน LED_BUILTIN กลับไปที่ 0 โวลต์และปิด LED ในระหว่างการเปิดและปิด คุณต้องการเวลาที่เพียงพอให้บุคคลเห็นการเปลี่ยนแปลง ดังนั้นคำสั่ง delay() จะบอกให้บอร์ดไม่ต้องทำอะไรเลยเป็นเวลา 1,000 มิลลิวินาทีหรือหนึ่งวินาที เมื่อคุณใช้คำสั่ง delay() จะไม่มีอะไรเกิดขึ้นอีกในช่วงเวลาดังกล่าว เมื่อคุณเข้าใจตัวอย่างพื้นฐานแล้ว ลองดูตัวอย่าง BlinkWithoutDelay เพื่อเรียนรู้วิธีสร้าง a delay ขณะทำสิ่งอื่น

เมื่อคุณเข้าใจตัวอย่างนี้แล้ว ลองดูตัวอย่าง DigitalReadSerial เพื่อเรียนรู้วิธีอ่าน a switch ที่เชื่อมต่อกับบอร์ด

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000);                      // wait for a second
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/28 by SM

tha

https://docs.arduino.cc/built-in-examples/basics/DigitalReadSerial

Digital Read Serial

อ่าน a switch, พิมพ์สถานะออกไปยัง the Arduino Serial Monitor.

LAST REVISION: 31/10/2566 21:11

ตัวอย่างนี้แสดงให้คุณเห็นวิธีเฝ้าดูสถานะของ a switch โดยการสร้าง serial communication ระหว่าง your Arduino และ your computer ผ่าน USB.

Hardware Required

  •  Arduino Board
  •  A momentary switch, button, or toggle switch
  •  10k ohm resistor
  •  hook-up wires
  •  breadboard

Circuit



เชื่อมต่อสายไฟสามเส้นเข้ากับบอร์ด สายสองเส้นแรก สีแดงและสีดำ เชื่อมต่อกับแถวแนวตั้งยาวสองแถวที่ด้านข้างของ the breadboard เพื่อจัดให้เข้าถึงแหล่งจ่ายไฟ 5V และกราวด์ ได้ สายที่สามต่อจาก digital pin 2 ไปยังขาข้างหนึ่งของปุ่มกด ขาเดียวกันของ the button นั้นเชื่อมต่อผ่าน a pull-down resistor (ที่นี่ 10k โอห์ม) ลงกราวด์ ขาอีกข้างของ the button เชื่อมต่อกับแหล่งจ่ายไฟ 5 โวลต์

Pushbuttons หรือ switches จะเชื่อมต่อจุดสองจุดในวงจรเมื่อคุณกดมัน เมื่อ the pushbutton เปิดอยู่ (ไม่ได้กด) จะไม่มีการเชื่อมต่อระหว่างขาทั้งสองข้างของ the pushbutton, ดังนั้น  the pin จึงเชื่อมต่อกับกราวด์ (ผ่าน  the pull-down resistor) และอ่านเป็น LOW หรือ 0. เมื่อ the button ถูกปิด (กด ) มันจะทำการเชื่อมต่อระหว่างขาทั้งสองข้างของมันเชื่อมต่อ the pin เข้ากับไฟ 5 โวลต์ ดังนั้น the pin อ่านเป็น HIGH หรือ 1

หากคุณถอด the digital i/o pin ออกจากทุกสิ่ง การอ่านค่าของมันอาจเปลี่ยนแปลงไปอย่างผิดปกติ เนื่องจาก the input เป็น "การลอย"  กล่าวคือ มันไม่มีการเชื่อมต่อที่มั่นคงกับแรงดันไฟฟ้าหรือกราวด์ และมันจะส่งคืนค่าอย่างใดอย่างหนึ่ง HIGH หรือ LOW แบบสุ่ม นั่นเป็นสาเหตุว่าทำไมคุณต้องมี a pull-down resistor ในวงจร

Schematic


tha

Code

ในโปรแกรมด้านล่าง สิ่งแรกสุดที่คุณทำใน the setup function คือเริ่ม serial communications, ที่ข้อมูล 9600 บิตต่อวินาที ระหว่างบอร์ดและคอมพิวเตอร์ของคุณด้วยบรรทัด:

Serial.begin(9600);

จากนั้น เริ่มต้น digital pin 2, the pin ที่จะอ่านเอาต์พุตจาก button ของคุณ, เป็น an input:

pinMode(2,INPUT);

เมื่อการตั้งค่าของคุณเสร็จสมบูรณ์แล้ว ให้ย้ายไปยัง the main loop ของโค้ดของคุณ เมื่อ button ของคุณถูกกด ไฟ 5 โวลต์จะไหลผ่านวงจรของคุณอย่างอิสระ และเมื่อไม่ได้กดปุ่ม the input pin จะเชื่อมต่อกับกราวด์ผ่านตัวต้านทาน 10k โอห์ม นี่คือ a digital input, ซึ่งหมายความว่าสวิตช์สามารถอยู่ในสถานะอย่างใดอย่างหนึ่งเท่านั้น an on state (ถูกเห็นโดย your Arduino เป็น a "1", หรือ HIGH) หรือ an off state (Arduino ของคุณเห็นเป็น "0" หรือ LOW) โดยไม่มีอะไรอยู่ระหว่างนั้น

สิ่งแรกที่คุณต้องทำใน the main loop ของโปรแกรมของคุณคือสร้างตัวแปรเพื่อเก็บข้อมูลที่เข้ามาจากสวิตช์ของคุณ เนื่องจากข้อมูลที่เข้ามาจากสวิตช์จะเป็นอย่างใดอย่างหนึ่ง "1" หรือ "0" คุณสามารถใช้ an intdatatype เรียกตัวแปรนี้ว่า sensorValue นี้ และตั้งค่ามันให้เท่ากับสิ่งที่อ่านได้บน digital pin 2. คุณสามารถทำทั้งหมดนี้ให้สำเร็จได้ด้วยโค้ดเพียงบรรทัดเดียว:

int sensorValue = digitalRead(2);

เมื่อบอร์ดอ่าน the input แล้ว ทำให้มันพิมพ์ข้อมูลนี้กลับไปที่คอมพิวเตอร์เป็น a decimal value. คุณสามารถดำเนินการนี้ได้ด้วยคำสั่ง Serial.println() ในโค้ดบรรทัดสุดท้ายของเรา:

Serial.println(sensorValue);

ตอนนี้ เมื่อคุณเปิด your Serial Monitor ใน the Arduino Software (IDE), คุณจะเห็นการไหลของ "0" หากสวิตช์ของคุณถูกเปิด หรือ "1" หากสวิตช์ของคุณถูกปิด.

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);  // delay in between reads for stability
}

Learn more

คุณสามารถค้นหาบทช่วยสอนพื้นฐานเพิ่มเติมได้ใน the built-in examples section.

คุณยังสามารถสำรวจ the language reference, ซึ่งเป็นคอลเล็กชันรายละเอียดของ the Arduino programming language.

Last revision 2015/07/29 by SM