In case you are fascinated by programming and electronics, you most likely don’t want an introduction to Arduino. If you wish to make your Arduino initiatives everlasting, then it’s a good suggestion to make use of solo microcontrollers moderately than Arduino boards within the ultimate setup. Whereas Arduino boards are nice for prototyping, shopping for an Arduino board for each mission is probably not very budget-friendly. Single microcontrollers typically price lower than a full Arduino board. In some circumstances, one might have multiple microcontroller for his or her mission, and in that scenario, utilizing two Arduino boards is probably not the most effective answer. The power to program microcontrollers with Arduino will enable customers to jot down supply code in C after which merely add it to the MCU.
On this article, I’ll present you methods to program an ATmega8A microcontroller utilizing Arduino. I’ll assume that you’ve got some primary expertise in Arduino. With out additional ado, let’s get began.
{Hardware} Necessities
- An Arduino board (boards with ATmel AVR microcontrollers, like Uno, Mega, and many others.; ##3.3~mathrm{V}## boards like Due or Zero usually are not most popular) with a USB cable,
- ATmega8A microcontroller,
- A breadboard,
- Two ##22~mathrm{pF}## capacitors,
- One ##100~mathrm{nF}## capacitor,
- One ##2.2~mathrm{mu F}## capacitor,
- One ##16~mathrm{MHz}## or ##11.0592~mathrm{MHz}## crystal,
- One ##10~mathrm{kOmega}## resistor,
- One ##330~Omega## resistor,
- One/two LEDs,
- Jumper wires.
Terminology and model
I ought to briefly point out the terminology we can be utilizing. You might have an Arduino board (Uno, Mega, and many others.) which itself has a microcontroller (“MCU” briefly). For instance, the Uno board has an ATmega328P, whereas the Mega 2560 board has the ATmega2560. This MCU can be referred to as the “on-board microcontroller” to tell apart it from the ATmega8A.
We can be referring to 4 varieties of pins on this article:
- The pins on the Arduino board. You employ these pin numbers immediately within the Arduino sketch, like digital pin 13 or analog pin A2.
- The pins of the onboard microcontroller. These are linked to the varied pins on the Arduino board, however the mapping differs from one Arduino board to a different. So, when vital, we might immediately check with/entry some pins of the onboard MCU moderately than the corresponding pins on the Arduino board.
- The native pins/ports of the ATmega8A. These are named PCx, PDx, PBx, and so forth. For particulars, check with the pinout diagram within the ATmega8A datasheet.
- The Arduino pins of the ATmega8A. These are the pin numbers you’ll be utilizing in your sketch that can be uploaded to the ATmega8A. There’s a good pinout diagram that reveals 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 might change with main revisions within the IDE, and I’ll attempt to maintain the article updated so far as potential.
Making ready the Arduino board
- Plug the Arduino into your pc 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 a port, and the Ports menu is greyed out, your Arduino has not been detected by your pc).
- Open the
ArduinoISP
sketch: Go to File ##rightarrow## Examples ##rightarrow## ArduinoISP ##rightarrow## ArduinoISP. - Ensure that 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). - 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 newest on the time of writing. To put in the board, please observe the steps within the GitHub web page.
Organising the circuit
At this juncture, you need to first examine the pinout diagram close to the underside of the README.md
of the MiniCore GitHub repo. The pinout diagram reveals 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 check with the pins. The pinout diagram will even show you how to in wiring the {hardware} later.
In an effort 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 enable the onboard MCU to speak with the ATmega8A.
As we now 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 can be common and invariant throughout Arduino boards. Notice 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 principle microcontroller, and these typically have the label “ICSP” beside them.
The headers have the next 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:
A couple of factors to notice:
- We do not use the ##mathtt{RESET}## pin of the ICSP header. Now we have linked 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 totally different tales and usually are not associated to one another. We should always be capable of reset the ATmega8A independently once we are programming it. To this impact, the
ArduinoISP
sketch, by default, makes use of the digital pin 10 on the Arduino board. Therefore, no matter the board you might be utilizing, the ##overline{mathtt{RESET}}## of the ATmega8A will all the time be linked to digital pin 10 on the Arduino board. Chances are you’ll change this pin as per your want by modifying theArduinoISP
sketch. - I’ve used an ##11.0592~mathrm{MHz}## crystal. You possibly can select any crystal from this checklist.
- Whereas burning the bootloader or importing a sketch, you need to compulsorily energy the ATmega8A from the Arduino board. Chances are you’ll use the
5V
andGND
pins on the ICSP header, or immediately connect with the5V
andGND
sockets on the Arduino board. In case you are prototyping on a breadboard, then chances are you’ll 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.
- First, you need to select the right board definition. Within the Arduino IDE, go to Instruments ##rightarrow## Boards ##rightarrow## MiniCore ##rightarrow## ATmega8.
- Set the programmer to
Arduino as ISP (MiniCore)
[Tools ##rightarrow## Programmer ##rightarrow## Arduino as ISP (MiniCore)]. - Select the clock (crystal) you’ve got used (Instruments ##rightarrow## Clock ##rightarrow## …).
- The opposite choices could also be as follows:
- BOD: ##2.7~mathrm{V}, ##
- EEPROM: EEPROM retained,
- Compiler LTO: LTO enabled
- Bootloader: Sure (UART0).
- Go to File ##rightarrow## Preferences ##rightarrow## Settings tab ##rightarrow## “Present verbose output throughout…” and choose each “Compilation” and “Add” checkboxes. It will show you how to troubleshoot any errors which will come up.
- Click on on Instruments ##rightarrow## Burn bootloader.
If the whole lot goes properly, there ought to be a message within the IDE: “Completed burning bootloader“; if there are errors, scroll by means of the article to the “Coping with runtime errors” part. In case you have linked 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,
- Ensure you have chosen the right board definition. The underside-right nook of the IDE ought to present you the chosen board choices.
- Compile the sketch.
- On the Arduino board, join a ##2.2~mathrm{mu F}## capacitor between the
RESET
andGND
pins. It will guarantee that the on-board MCU stays reset throughout the add course of. (In case you are utilizing the Uno board, chances are you’ll detach the on-board microcontroller by eradicating it from its IC base.) - The circuit would be the similar because it was while you had burnt the bootloader.
- Go to Sketch ##rightarrow## Add utilizing Programmer.
Take a look at 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); delay(1000); digitalWrite(17, LOW); delay(1000); }
Notice that digital pin no. 17 of the ATmega8A corresponds to port PC3. You must be capable of 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 the whole lot goes high quality, the LED ought to begin blinking. When you disconnect and reconnect energy to the ATmega8A, the LED ought to begin blinking once more. Congratulations! You might have efficiently uploaded your first sketch to the ATmega8A.
Suitable microcontrollers
Though we now have used the ATmega8A on this article to reveal the process, you possibly can program many different microcontrollers similarly. 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. As an illustration, MiniCore helps not solely the ATmega8A, but additionally ATmega48, ATmega88, ATmega168, ATmega328, and ATmega328PB, and these will 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, chances are you’ll disconnect all pins from the Arduino board. Chances are you’ll present the ability supply to the microcontroller externally with out utilizing an Arduino (like a 9V battery with a LM7805 voltage regulator, or immediately from the mains AC utilizing an AC-DC adapter).
- Not all Arduino libraries have been carried out in different microcontrollers. Whenever you compile your sketch, you’ll 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 obtainable, the compiler will throw an error. Notice that the bootloader makes use of 513 bytes of flash reminiscence.
- Chances are you’ll use an inside clock and thereby acquire two additional pins. However until vital, it’s preferable to make use of an exterior clock.
- By default, LTO is disabled within the MiniCore board definition. This has been carried out to keep up backward compatibility with earlier variations of the IDE. It is suggested to allow LTO, which may shrink your code and cut back its dimension on the time of linking. In some board definitions by MCUdude, LTO is enabled by default. Notice that if you wish to change the LTO choice, it’s not essential to burn the bootloader once more. Chances are you’ll 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, nevertheless, 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 varieties of errors that you could be ceaselessly encounter whereas importing a sketch or burning the bootloader:
- Machine signature invalid, and
- Timeout after no response from the microcontroller.
The commonest purpose for these errors is connections. Test when you’ve got a unfastened connection someplace, and confirm that you’ve got arrange the circuit correctly. Test connections for continuity with a multimeter. Generally, the IC might not have been firmly hooked up to the breadboard, so examine 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 by means of it, and I hope you will discover 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 information. I additionally thank the Mentors of Physics Boards for his or her dedication and arduous work (which is totally voluntary), with out which the web site couldn’t have change into what it’s right this moment.
Questions, feedback, and ideas are all the time welcome.

Finding out physics at a school in Kolkata, India. All in favour of accelerator physics, beam instrumentation and diagnostics, and particle physics detector design. Additionally passionate in electronics and programming in Java, Android, MATLAB and Python.