analogRead in Arduino
0 to 1023 counts, and the arithmetic that makes them volts.
// analogRead returns 0-1023; arithmetic turns counts into volts.
const int SOIL_PIN = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
int counts = analogRead(SOIL_PIN);
float volts = counts * (5.0 / 1023.0);
Serial.print(counts);
Serial.print(" counts = ");
Serial.print(volts);
Serial.println(" V");
delay(500);
}
How it works
analogRead(SOIL_PIN)samples the 10-bit converter.counts * (5.0 / 1023.0)rescales to the 5 V range.- Chained
printcalls compose one labeled line.
Keywords and builtins used here
SerialanalogReadbeginconstdelayfloatintloopprintprintlnsetupvoid
The run, in numbers
- Lines
- 16
- Characters to type
- 324
- Tokens
- 82
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 70 seconds.
Step 1 of 3 in Analog, step 7 of 27 in Language basics.