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
0instead 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.
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 readanalogReference(INTERNAL), the bug goes away. but when I useanalogReference(DEFAULT), the problem appears again.A0 -- A7and the output is14 -- 21. From datasheet, the physical analog pins onAtmega328p-PUare '23 -- 28`.AREFpin on my circuit board is connected to100nFcapacitor and other end to theground, same like theArduino Uno Schematic.