0Pricing
Arduino & IoT Academy · บทเรียน

เซอร์โวควบคุมด้วยปุ่มหมุน

บังคับเซอร์โวด้วยโพเทนชิโอมิเตอร์

เซอร์โวควบคุมด้วยปุ่มหมุน เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Steer with a Knob

Time to take control by hand. Turn a potentiometer and the servo follows in real time, like a tiny steering wheel. 🎛️

The Plan

The idea is simple: read the knob, convert that number into an angle, and write it to the servo, over and over inside loop().

Read the Knob

Use analogRead() on the pot's pin. It gives back a number from 0 to 1023 that follows how far you've turned the knob.

int val = analogRead(A0);

The Range Mismatch

The knob speaks 0 to 1023, but the servo only understands 0 to 180. You need to convert between those two scales.

map() to the Rescue

The map() function rescales a number from one range into another. It's the perfect bridge from knob units to servo degrees.

int angle = map(val, 0, 1023, 0, 180);

Send It to the Servo

Now feed that mapped angle straight into write(). The servo snaps to whatever position your knob is pointing at.

myServo.write(angle);

The Whole Loop

Read, map, write: stack those three lines in loop() and the servo tracks your knob continuously.

void loop() {
  int val = analogRead(A0);
  int angle = map(val, 0, 1023, 0, 180);
  myServo.write(angle);
}

Let It Settle

Add a tiny delay() at the end of the loop. It gives the servo a moment to reach each position and smooths out the motion.

delay(15);

Wire the Pot

The potentiometer's outer pins go to 5V and ground, and the middle wiper pin goes to analog pin A0. That gives a clean sweep of voltage.

Tame the Jitter

Analog readings wobble a little, so the servo may jitter. Reading several times and averaging, or ignoring tiny changes, calms it down.

Flip the Direction

Want the servo to turn the opposite way? Just swap the map output to go from 180 to 0 instead of 0 to 180.

int angle = map(val, 0, 1023, 180, 0);

Quick Check

Let's connect the knob reading to a servo angle.

Recap

Read the pot, map 0 to 1023 into 0 to 180, then write that angle each loop. Now your hand directly steers the servo. 🙌

คำถามที่พบบ่อย

บทเรียน “เซอร์โวควบคุมด้วยปุ่มหมุน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เซอร์โวควบคุมด้วยปุ่มหมุน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เซอร์โวควบคุมด้วยปุ่มหมุน”

บังคับเซอร์โวด้วยโพเทนชิโอมิเตอร์ คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “เซอร์โวควบคุมด้วยปุ่มหมุน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม

ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เซอร์โวรู้มุมของตัวเองได้อย่างไร
  2. ไลบรารีเซอร์โวและ attach
  3. กวาดจาก 0 ถึง 180
  4. เซอร์โวควบคุมด้วยปุ่มหมุน
← กลับไปที่ Arduino & IoT Academy