Reputation: 1393
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 ofA0
, 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.
Upvotes: 1
Views: 2213
Reputation: 11
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
Upvotes: 1
Reputation: 1393
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.
Upvotes: 1
Reputation: 15887
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.
Upvotes: 0
Reputation: 637
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.
Upvotes: 0