Can't get stand alone ATmega to work - please help!

Hello everyone,
I’ve just registered here because I need some help from experts. I really hope someone can find a solution to my problem. I appreciate any and all help.
Please be aware that I do not have any programming experience but have a friend helping me out that has some experience in using C and programming AVRs but not with Arduino.

What I’m trying to accomplish:
I want an Arduino sketch to run on my ATmega8 or ATmega328 chip without using the Arduino board. I want to build my own circuit. No bootloader if possible.

As a test I’m trying to run the simple BLINK sketch.

What I’ve tried so far:
I have connected the AVR to a programmer and following the online guides for bread board programming I have uploaded the BLINK. I can communicate with the chip and I can burn the data which is then verified correctly. Everything checks out fine but the BLINK doesn’t blink.
I have checked the connections time and time again and everything is fine. I have used different AVRs and crystals but still get nothing.
I have followed a guide describing using an Arduino board to program an AVR and did it exactly step-by-step. Everything seems fine but still no blinky.
I have tried changing the fuse bits and using an internal clock. I can still communicate with the AVR, burn the code and verify it but it doesn’t run. No blinky-blinky.

I have tried burning the Arduino bootloader onto the AVR and again, no success.
I have tried burning the code without the bootloader. Same effect.

The only thing I didn’t try was to install the chip in the Arduino board as the board I’m using has an SMD chip while I’m using a through hole package.

Important note - I have a code written in C (no Arduino involved here) and as soon as I upload this code it starts right up and runs just fine. With the Arduino code the chip just sits there doing absolutely nothing but I can still talk to it with the programmer.

What am I doing wrong??
There must be something that is not included in all those online guides that prevents the chip from executing the Arduino code.

Thanks,
Tom

Jamesik:
the BLINK doesn't blink.

How do you know?

You might be looking at the wrong pin. Which pin are you connecting your LED to?

Well, I checked to make sure that I am looking at the right pin. At first I've tried it on my custom board where I have two LEDs connected pins 2 and 3 but when I couldn't get that to work I hooked my AVR exactly as described in the Arduino tutorial (on a bread board). I followed all steps EXACTLY and connected the LED as described. Doesn't blink.
I see absolutely nothing on all pins. I've checked and confirmed this with a multimeter. It's as if the chip doesn't start at all.

Note - although I have no programming experience I do have years of electronics experience. Now that I have a bit more free time on my hands I wanted to try using micro controllers in my circuits but so far I can't get it to work.
Just to make sure I'm not a complete idiot - I did get the BLINK to work on the Arduino board :wink:

It’s hard to see how a sketch would upload and verify but not run.

Try blinking all the pins:

void setup()
{
  DDRB = 255;
  DDRC = 255;
  DDRD = 255;
}
void loop()
{
   delay(1000);
   PINB = 255;
   PINC = 255;
   PIND = 255;
}

Thank you for your help fungus.

I just tried your suggestion and the results are the same. The code uploads to the chip and I can verify it. Checks out fine but the program doesn't run. It's as if it's totally dead.
The ATmega8 is on the breadboard with all necessary connections including the crystal and its accompanying caps.

Does it make a difference as to what hardware/software I use to burn the chip? This time I've used USDasp programmer with BASCOM AVR. I have compiled the sketch in Arduino IDE and then used the other soft to upload the hex file to the chip.
I can verify the presence of the chip and identify it. I can then erase the chip and upload the hex. It will then verify the uploaded data and display the result. I get NO error messages. Blink doesn't blink.
I use this setup because I don't want to waste the precious memory on the Arduino bootloader. I did try burning the bootloader and using the IDE to write the code directly and it also works fine with no errors but the effect is the same. No blinky.

As soon as I burn my C hex file (written without any Arduino) the program starts right after resetting the chip and works fine.

Any ideas??

Maybe:
listen-to-your-arduino

Also, check out Nick’s page:
http://www.gammon.com.au/forum/?id=11637

Often, new users flash the boot loader, but forget the fuses that control things like oscillator, prescalling, BOD, etc.

Ray

Thank you mrburnette for your suggestions. I will try the AM radio trick and will report back.
I did check all those fuse bits before I posted here and all appear to be fine but it's possible that I missed something. Well, I think it is actually pretty obvious that I did miss something. Otherwise I wouldn't be posting for help. :wink:
I'll post my findings later on tonight or tomorrow night.

Thanks!

I did check all those fuse bits before I posted here and all appear to be fine

If you can "talk" to the 328P over ISP using the ArduinoISP sketch then this pretty much states that the integrity of your Arduino install, USB, cabling, etc. are all OK.

When working with breadboarded uC, sometimes the crystal and load caps can be an issue. Protoboards are notorious for having oxide and resistance from use, finger oils, tape from components, wax, and so on... sometimes, just pulling and inserting the crystal will "correct" the issue. This is where the radio trick really helps. RF from the 16MHz crystal really creates a lots of hash.

You may also find this article on Windows Batch scripts and AVRDUDE helpful:
http://www.hackster.io/rayburne/avr-firmware-duplicator

Good luck,

Ray

mrburnette:
If you can "talk" to the 328P over ISP using the ArduinoISP sketch then this pretty much states that the integrity of your Arduino install, USB, cabling, etc. are all OK.

Yep, all seems to be OK yet it doesn't work. This is why it's so frustrating.

When working with breadboarded uC, sometimes the crystal and load caps can be an issue.

I seriously doubt that this is a problem with my setup. Why? Several reasons:

  1. Tried using internal or external oscillator
  2. Tried uploading with external programmer
  3. Tried uploading with an Arduino board
  4. Tried ATmega8 and new ATmega328 (thought the 8 was bad)
  5. Asked my friend to try it on his chip
  6. Tried uploading with ArduinoIDE
  7. Tried uploading with BascomAVR
  8. Tried loading the Arduino bootloader and then the Blink sketch.
  9. Tried loading the Blink without the bootloader
  10. Tried breadboard and my custom project board with soldered components

Every time the result is the same. I can identify the chip, erase it, program it, verify it. I get NO errors during programming. All checks out but the blink doesn't work.

IF I upload my other hex which was written in C and has nothing to do with the Arduino (written by a guy who doesn't do Arduino at all) it starts right up and runs fine.

This lead me to research the fuse bit settings and checked those time and time again to make sure that they are good. I didn't find any bad settings here ever!
It seems that I can only get the sketch to work on the dedicated Arduino board and not as a stand alone solution. In my case I can't and don't want to use the Arduino board. This is a controller board that is specialized and I can't connect a separate board to it.
The original plan was to use software written in pure C but Arduino seems easier and more people know how to do it. At this point, since I can't find anyone willing to help me out with the original project written in C (regardless of the $$ I offer) and I can't seem to be able to get the Arduino to work, the only thing left to do is to throw both board as far as I can so I'll never see them again.

This is where the radio trick really helps. RF from the 16MHz crystal really creates a lots of hash.

I can't find an old radio that is small enough to use in this experiment. The only one that I have is way to big to get the antenna close enough to the board. I'll keep looking though.

I appreciate all your help and if there's anything else you can think of please do let me know. I really want this thing to work!

Thanks,
Tom

The problem must be in your setup. I and hundred of people build board-duinos all the time. I just cannot figure out what is wrong by the steps you are writing.

programming-arduino-uno-in-pure-c

Arduino is an environment, not a language. A bunch of Hocus-Pocus goes on with header files, macros, and library routines to make an Arduino an Arduino! The compiler you never see is a C and C++ compiler.

Options:
Use a prebuilt board designed to be embedded:
Pro Mini
IF the above works, you can order them by the hundreds from China for under $3 each - free shipping!

More powerful options:
teensy

Finally, computers, programming, microcontrollers are all synonyms for "fustration.". Chill and weigh your options. The forum has a Collaboration section... Good folks willing to work for wages.

Ray

mrburnette:
The problem must be in your setup. I and hundred of people build board-duinos all the time. I just cannot figure out what is wrong by the steps you are writing.

I am more than sure about this. I just can't for the life of me figure out what I'm doing wrong. I guess I was hoping that someone will say something like "oh, easy, when programming stand alone AVRs you have to ..." I guess the answer is not that simple.
What really got me confused is the fact that when I asked my friend to duplicate my setup and program his ATmega328 he got the same exact result as mine. So he must have made the same mistake. Something so simple and so dumb that people don't even think about mentioning in the step-by-step guides.

Arduino is an environment, not a language. A bunch of Hocus-Pocus goes on with header files, macros, and library routines to make an Arduino an Arduino! The compiler you never see is a C and C++ compiler.

I got the general idea of what it is but the truth is that while I'm pretty handy with hardware the whole programming, C, Arduino etc is completely new to me and I know pretty much nothing about it. So, I built a circuit and asked someone to write the code and deliver a chip that was plug-n-play. I got a test version and the guy just backed out saying that he was too busy. Now I'm trying to get the Arduino setup working as more people know how to write the code.

Use a prebuilt board designed to be embedded

Although this sounds like a great solution I don't really want to go this way. With the number of units that I need to make I need to keep my costs down and I need to make assembly as easy as possible.

Finally, computers, programming, microcontrollers are all synonyms for "fustration.". Chill and weigh your options.

Thanks Ray! I'm not giving up just yet. On the other hand I do feel like I've hit a wall here. I just need that little help to get through this :wink:
I'll keep trying and I will update this thread if I end up finding a solution. I don't believe that I'm the only one with this problem (not counting my friend).

If your loading a program for a arduino without the bootloader you have to set the chips fuses you cant be setting them..
Show the code you loaded.

@jamesik:

This is one of the best guides I have found on building a board-duino:

Nick Gammon

There is also some very good firmware that Nick has written to help work through issues.

After an Atmega328P is programmed to use a crystal, you must have a crystal in the circuit before you can reprogram anything. Some ArduinoISP circuits and sketches bring out a "clock" line to allow you to program without using a crystal oscillator.

If you are going to do anything in quantity, you are going to have to have a ZIF socket programmer. They are very inexpensive and work great. This generally clears up all of the programming issues at the chip level.
ZIF programming shield

For SMT:
Pogo Bed

Hint: buy a "real" arduino UNO to mate this board on so that you have a relatively "clean" programming environment for a reference build. I have one that I have used for years that is dedicated and just sits on the bench and my total investment was about $45 and an hour to solder the shield.

You can also use a PC to "batch" copy sections into a chip:
My Windows 328P copy script

Buy quality parts - in quantity! Mouser and Newark (Element14) and others offer great breaks on prices in the over 25 quantity.

I think you really ought to rethink the 328 Pro Mini from China. Everything is on one board and the price at the 100 Quantity of SMT 328P is $2.27 U.S. each. The boards with SMT328P and resonator and bypass capacitors is under $2 in 10 Quantity!
10pcs-lot-Pro-Mini-328-Mini-ATMEGA328-5V-16MHz

... unless you plan to contract your own off-shore manufacture for the entire device. Lots of companies use pre-made modules on another circuit board... either with direct soldering of the modules on wire standoffs or with pin-headers for easy replacement/upgrade.

Good luck,

Ray

be80be:
If your loading a program for a arduino without the bootloader you have to set the chips fuses you cant be setting them..
Show the code you loaded.

I have to say that you lost me with this whole setting the fuses deal here.
The code is the basic "blink" that is included with the Arduino IDE. That's right, it's that code that is NOT modified in any way whatsoever. I did change the LED pin for my custom board (I have LEDs on pins 2 and 3) but when that didn't work I tried the default setup on a breadboard and unmodified "blink" and it doesn't blink.
So, the code is what is included with IDE.

mrburnette:
@jamesik:

This is one of the best guides I have found on building a board-duino:

Nick Gammon

This is exactly what I did. The only difference is that I didn't use an UNO. I only have this Arduino board that I got from a friend of mine. He said that it is a super-duper-mega version of Arduino... or something like that. Like I said, I don't know much about Arduino boards in general :wink:

After an Atmega328P is programmed to use a crystal, you must have a crystal in the circuit before you can reprogram anything. Some ArduinoISP circuits and sketches bring out a "clock" line to allow you to program without using a crystal oscillator.

I don't see a problem with the clock in my case. As stated before, I have tried setting internal oscillator and external one with a crystal. I did try different chips and different crystals. Besides, there is no problem with "talking" to the chip. I can ALWAYS read the contents, check and program the fuses, I can verify the data on the chip. It works just fine.
From the behavior that I observe I was under the impression that I'm missing something somewhere that tells the chip to execute the code. It's as if its just sitting there doing nothing because it doesn't know it was suppose to run the code I just uploaded. Or, I thought it's just stuck in some programming mode because I can always talk to it again with the programmer or that Arduino board that I have.

If you are going to do anything in quantity, you are going to have to have a ZIF socket programmer.

I have incorporated a programming port on my board and was hoping I could use that. This way I could simply assemble all the boards including the AVR and then simply connect the programmer, click on "burn" and be done with it. It works great with the non-Arduino hex file that I have. I just plug the programmer in, click on "burn" and after a few seconds my on-board LEDs go through a test routine. I then unplug the programmer, connect external power source and it works.

Hint: buy a "real" arduino UNO to mate this board on so that you have a relatively "clean" programming environment for a reference build.

That is a good advice. I'm starting to think that this super-duper board that my friend got (and let me use one of them), might the culprit. On the other hand I did try an external programming board and it didn't work either.
I guess I'll start over by getting a real UNO and see what happens then.

I think you really ought to rethink the 328 Pro Mini from China. [...]
... unless you plan to contract your own off-shore manufacture for the entire device. Lots of companies use pre-made modules on another circuit board... either with direct soldering of the modules on wire standoffs or with pin-headers for easy replacement/upgrade.

I will be making the boards myself. The quantity is not large enough. If I was to make 10.000 units then yes, I would look for someone to take care of the production. With up to 1000 units I will be doing all the work myself. I have all the necessary equipment and experience required. I just need to figure out why Arduino doesn't want to talk to me.
My circuit is going to be used in a car and for that reason I'd like to avoid using external boards. I was going to use a SMD chip to make the board as small as possible and to make it vibration resistant. I was never a fan of using additional boards because of the vibration experienced in a car. I much prefer a single board design.

Thank you for all your help. I will post when I find a solution to my problem.

Thanks Ray and others!

Tom

You need to show what code your using. You can load a blank chip without bootloader any thing you want but the bootloader set’s up the chip to run it oscillator type speed elc.
If you use the arduino ide to load a chip with a simple blink it will.

This is what avr-gcc would look like

#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

int
main (void)
{
    DDRB |= _BV(DDB0); 
    
    while(1) 
    {
        PORTB ^= _BV(PB0);
        _delay_ms(500);
    }
}

This is what arduino looks like

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

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

both can be compiled into a hex that you load the chip with. But the first one will run stand alone the second one needs to use a bootloader.
and for any of it to work the chip needs to be wired like this for the the first one to work and fuses set for the second to match this layout. But if you add a crystal and caps the fuses change. But to help you we need to see what code and how your hardware is setup.

Actually, telling you to burn the fuses may have been a mistake. By default the fuses come ready to run on internal clock and to start execution at the address occupied by your sketch. This would be fine. with or without the bootloader present.

If you did, indeed burn the fuses it might be an idea to tell us what values you used. If you used those on the arduino mega boards, then your chips will now be expecting an external 16mhz chrystal oscillator AND it will start execution at the address where the bootloader normally resides.

Did you burn those fuses? and if so, what values did you use?

I have only changed one fuse manually and that was the oscillator - internal or external.
I have not touched any other settings because I do realize that I know nothing about what they do. So, the fuse was originally set to run on internal clock but since I find a lot of information about Arduino running with external oscillator I decided to try that option. I've added an external crystal and caps and changed the setting to external. Noting has changed, I can still upload and verify the code but it doesn't run. I did confirm that the setting was correct because I couldn't read the chip when the crystal was removed. After changing the fuse back to internal clock it would keep running even with the crystal removed.

To make sure that we are clear on what setup I'm using I'll try to describe again what I had and did.
I have a custom board with an ATmega8 that was programmed by somebody else. No Arduino was involved here. The code uploaded was written in C. It run fine. This board does not have a crystal and runs on internal clock. There is a programming connector installed on the board. The connector uses pins VDD, MOSI, SS, RST, SCK, MISO, GND from the AVR.
I can't find the exact guides that I've followed while attempting the programming but I can describe it here from my poor memory :wink:
One of them was this one:

As per the guide I have made the necessary changes to the preferences file to use my programmer - USBasp.
Using ArduinoIDE I have chosen the blink sketch and changed LED pin from the default 13 to my pin number 2 (or 3).
I have then uploaded the code to the AVR. I had no error messages and the IDE said it was successful. After power cycling the board I got nothing. I checked all pins and there was no blinking or anything happening on any of them.
I could still communicate with the board and decided to try a different approach. I took the hex file created in the IDE and opened it in a BASCOM AVR software. This software allows me to check the presence of the chip, identify it (it says that it's an ATmega8A), erase it, burn the code to the chip, verify the process. After power cycling I again got nothing.
Then I took my original hex file (non Arduino) and again using the BASCOM burned that to the chip. The program started running as soon as the programming was finished.
Thinking there might have been something wrong with the chip I tried the same process on a brand new ATmega328. Same effect. I tried my original C hex on that chip and again the program started right after programming was finished.

Thinking that I must be doing something wrong I asked my friend to burn the bootloader onto my ATmega8 chip and try it this way. He did that using his Arduino Mega board and settings used for breadboard programming. Then we programmed the blink using ArduinoIDE. We got no error messages, the programming was successful but it didn't run.
Using the USBasp board and BASCOM programmer I again uploaded my original hex and it started right up.

I have tried following different guides found around the web and doing it slowly step-by-step to make sure I don't miss any steps. My friend did the same on his ATmega328 and we both got the same results. With or without the bootloader we can talk to the chip but the blink isn't executed. We both can still talk to the chip via the programmer so it still works.
I have even tried using the default "blink" and wired the LED to the default pin. At this point I didn't make any changes to the code.
At this point I have tried adding a crystal and caps to the board and changed the fuse to use an external resonator and uploaded the code again. Still nothing. Went back to internal clock and still got nothing.

After trying so many different ways of uploading with and without the bootloader I again used the BASCOM AVR and that USBasp programmer and uploaded my old hex file. It started right up.

Again, I'm wring this from my memory so I might have missed something here. The important part is that I have always followed the guides to the letter. I didn't skip any steps and I never go by "good enough" standards.

Any ideas what I might be missing here? What surprises me is that my friend got the same result when I asked him to follow the same guides and we are both lost here.

Thank you guys! I really hope we can find a solution. I'm waiting for that "duh" moment :wink:

Jamesik:
I have only changed one fuse manually

Can you describe exactly how you did that.

KenF:
Can you describe exactly how you did that.

Sure
The BASCOM software allows me to read the contents of the AVR chip. It also allows me to read the fuses. I click on "read chip contents" and then I get three tabs. First one is the program memory, second is the additional memory and third is the fuses tab. There is a whole list of fuses separated into different categories. One of them is called Clock (IIRC) and when I click on that a drop down menu opens with all available options. Internal or external with different speeds. My ATmega8 was originally set to Internal oscillator at 1MHz. I tried different settings here but to no avail.
After choosing the appropriate option I click on "Write fuses". The programmer's LED changes from green (ready) to red. After a second or so it changes back to green and the software reports that the changes were written to the AVR. That is how I did it.
Like I said, I checked to make sure that the change actually works. When changing to external clock I can only read the chip's status when external crystal is present. Without it the chip is dead. When I change to internal oscillator the crystal can be removed and the chip still works.
There is an option in BASCOM AVR called "Identify chip". When I click on that it tells me if the chip is connected and what type it is and the amount of memory.
Any ideas?

I looked at my arduino files and everything in it loads code to run after a boot loader some thing has to tell the chip where your code is to run it.
Could you post your preferences.txt.