Skip to content

Instantly share code, notes, and snippets.

@alistairjevans
Created May 19, 2019 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistairjevans/062d1334ec052a93004c7fd9d62602f8 to your computer and use it in GitHub Desktop.
Save alistairjevans/062d1334ec052a93004c7fd9d62602f8 to your computer and use it in GitHub Desktop.
Reading an analog voltage
void setup()
{
// Initialise my serial port with 9600 baud
Serial.begin(9600);
}
void loop()
{
// Analog values are read as integer values between 1 and 1023.
// Each 1 is a fraction of 5V. So 0 = 0V, 1023 = 5V, and the scale
// goes in-between.
const float VOLTAGE_MULTIPLIER = (5.0 / 1023.0);
// Read the analog value on pin A0 (as fractions of 5V).
int sensorValue = analogRead(A0);
// Convert to volts using our multiplier.
float sensorVoltage = sensorValue * VOLTAGE_MULTIPLIER;
// Write the current voltage to the Serial port.
Serial.println(sensorVoltage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment