1

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?

3
  • Please edit your question to include a Minimal, Complete, and Verifiable example of code, not just snippets. For example, as is we've got no way to tell if sensorValue is volatile Commented Dec 13, 2016 at 23:14
  • Just a little precaution. Do not call Serial.print() on ISR. An Interrupt Routines should be completed and returned as soon as possible.
    – duck
    Commented Dec 14, 2016 at 1:38
  • sensorValue is not changed on anywhere else on the code. I am aware that using Serial.print() on ISR is not a good idea. Serial print will be deleted from the code once the project is complete. Right now, I am using a high baud rate (actually 2000000 works best) to compensate.
    – user29094
    Commented Dec 14, 2016 at 11:17

1 Answer 1

1

However, does not work for:

 ADMUX = 0xC0;

See the datasheet:

ADMUX

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.

1
  • 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
    Commented Dec 14, 2016 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.