typestar

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

  1. analogRead(SOIL_PIN) samples the 10-bit converter.
  2. counts * (5.0 / 1023.0) rescales to the 5 V range.
  3. Chained print calls compose one labeled line.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Analog, step 7 of 27 in Language basics.

← Previous Next →