Problem in reading low potentialmetre value analogread ATMEGA328P-PU standalone

Hi!
I have tested the following code in Arduino Nano and I found its working like a beast. Then I uploaded the same code to Atmega328P PU using "Upload Using Programmer" command and Arduino Uno.
All was working fine in standalone atmega328P except it unable read around 0-62 ohm (0- 0.031 volt).
Standalone Atmega328P treat 62 ohm (0.031 volt) as nothing.
If I continue increasing from 62 ohm to 10K ohm then it start to read from 0 to 1023.

int readPotValue () {
  potValue = analogRead(analogPin);
  checkPot = potValue;
}
void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
}
void loop() {
  attachInterrupt(digitalPinToInterrupt(interruptPin), readPotValue, LOW);
  if (checkPot == potValue) {
    Serial.print("Value of potValue is: ");
    Serial.println(potValue);
    potValue = 500;
    Serial.println();
  }
}

Please note that, checkPot and potValue are global variable.

Things that I'm aware of:

  1. 100 nF capacitor with A0 pin.
  2. Several filter capacitor for stabilizing also very close to vcc.
  3. Double reading A0 pin value in a row and using last one.

I'm afraid that the problem is with the bootloader but ignore it because it's just an imagination of my little brain.

I have developed full code and edited and tried hard for freeing from bug and lastly I am satisfied and lastly this is happening with standalone micro-controller.
Now I really need your help badly to rescue myself.

Analog reading with ATMEGA328P-PU.png

Analog reading with ATMEGA328P-PU.png

I see no bypass or bulk capacitance on your schematic, is it possible you forgot to add them?

gilshultz:
I see no bypass or bulk capacitance on your schematic, is it possible you forgot to add them?

Hi,
Thanks for your time & consideration.
I just come to know after your concern about bypass or bulk capacitor. I hope I'll use it and will post update here about it.
As I mentioned in post, the same code and diagram worked for me with Arduino Nano, I think it should also work in standalone circuit too, but you know its not.
I just want to know to rectify my diagram or code.
Thanks

gilshultz:
I see no bypass or bulk capacitance on your schematic, is it possible you forgot to add them?

Hi!
I used a 100 nF and 1 mF capacitor as bypass and bulk capacitor. Sadly it didn’t make any changes.
Thank you

Variables that are used in both the ISR and loop() should be declared volatile.

sterretje:
Variables that are used in both the ISR and loop() should be declared volatile.

Hi,
Thanks for your time and advise. I explored what you said and it was totally relevant. And after that I changed code as follows:

const byte interruptPin = 2;
const byte analogPin = A0;

volatile byte interruptOccur = 0;

volatile unsigned int potValue = 0;
volatile unsigned int checkPot = 0;

void setup() {
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
}

void loop() {
  attachInterrupt(digitalPinToInterrupt(interruptPin), interrupt, LOW);
  if (interruptOccur == 1) {
    readPotValue ();
    interruptOccur = 0;
  }
  if (checkPot == potValue) {
    Serial.print("Value of potValue is: ");
    Serial.println(potValue);
    potValue = 500;
    Serial.println();
  }
}

void interrupt () {
  interruptOccur = 1;
}
int readPotValue () {
  potValue = analogRead(analogPin);
  checkPot = potValue;
}

And sadly, the issue was not solved.
Initially, I burned the bootloader of (standalone) atmega328p MC using official Arduino IDE Version 1.8.13
Do you think that, it is a problem from bootloader?
If so what differences do I need to change in hardware/software or both?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.