Lesson 7: Analog input
Trang này cần trình duyệt gần đây (hỗ trợ SharedArrayBuffer). Vui lòng cập nhật Chrome, Edge, Firefox hoặc Safari lên phiên bản mới nhất.
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);
}
}
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read A0 and print the voltage with 2 decimals, like Voltage: 2.50, every 500 ms. The voltage is value * 5.0 / 1023.0.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
A light sensor is on A0. Switch the LED on pin 13 ON when the reading is below 300 (dark) and OFF otherwise.
Nhấn Chạy để xem kết quả ở đây.