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?
1 Answer 1
However, does not work for:
ADMUX = 0xC0;
See the datasheet:
An ADMUX of 0xC0 will be using a voltage reference of "Internal 1.1V Voltage Reference with external capacitor at AREF pin". Since you are supplying 2.5V that exceeds 1.1V and thus you get a reading of 1023.
Serial.println((int)sensorValue);
Also, don't do serial prints inside an ISR.
-
Thanks!! You were right. I forgot all about that! I changed admux to '0x40' and that solves the problem. Also thanks for the serial print issue. I'm aware it's bad and will remove that bit once development is complete. Right now, I am using high baud rate (2000000) so that isr is not affected that much.user29094– user2909412/14/2016 11:26:45Commented Dec 14, 2016 at 11:26
Serial.print()
on ISR. An Interrupt Routines should be completed and returned as soon as possible.