Lesson 7: Analog input
Cette page nécessite un navigateur récent (avec support SharedArrayBuffer). Veuillez mettre à jour Chrome, Edge, Firefox ou Safari vers la dernière version.
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);
}
}
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Read A0 and print the voltage with 2 decimals, like Voltage: 2.50, every 500 ms. The voltage is value * 5.0 / 1023.0.
Cliquez sur Exécuter pour voir le résultat ici.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
A light sensor is on A0. Switch the LED on pin 13 ON when the reading is below 300 (dark) and OFF otherwise.
Cliquez sur Exécuter pour voir le résultat ici.