Created
May 19, 2019 12:31
-
-
Save alistairjevans/062d1334ec052a93004c7fd9d62602f8 to your computer and use it in GitHub Desktop.
Reading an analog voltage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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