I'm trying to read to voltage output of module ACS712. Here is my problem: The module outputs a voltage around 2.5V, however, arduino sends the value "1023" to the PC. (Gives a reading of 5V).
Arduino's analogRead function works, however, is slow so I used atmega registers to solve the issue. Which worked... Except only for the module. Here is the relevant part of my code:
Setup:
void setup() {
ADMUX = 0xC0;
ADCSRA = 0x8E; // prescaler = 64
ADCSRB = 0x40;
sei();
bitWrite(ADCSRA, 6, 1); //This starts single converion.
Serial.begin(250000);
}
Relevant ISR function:
ISR(ADC_vect) {
sensorValue = ADCL; // read adcl first. locks adc bytes.
sensorValue += ADCH << 8;
Serial.println((int)sensorValue);
bitWrite(ADCSRA, 6, 1); // Set high so another conversion starts.
}
Now, this code runs as expected for another adc pin which is located at "A3", or in code:
ADMUX = 0xC3;
However, does not work for:
ADMUX = 0xC0;
Yes, I tried replacing the wires A0 and A3. This time "0xC0" works but "0xC3" doesn't. So my arduino somehow is resistant on reading the module output!
Also, this works on same hardware:
sensorValue = analogRead(A0);
Serial.println((int)sensorValue);
Arduino's analogRead runs as expected on same hardware.
So basically, the hardware seems okay, but there seems to be a problem with my code. I just can't read the modules output voltage and I'm all out of ideas. Can anyone help?
Serial.print()
on ISR. An Interrupt Routines should be completed and returned as soon as possible.