ปุ่มหมุนควบคุมความสว่าง LED
ผสานค่าที่อ่านจากโพเทนชิโอมิเตอร์เข้ากับเอาต์พุต PWM
ปุ่มหมุนควบคุมความสว่าง LED เป็นบทเรียน Arduino & IoT Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Arduino & IoT Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Let a Knob Do the Driving
Now you combine two skills: read a potentiometer and use that reading to set an LED's brightness in real time. 🎛️
The Knob Speaks 0 to 1023
analogRead gives you a 0 to 1023 value as you turn the pot. That is the position of the knob expressed as a number.
int knob = analogRead(A0);The LED Wants 0 to 255
But analogWrite only accepts 0 to 255. The two ranges do not match, so you cannot feed the knob value straight in.
map() Bridges the Gap
The map function rescales a number from one range to another. It turns the wide knob range into the narrow LED range.
int led = map(knob, 0, 1023, 0, 255);Send It to the LED
Pass the mapped value to analogWrite. Now the LED brightness tracks exactly where you set the knob.
analogWrite(9, led);Wire the Potentiometer
Connect the pot's outer legs to 5V and GND, and its middle wiper leg to analog pin A0 so the board can read it.
Pin Setup in setup()
The LED pin still needs OUTPUT. Analog input pins do not need pinMode, so setup stays short and simple.
void setup() {
pinMode(9, OUTPUT);
}The Full loop()
Each pass, read the knob, map it, and write it. Because loop repeats fast, the LED feels instant as you turn the dial.
void loop() {
int knob = analogRead(A0);
int led = map(knob, 0, 1023, 0, 255);
analogWrite(9, led);
}Skip map With a Shortcut
You could also divide the knob value by 4, since 1023 over 4 is about 255. It works, but map reads more clearly.
int led = knob / 4;Watch the Direction
If the LED dims when you turn up, just swap map's output ends to 255 then 0, or flip the pot's outer wires.
You Just Built an Interface
This read, map, write pattern is everywhere in IoT: a sensor in, a useful range out, an action on the hardware. 🚀
Quick Check
Recall why map matters in this project.
Recap: Knob to Brightness
Read the knob with analogRead, map 0 to 1023 into 0 to 255, then analogWrite it. You built a real dimmer. 🎉
คำถามที่พบบ่อย
บทเรียน “ปุ่มหมุนควบคุมความสว่าง LED” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ปุ่มหมุนควบคุมความสว่าง LED” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Arduino & IoT Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Arduino & IoT Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ปุ่มหมุนควบคุมความสว่าง LED”
ผสานค่าที่อ่านจากโพเทนชิโอมิเตอร์เข้ากับเอาต์พุต PWM คุณปฏิบัติ Arduino & IoT Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Arduino & IoT Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Arduino & IoT Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ปุ่มหมุนควบคุมความสว่าง LED” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Arduino & IoT Academy นี้ได้ไหม
ได้ บทเรียน Arduino & IoT Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- PWM ทำอะไรจริง ๆ
- analogWrite 0–255
- ค่อย ๆ เพิ่มและลดแสง LED
- ปุ่มหมุนควบคุมความสว่าง LED