I am trying to use the Arduino Mega 2560 to read feedback from a sensor. Right now I am not using the sensor though, I am just applying a test voltage to analog input A8. To generate the voltage I am jumping from 5V to a breadboard, going through a resistor, and then connecting with a wire back to A8. My resistor values are 220 kOhms and 4.7 MOhms, which generate voltages of 4.04 V and 0.86 V respectively (as measured with a voltmeter). However, when using analogRead, I am getting a value of 1023 (5 Volts) regardless of which resistor I use. The only time I get a different value is when I connect A8 directly to ground, in which case analogRead gives 0. The breadboard is grounded to the Arduino. I have also tried analog input A0 with the same results. If anyone has suggestions for what I am doing wrong, I would greatly appreciate it! Thank you.
Here is the code I am using:
/*
ReadAnalogVoltage
Reads an analog input on pin 8, converts it to voltage, and prints the result to the serial monitor.
This example code is in the public domain.
*/
//
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
if (Serial.available() > 0) {
int b = Serial.read();
// read the input on analog pin 0:
int sensorValue = analogRead(A8);
Serial.println(sensorValue);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
}
}
