Home Physics Programming an ATmega8A utilizing Arduino

Programming an ATmega8A utilizing Arduino

Programming an ATmega8A utilizing Arduino


In case you are occupied with programming and electronics, you in all probability don’t want an introduction to Arduino. If you wish to make your Arduino tasks everlasting, then it’s a good suggestion to make use of solo microcontrollers quite than Arduino boards within the remaining setup. Whereas Arduino boards are nice for prototyping, shopping for an Arduino board for every mission might not be very budget-friendly. Single microcontrollers usually price lower than a full Arduino board. In some circumstances, one might have a couple of microcontroller for his or her mission, and in that scenario, utilizing two Arduino boards might not be one of the best answer. The power to program microcontrollers with Arduino will permit customers to jot down supply code in C after which merely add it to the MCU.

On this article, I’ll present you easy methods to program an ATmega8A microcontroller utilizing Arduino. I’ll assume that you’ve got some fundamental expertise in Arduino. With out additional ado, let’s get began.

{Hardware} Necessities

  1. An Arduino board (boards with ATmel AVR microcontrollers, like Uno, Mega, and so on.; ##3.3~mathrm{V}## boards like Due or Zero aren’t most popular) with a USB cable,
  2. ATmega8A microcontroller,
  3. A breadboard,
  4. Two ##22~mathrm{pF}## capacitors,
  5. One ##100~mathrm{nF}## capacitor,
  6. One ##2.2~mathrm{mu F}## capacitor,
  7. One ##16~mathrm{MHz}## or ##11.0592~mathrm{MHz}## crystal,
  8. One ##10~mathrm{kOmega}## resistor,
  9. One ##330~Omega## resistor,
  10. One/two LEDs,
  11. Jumper wires.

Terminology and model

I ought to briefly point out the terminology we shall be utilizing. You’ve gotten an Arduino board (Uno, Mega, and so on.) which itself has a microcontroller (“MCU” in brief). For instance, the Uno board has an ATmega328P, whereas the Mega 2560 board has the ATmega2560. This MCU shall be referred to as the “on-board microcontroller” to tell apart it from the ATmega8A.

We shall be referring to 4 kinds of pins on this article:

  1. The pins on the Arduino board. You utilize these pin numbers instantly within the Arduino sketch, like digital pin 13 or analog pin A2.
  2. The pins of the onboard microcontroller. These are related to the assorted pins on the Arduino board, however the mapping differs from one Arduino board to a different. So, when needed, we could instantly confer with/entry some pins of the onboard MCU quite than the corresponding pins on the Arduino board.
  3. The native pins/ports of the ATmega8A. These are named PCx, PDx, PBx, and so forth. For particulars, confer with the pinout diagram within the ATmega8A datasheet.
  4. The Arduino pins of the ATmega8A. These are the pin numbers you may be utilizing in your sketch that shall be uploaded to the ATmega8A. There’s a good pinout diagram that exhibits which native pin of the ATmega8A corresponds to which Arduino pin quantity; we’ll come to it later.

Lastly, all sketches and steps on this article have been written w.r.t. Arduino IDE v1.8.13. These steps could change with main revisions within the IDE, and I’ll attempt to maintain the article updated so far as potential.

Getting ready the Arduino board

  1. Plug the Arduino into your laptop through the USB cable, and ensure the Arduino IDE can detect it (go to Instruments ##rightarrow## Get Board information; if nothing is proven, the IDE tells you to pick out a port, and the Ports menu is greyed out, your Arduino has not been detected by your laptop).
  2. Open the ArduinoISP sketch: Go to File ##rightarrow## Examples ##rightarrow## ArduinoISP ##rightarrow## ArduinoISP.
  3. Be certain the board definition within the IDE is about as per your Arduino board (Instruments ##rightarrow## Board) and the programmer is about to AVRISP mkII (Instruments ##rightarrow## Programmer).
  4. Add the ArduinoISP sketch to your Arduino.

Putting in the board definition for ATmega8A

We will use the MiniCore board definition by MCUdude. On this article, we’re utilizing v2.1.3, which is the most recent on the time of writing. To put in the board, please observe the steps within the GitHub web page.

Establishing the circuit

At this juncture, you must first examine the pinout diagram close to the underside of the README.md of the MiniCore GitHub repo. The pinout diagram exhibits which native pin of the ATmega8A corresponds to which Arduino pin. When writing your supply code for the ATmega8A, you’ll use these Arduino pin numbers to confer with the pins. The pinout diagram may even enable you to in wiring the {hardware} later.

With a purpose to program the ATmega8A, we will be connecting the MOSI (Grasp Out – Slave In), MISO (Grasp In – Slave Out), and SCK (Serial Clock) pins on the ATmega8A to the corresponding pins of the onboard MCU. These pins will permit the onboard MCU to speak with the ATmega8A.

As now we have said earlier, the mapping from the pins of the onboard MCU to the pins on the Arduino board differs from one board to a different. Therefore, we will be utilizing the ICSP header pins on the Arduino board, the pinout of which shall be common and invariant throughout Arduino boards. Observe that boards like Uno and Mega (which have a separate chip for USB communication) have two units of ICSP headers. We’ll use the headers for the primary microcontroller, and these typically have the label “ICSP” beside them.

The headers have the next pinout:


ICSP headers pinout


The pinout of the ICSP headers can be proven within the ArduinoISP sketch within the feedback.

The complete circuit, connecting the Arduino to the ATmega8A, is proven within the following diagram:

Circuit for programming ATmega8A with Arduino

A couple of factors to notice:

  • We do not use the ##mathtt{RESET}## pin of the ICSP header. We now have related the ##overline{mathtt{RESET}}## (pin PC6) of the ATmega8A to pin 10 of the Arduino board and not to the ICSP header. The ##mathtt{RESET}## pin of the ICSP header corresponds to the ##overline{mathtt{RESET}}## of the on-board MCU. Resetting the ATmega8A and the onboard MCU are two completely different tales and aren’t associated to one another. We should always be capable to reset the ATmega8A independently after we are programming it. To this impact, the ArduinoISP sketch, by default, makes use of the digital pin 10 on the Arduino board. Therefore, regardless of the board you might be utilizing, the ##overline{mathtt{RESET}}## of the ATmega8A will at all times be related to digital pin 10 on the Arduino board. You could change this pin as per your want by modifying the ArduinoISP sketch.
  • I’ve used an ##11.0592~mathrm{MHz}## crystal. You may select any crystal from this checklist.
  • Whereas burning the bootloader or importing a sketch, it’s a must to compulsorily energy the ATmega8A from the Arduino board. You could use the 5V and GND pins on the ICSP header, or instantly connect with the 5V and GND sockets on the Arduino board. In case you are prototyping on a breadboard, then you might join the ability rails to the Arduino board.

Burning the bootloader

After you have arrange the circuit, as proven above, you might be able to burn the bootloader.

  1. First, it’s a must to select the right board definition. Within the Arduino IDE, go to Instruments ##rightarrow## Boards ##rightarrow## MiniCore ##rightarrow## ATmega8.
  2. Set the programmer to Arduino as ISP (MiniCore) [Tools ##rightarrow## Programmer ##rightarrow## Arduino as ISP (MiniCore)].
  3. Select the clock (crystal) you may have used (Instruments ##rightarrow## Clock ##rightarrow## …).
  4. The opposite choices could also be as follows:
    • BOD: ##2.7~mathrm{V}, ##
    • EEPROM: EEPROM retained,
    • Compiler LTO: LTO enabled
    • Bootloader: Sure (UART0).
  5. Go to File ##rightarrow## Preferences ##rightarrow## Settings tab ##rightarrow## “Present verbose output throughout…” and choose each “Compilation” and “Add” checkboxes. It will enable you to troubleshoot any errors that will come up.
  6. Click on on Instruments ##rightarrow## Burn bootloader.

If every thing goes properly, there needs to be a message within the IDE: “Achieved burning bootloader“; if there are errors, scroll via the article to the “Coping with runtime errors” part. You probably have related the LED to the SCK line of the ATmega8A as proven within the circuit diagram, it ought to begin blinking after this if the burn succeeds.

Importing sketches to the ATmega8A

To add any sketch,

  1. Be sure you have chosen the right board definition. The underside-right nook of the IDE ought to present you the chosen board choices.
  2. Compile the sketch.
  3. On the Arduino board, join a ##2.2~mathrm{mu F}## capacitor between the RESET and GND pins. It will ensure that the on-board MCU stays reset throughout the add course of. (In case you are utilizing the Uno board, you might detach the on-board microcontroller by eradicating it from its IC base.)
  4. The circuit would be the identical because it was while you had burnt the bootloader.
  5. Go to Sketch ##rightarrow## Add utilizing Programmer.

Check with a Blink sketch

Now you might be able to add sketches to the ATmega8A. We will take a look at with the next Blink sketch, which is akin to the “HelloWorld” program.

void setup() {
    pinMode(17, OUTPUT);

void loop() {
    digitalWrite(17, HIGH);
    digitalWrite(17, LOW);

Observe that digital pin no. 17 of the ATmega8A corresponds to port PC3. It is best to be capable to find this pin on the pinout diagram given within the MiniCore repo.

Join a LED with a resistor between digital pin 17 of the ATmega8A and GND, and add the sketch. If every thing goes high-quality, the LED ought to begin blinking. In case you disconnect and reconnect energy to the ATmega8A, the LED ought to begin blinking once more. Congratulations! You’ve gotten efficiently uploaded your first sketch to the ATmega8A.

Suitable microcontrollers

Though now we have used the ATmega8A on this article to display the process, you may program many different microcontrollers in a similar way. MCUdude has written many different board definitions like MicroCore, MajorCore, MightyCore, and so forth. The checklist of supported microcontrollers is given within the GitHub repos. For example, MiniCore helps not solely the ATmega8A, but in addition ATmega48, ATmega88, ATmega168, ATmega328, and ATmega328PB, and these might be programmed equally. For different microcontrollers, be sure you examine the pinout and minimal setup diagrams and modify the circuit accordingly.

Some factors to remember

  • As soon as a sketch is uploaded, you might disconnect all pins from the Arduino board. You could present the ability supply to the microcontroller externally with out utilizing an Arduino (like a 9V battery with a LM7805 voltage regulator, or instantly from the mains AC utilizing an AC-DC adapter).
  • Not all Arduino libraries have been carried out in different microcontrollers. While you compile your sketch, you’re going to get errors if the library you might be utilizing just isn’t carried out.
  • Microcontrollers have various sizes of flash reminiscence. In case your sketch requires extra reminiscence than is offered, the compiler will throw an error. Observe that the bootloader makes use of 513 bytes of flash reminiscence.
  • You could use an inner clock and thereby acquire two further pins. However except needed, it’s preferable to make use of an exterior clock.
  • By default, LTO is disabled within the MiniCore board definition. This has been performed to take care of backward compatibility with earlier variations of the IDE. It is strongly recommended to allow LTO, which might shrink your code and scale back its dimension on the time of linking. In some board definitions by MCUdude, LTO is enabled by default. Observe that if you wish to change the LTO choice, it isn’t essential to burn the bootloader once more. You could merely change the choice and compile your code once more.
  • It isn’t essential to burn the bootloader each time you want to add a sketch. It’s, nonetheless, essential to burn the bootloader once more in case you wish to change any of the next choices:
    • BOD
    • Clock frequency
    • EEPROM choice

Coping with runtime errors

There are two kinds of errors that you could be incessantly encounter whereas importing a sketch or burning the bootloader:

  1. Machine signature invalid, and
  2. Timeout after no response from the microcontroller.

The most typical purpose for these errors is connections. Test when you have a free connection someplace, and confirm that you’ve got arrange the circuit correctly. Test connections for continuity with a multimeter. Generally, the IC could not have been firmly hooked up to the breadboard, so verify that too. Whereas importing a sketch, be sure you join the capacitor between RESET and GND within the Arduino board.

That’s all for this text. Thanks for studying via it, and I hope you’ll find it useful.

I wish to take this chance to thank Greg Bernhardt, the creator, and admin of Physics Boards, for continually motivating us to jot down articles within the Insights Weblog and offering us with a platform the place we are able to share our data. I additionally thank the Mentors of Physics Boards for his or her dedication and exhausting work (which is totally voluntary), with out which the web site couldn’t have grow to be what it’s right now.

Questions, feedback, and recommendations are at all times welcome.



Please enter your comment!
Please enter your name here