Lesson 7: Analog input
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
More than on and off
- The pins A0 to A5 can measure a voltage from 0 V to 5 V.
analogRead(A0)gives a number from 0 (0 V) to 1023 (5 V). 2.5 V gives about 512.- A potentiometer (a knob) or a light sensor gives a voltage. On this page, move the slider.
void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(A0);
Serial.println(value);
delay(500);
}
English
A night light
- A light sensor gives a small number in the dark and a big number in bright light.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
if (analogRead(A0) < 300) { // dark
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
}
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read A0 and print the voltage with 2 decimals, like Voltage: 2.50, every 500 ms. The voltage is value * 5.0 / 1023.0.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A light sensor is on A0. Switch the LED on pin 13 ON when the reading is below 300 (dark) and OFF otherwise.
คลิก Run เพื่อดูผลลัพธ์ที่นี่