1

I am using Atmega328 with arduino bootloader. My whole code is working fine. Now I need to use analogRead() to get ADC values, but as soon as PC see analogRead(), it restart microcontroller. Here is the sample code.

void setup() {
  Serial.begin(19200);
  while(!Serial);
  Serial.println("Setup finish");
  delay(200);
}

void loop() {
  Serial.println("Reading analong Values");
  uint16_t a = analogRead(A0);
  Serial.println(a);
  delay(1000);
}

The output is,

Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�Setup finish
�

I also tried to put delay() before and after it but no vain. How to fix it. Thank you.

Update: I have tried 0 instead of A0, but no vain.

Update: The problem all boils down to voltage selection(3.3 or 5V) switch on FTDI programmer. Setting it to 5V works perfectly, but switching it to 3.3V, the problem appears again.

4
  • 1
    What do you have feeding into your Analog 0 pin? I would suggest adding Serial.println("n"); where n is a number in between all your code to see where exactly it is getting hung up on just in case it isn't the analog read Commented Dec 2, 2016 at 14:43
  • When I add analogReference(INTERNAL), the bug goes away. but when I use analogReference(DEFAULT), the problem appears again. Commented Dec 2, 2016 at 15:04
  • I print out A0 -- A7 and the output is 14 -- 21. From datasheet, the physical analog pins on Atmega328p-PU are '23 -- 28`. Commented Dec 2, 2016 at 15:07
  • The AREF pin on my circuit board is connected to 100nF capacitor and other end to the ground, same like the Arduino Uno Schematic. Commented Dec 2, 2016 at 15:09

4 Answers 4

1

The inductor between power supply 3.3V and AVcc pin was the problem of analogRead() reset. The purpose of this inductor is mentioned here, Inductor purpose. But short-circuiting the inductor clears the problem.

Sign up to request clarification or add additional context in comments.

Comments

1

I recommend that instead of writing uint16_t a = analogRead(A0), you should declare the input of A0 as a variable and call it later in the program.Also, there is a mistake in the void setup() section which is that you wrote the while loop with the condition and the ended the line with a semi colon. You should have written the actions that are to be done if the statement is true between curly brackets

Comments

0

From the Arduino site here:

DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)

INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)

This clearly shows that atmega328 you are using requires an internal reference to 1.1v when reading off the analog inputs. It is probably restarting because when using analogReference(DEFAULT); the atmega328 doesn't know how to properly decode the signal and it crashes out.

12 Comments

The voltage you reference the pin at doesn't change what the value coming in is. It simply tells the micro controller how to read the value and reference that value. There are different dependencies across the different boards that you have to work with and anything to do with analog always has the potential to be finicky. If you want to use the atmega328 then you have to use the internal reference. If you really want to have an input signal up to 3.3v, then you have to switch boards.
it has to do with the components you are using. if you have a 3.3v controller with a 5v device attached it, the arduino wouldn't be able to read that. Logic levels have to match except in the case where a 3.3v device is sending data to a 5v device. Basically you can't read values that have a higher logic level than the board your code is running on. @YannVernier also makes a very good point with the A0 vs 0, I hadn't caught that
This should help with understanding logic levels: learn.sparkfun.com/tutorials/logic-levels
What is connected to your A0?
I'm at a loss then, it seems like you don't have a solid grasp on how the data is being transferred around and that's what's causing your confusion. I would very highly suggest doing some reading on the Arduino page about Analog data and how it interacts with your specific board. Like I said, analog can be finicky but it does work assuming you know how to approach it right. It's hard to help you suss out the exactly problem without the circuit directly in front of me
|
0

Since analogRead only works on analog input pins, it takes the channel number, not a pin number. Try passing 0 rather than A0. It likely fails because A0 has a higher number (as a digital pin) than the number of analog input channels, causing an out of bounds error.

4 Comments

It is still the same, but when programmer (FTDI) is set to 5V, analogRead() work fine. The problem appears as soon as I change the switch to FTDI to 3.3V.
A0 is not the problem. analogRead maps a pin number to the channel number.
That sounds a bit odd. Most Arduino boards are 5V powered. I suppose if you were to drive a pin high connected to a 3.3V supplied chip, and the 3.3V supply supported negative current and the 5V one was weak, you could get power down to where the controller runs out of spec (AVRs are frequently only rated to 8MHz on lower voltages). You could get other mystifying results too.
@gre_gor: I stand corrected. Checked github.com/arduino/Arduino/blob/master/hardware/arduino/avr/… as well, can confirm they do double map the things, and merely left the documentation unclear.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.