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.
点击“运行”查看此处输出。
难度 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.
点击“运行”查看此处输出。