analogRead problem with Atmega328

Thread Starter

sumeryamaner

Joined May 29, 2017
117
I have modified the code like this:
Code:
void setup ()
{
  Wire.begin();
  Wire.setClock(400000L);
  analogReference(INTERNAL);
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
  oled.setFont(Callibri15);
  oled.setLetterSpacing(2);
  oled.clear();
  selftest();
}

void loop()
{
  selftest();
  delay(1000);
}
When the selftest() function is being called from setup() it returns 3.20 V. If it is called from loop() it returns correct values. This is really very interesting...

Then I made another modification:
Code:
void setup ()
{
  Wire.begin();
  Wire.setClock(400000L);
  analogReference(INTERNAL);
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
  oled.setFont(Callibri15);
  oled.setLetterSpacing(2);
  oled.clear();
  selftest();
  oled.println("1");
  delay(1000);
  selftest();
  oled.println("2");
  delay(1000);
  selftest();
  oled.println("3");
}

void loop()
{
  selftest();
  delay(1000);
}
After the first call to selftest() the results are correct even if it is being called from setup().

That means, there must be an initial analog to digital conversion to sort thing out...

Now the new problem...
The original circuit and code has an output for a DC motor drive (using PWM and a power MOSFET).
As you can clearly see, this test code does not contain any parts addressing the motor drive output. But... During the tests, after a couple of seconds the motor is being activated as if I am sending increasing PWM commands via analogWrite() function!!! This is unbelievable...
 
I have modified the code like this:

That means, there must be an initial analog to digital conversion to sort thing out...
Maybe, but not necessarily. Is it that a second call is necessary, or is a time interval from setting Vref that is necessary?
In the code you posted in #6, what struck me is that the first conversion was called a few statements after setting the internal reference. In the code that I posted in #7, which uses a "manual" way of setting the reference, I see:
delay(2); // Wait for Vref to settle

In your code in #6 only these lines are executed before calling the function that does the first read:
analogReference(INTERNAL);
oled.begin(&Adafruit128x64, I2C_ADDRESS);
oled.setFont(Callibri15);
oled.setLetterSpacing(2);
oled.clear();
selftest();

Subsequent calls have delays.

I specifically tested for that as you can see in my last post with code, but I did not do so on a standalone, I did it on an UNO. I guess you could test it out systematically.

As far as the "new" problem, well, I guess you have to expect working through things like that. I will continue to follow the thread and post if I think I might have something relevant to add after I have had a chance to digest, although my first impression is that a timer is getting corrupted..
 

Thread Starter

sumeryamaner

Joined May 29, 2017
117
Anyway this thread was very useful for me personally... Thank you for all the input.
I have cancelled the internal reference and I am now using the Vcc as reference. The very first measurement is always incorrect. The second one is almost OK. The subsequent ones are OK. For the new problem, I am afraid I have to contact an exorcist!!!
 

djsfantasi

Joined Apr 11, 2010
9,237
Anyway this thread was very useful for me personally... Thank you for all the input.
I have cancelled the internal reference and I am now using the Vcc as reference. The very first measurement is always incorrect. The second one is almost OK. The subsequent ones are OK. For the new problem, I am afraid I have to contact an exorcist!!!
See post #25
 

mvas

Joined Jun 19, 2017
539
This analog read issue can be fixed, and should be fixed, by fixing the connection between the battery and analog input.
 

Ian Rogers

Joined Dec 12, 2012
1,136
Just a side note here... I read through and I'm sorry if this was mentioned.. Your potential divider is to the tune of 110k... This will slow down things considerably.. I struggled with a 22k divider.. If you need to use the 110k for "battery saving" purposes then your read will have to be very few and far between... You do not need to check the battery every cycle.... I would put a cycle clock and check it once a second...
 

Thread Starter

sumeryamaner

Joined May 29, 2017
117
Just a side note here... I read through and I'm sorry if this was mentioned.. Your potential divider is to the tune of 110k... This will slow down things considerably.. I struggled with a 22k divider.. If you need to use the 110k for "battery saving" purposes then your read will have to be very few and far between... You do not need to check the battery every cycle.... I would put a cycle clock and check it once a second...
The original code does not read the voltage too often. The above code is meant only for testing purposes. But even if I read the voltage infrequently, I want to be sure that the result is somehow correct. :)
For example I am driving a glow plug using PWM output. During this function I have to monitor the battery voltage continuosly to fine tune the PWM signal so that the glow plug is being energised sufficiently but not burnt...
 

Ian Rogers

Joined Dec 12, 2012
1,136
I read somewhere that you are referencing Vcc on the ADC...This will not work as the ADC will read ratiometrically... I use a 2.5Vref so I can get a good battery voltage reference... I would read ADC 4 times then / by 4 with at least 1mS between reads... You are best to up your ADC TAD to the maximum.. I must emphasise that I predominantly use PIC's but I know the procedure is the same..

As long as you load the ADC cap for long enough, your readings should be stable enough..
 

Thread Starter

sumeryamaner

Joined May 29, 2017
117
I read somewhere that you are referencing Vcc on the ADC...This will not work as the ADC will read ratiometrically... I use a 2.5Vref so I can get a good battery voltage reference... I would read ADC 4 times then / by 4 with at least 1mS between reads... You are best to up your ADC TAD to the maximum.. I must emphasise that I predominantly use PIC's but I know the procedure is the same..

As long as you load the ADC cap for long enough, your readings should be stable enough..
Yes. The default for Arduino IDE is that the analog reference is Vcc. In my circuit the Vcc is adequately stabilised and I think I can use it for reference.
 
Yes I do!
The MOSFET driving the motor is connected to PD6 (Atmega328 pin 12). As you can see I didn't declare this pin as OUTPUT in the test code but the motor keeps running. :)
JUST FOR ILLUSTRATION:


Do you have a pull-down R as you see here as R2? [from Gammon.com] I realize that you have not shown the schematic for your driver, but a missing R2 could, conceivably to me, cause such "possession".
 

Ian Rogers

Joined Dec 12, 2012
1,136
Yes. The default for Arduino IDE is that the analog reference is Vcc. In my circuit the Vcc is adequately stabilised and I think I can use it for reference.
Sorry!I wrongly assumed the Battery was the Vcc… If the Vcc is regulated, then all's good.

Just for information... When I need a stable reading I oversample 24 times with a 1mS delay between samples...But I work on cranes, and they are noisy little b***ers…
 

Thread Starter

sumeryamaner

Joined May 29, 2017
117
JUST FOR ILLUSTRATION:


Do you have a pull-down R as you see here as R2? [from Gammon.com] I realize that you have not shown the schematic for your driver, but a missing R2 could, conceivably to me, cause such "possession".
You are right. I have omitted the pull down resistors. In this case if I declare the PD6 as OUTPUT I shouldn't have this behaviour. In the final circuit I will use those pull down resistors. Yhank you.
But...
There is another similar motor connected to PB1 which stays OFF all the time. :)
 

Thread Starter

sumeryamaner

Joined May 29, 2017
117
Sorry!I wrongly assumed the Battery was the Vcc… If the Vcc is regulated, then all's good.

Just for information... When I need a stable reading I oversample 24 times with a 1mS delay between samples...But I work on cranes, and they are noisy little b***ers…
Great advice. I will try it. Maybe not 24 samples but at least 5 - 6 samples... Thank you.
 
Top