[SOLVED] Problem running code on atmega328p-pu (using FT232R for upload)

Hi guys, I'm trying to set up an atmega328p-pu on a breadboard and trying to program it using a FT232R breakout board.
I feel it should be an easy task, but I just cannot figure out a solution.
I bought the MCU which has an UNO bootloader and 16 Mhz crystal.

I have set it up as you can see below:

(Some decoupling caps are missing - would that make a difference?)

When I upload the blink example I can see the heartbeat on the LED and both the TX and RX flashing.
The code "seems" to upload successfully as I get the following output:

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\Downloads\arduino-1.8.4-windows\arduino-1.8.4\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM20
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 4.4
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\AppData\Local\Temp\arduino_build_301504/sketch_sep15a.ino.hex"
avrdude: writing flash (1864 bytes):

Writing | ################################################## | 100% 0.72s

avrdude: 1864 bytes of flash written
avrdude: verifying flash memory against C:\Users\AppData\Local\Temp\arduino_build_301504/sketch_sep15a.ino.hex:
avrdude: load data flash data from input file C:\Users\AppData\Local\Temp\arduino_build_301504/sketch_sep15a.ino.hex:
avrdude: input file C:\Users\AppData\Local\Temp\arduino_build_301504/sketch_sep15a.ino.hex contains 1864 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.64s

avrdude: verifying ...
avrdude: 1864 bytes of flash verified

avrdude done.  Thank you.

One puzzling thing was that I have set the FT232R on my COM to have a baud rate of 9600, but it seems to be overriden to 115200 when I upload on the arduino IDE.

Trying to see how the code runs, I have added in some serial print statements:

int ledPin = 19;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.print("setup");
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("Loops");
  digitalWrite(ledPin, HIGH);
  delay(10000);
  digitalWrite(ledPin, LOW);
  delay(10000);
}

By setting the baud rate within the code to 9600, I could see the serial prints whereas I couldn't see anything without it. Monitoring the COM outputs showed an endless print of "setupLoops". It looks like setup() is looping when it shouldn't and loops() is correctly running but the LED isn't blinking.

It appears I'm getting the timing wrong somehow, but I am clueless. Another reason I can think of is not delivering enough power delivered as I am sourcing it from the FT232R.

If you could please help it would be much appreciated!
Thank you in advance.

Make a pencil drawing showing your connections with everything labeled so we can read it and post a photo of the drawing.

Do you have a connection from DTR to the reset pin on the 328?

I run all my breadboard 328s using the internal 8MHz oscillator.

Nick Gammon's diagnostic programs may help, And also see his bootloading tutorial

...R

(Some decoupling caps are missing - would that make a difference?)

Yes, it does.

I see you are using “Pin 19” for your LED pin but you have the LED connected to Pin 13 like a normal Arduino. Just because your Arduino is on a breadboard doesn’t mean you should start using the ATmega328P-PU pin numbers instead of the Arduino pin numbers. That’s why you have the pin number label on your chip!

johnwasser:
I see you are using "Pin 19" for your LED pin but you have the LED connected to Pin 13 like a normal Arduino. Just because your Arduino is on a breadboard doesn't mean you should start using the ATmega328P-PU pin numbers instead of the Arduino pin numbers. That's why you have the pin number label on your chip!

Yes, it was embarrassingly simple! I just had initialised the wrong pin number in my code. Should have been 13 as you said and I can see the LED blinking now. However, the delay function doesn't seem to work properly when I pass in different values now. I will have to look into this but at least the code uploaded.

Thank you everyone for your comments!

In what way does the delay() function "doesn't seem to work properly"?

I am trying to change the frequency of the blinking LED in this code:

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

No matter what ms value is in delay(), it always seem to flicker at a certain rate. It definitely doesn't seem to wait a whole second in between. I tried to increase the delay time very long but nonetheless it generates a rapid fast blink. In fact, I took out the delays altogether to drive the LED high but it was still flickering!

I also tried running a DigitalReadSerial example:

/*
  DigitalReadSerial

  Reads a digital input on pin 2, prints the result to the Serial Monitor

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/DigitalReadSerial
*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

I get some sort of garbage back when I'm expecting 0s. °0 returns every 5th print.

0
°0
0
0
0
0
°0
0
0
0
0
°0
0
0
0
0
°0
0
0

When I press the button to expect 1s, the timing is out of place and I also get garbage back together.

0
0
0
0
°1
1
0
0

Should it not be a row of 1s unless I've made the button to debounce?

It seems to me that I can communicate to the board but something has gone wrong. It could really be the absence of decoupling capacitors screwing up the signals so I will go over the hardware connections again. I've found old threads years ago mentioning problems with Linux environment and delay function but I'm running Arduino IDE 1.8.4 on Windows 7, so I'm guessing it should be irrelevant.

If you feel I have overlooked anything else, please let me know. Thank you.

tldbs0204:
No matter what ms value is in delay(), it always seem to flicker at a certain rate. It definitely doesn't seem to wait a whole second in between.

Your little blink program should work properly. It would be pointless to explore anything else until you are sure that that program works reliably.

If it is not working properly there is probably something wrong with your hardware or the program did not actually upload at all.

You can check if the program uploads by changing the LED to a different pin and uploading the revised version,

...R

I know that the program uploads because I could change the pin numbers and they flashed accordingly.
Also added some decoupling capacitors and changed the power source from usb to power supply but not lucky yet.

I’ll come back after I’ve made some progress…

Thanks.

Hi,
Have you fitted bypass capacitors yet?

You are changing both delay functions?

delay(500);

Tom.... :slight_smile:

Hi there,

So I disconnected everything and started on the breadboard again. Checked all connections, re-installed Arduino IDE, and got a different USB cable.
I have placed capacitors across Vcc-Gnd rails.

Then tried passing in some ridiculously long numbers in both delay() functions but still flickering at a high rate...

I feel the oscillator is a problem here but can't quite figure out what could have gone wrong.
The settings in boards.txt seems fine to me but it appears somehow the oscillator at 16 Mhz is not syncing correctly with PC commands..

##############################################################

bdo.name=Breaduino // my custom board

bdo.vid.0=0x2341
bdo.pid.0=0x0043
bdo.vid.1=0x2341
bdo.pid.1=0x0001
bdo.vid.2=0x2A03
bdo.pid.2=0x0043
bdo.vid.3=0x2341
bdo.pid.3=0x0243

bdo.upload.tool=avrdude
bdo.upload.protocol=arduino
bdo.upload.maximum_size=32256
bdo.upload.maximum_data_size=2048
bdo.upload.speed=115200

bdo.bootloader.tool=avrdude
bdo.bootloader.low_fuses=0xFF
bdo.bootloader.high_fuses=0xDA
bdo.bootloader.extended_fuses=0x05
bdo.bootloader.unlock_bits=0x3F
bdo.bootloader.lock_bits=0x0F
bdo.bootloader.file=optiboot/optiboot_atmega328.hex

bdo.build.mcu=atmega328p
bdo.build.f_cpu=16000000L
bdo.build.board=AVR_UNO
bdo.build.core=arduino
bdo.build.variant=standard

##############################################################

The code definitely uploads successfully:

avrdude: Version 6.3, compiled on Jan 17 2017 at 12:00:53
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

         Using Port                    : COM20
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
         AVR Part                      : ATmega328P
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC2
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : Arduino
         Description     : Arduino
         Hardware Version: 3
         Firmware Version: 4.4
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\AppData\Local\Temp\arduino_build_807455/Blink.ino.hex"
avrdude: writing flash (940 bytes):

Writing | ################################################## | 100% 0.38s

avrdude: 940 bytes of flash written
avrdude: verifying flash memory against C:\Users\AppData\Local\Temp\arduino_build_807455/Blink.ino.hex:
avrdude: load data flash data from input file C:\Users\AppData\Local\Temp\arduino_build_807455/Blink.ino.hex:
avrdude: input file C:\Users\AppData\Local\Temp\arduino_build_807455/Blink.ino.hex contains 940 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.34s

avrdude: verifying ...
avrdude: 940 bytes of flash verified

avrdude done.  Thank you.

Any of your expert opinions on clock settings & timing with mcus would be much appreciated..! Thanks.

Just running this piece of code for testing purposes:

#include <avr/boot.h>

void setup() {
  Serial.begin(9600);
  int lf = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
  int hf = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
  int ef = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS);

  //Serial.println("\nLow Fuse Bits: ");
  //Serial.println(lf, HEX);
  //Serial.println("\nHigh Fuse Bits: ");
  //Serial.println(hf, HEX);
  //Serial.println("\nExtended Fuse Bits: ");
  Serial.println(ef, HEX);
}

void loop() {
}

When I run it, it only outputs "\nLow Fuse Bits: " continuously. I then commented out the rest of sections to retrieve each fuse values. The returned values didn’t match up to my settings in boards.txt. It must be a communication interference producing garbage right?

Have you tried Nick Gammon's programs that I gave you a link to?

...R

tldbs0204:
The returned values didn't match up to my settings in boards.txt.

The fuses only get set when you Burn Bootloader. Did you do that successfully?

Robin2:
Have you tried Nick Gammon's programs that I gave you a link to?

...R

Hey, I ditched the AVR Pocket Programmer and got a Mega2560 to re-burn the bootloader and finally everything works now! I also checked and confirmed the functionality on my other spare mcu successfully.

I was going through so much trouble with the AVR programmer but using another Arduino was so much simpler and easier - lesson learnt: own a good working Arduino for troubleshooting & peace of mind.

Thank you all so much for your help!! I really appreciate it!
Hope you have a nice day. :slight_smile:

tldbs0204:
own a good working Arduino for troubleshooting & peace of mind.

+1

...R