Welcome back to beyond Arduino. In the previous two entries, I talked about how certain peripherals of AVR microcontrollers work. And while these simulator boards are all good and fun, you should use an actual AVR microcontroller eventually. And since this series is titled “Beyond Arduino”, you most likely have an Arduino board yourself, which runs using an AVR microcontroller. No worries if you don’t though, you can follow along with the video and get into AVRs without an Arduino. Anways, the point of this video is to show you how you can take the rather bulky arduino boards and remove all of the unneccesary components, leaving only what you need. This video will also show you how you can program these microcontrollers without relying on any of those USB to UART converters or even a bootloader. So, in hindsight, this could have been episode one. Either way, let’s get into it.
The first question you may be asking is why we would even want to develop our project outside of Arduino hardware and software? Well, the first thing I will say that there is nothing wrong with using Arduino, and it comes with a long list of advantages, such as incredible library and community support. The reason why I am teaching you how to operate outside the Arduino ecosystem is so that you can get a better understanding of how microcontrollers work and allow you to use devices other than the ones that Arduino supports. You may one day have a reason to use some sort of underground micrcontroller without any Arduino support. Or you may need to shrink your project by using an ATtiny. And what better way to start than with a microcontroller that already comes with many Arduinos.
Let’s take a look at the anatomy of an Arduino Uno. Unfortunately, I don’t have an official Arduino Uno, but I do have this clone board that I made when I was first starting out in electronics. It’s functionally the same, so you should still be able to follow along. The first thing you will notice is this large IC. This is an ATmega328. And lucky for you, it is basically the most famous microcontroller thanks to the Arduino using it. Some of you may have an Arduino Uno that is slightly different in that instead of this large DIP chip, you have a smaller SMD chip. And if you read closely, yes they are the same ATmega328. Unfortunately, this will be much more difficult to use. So unless you have the skills and tools to remove this, I recommend that you just buy it in the DIP package. Although they are hard to come by these days. I’ve found that a suitable alternative would be the ATmega8A, if you really want to get one now. The same goes for everybody else who doesn’t have access to an Arduino like this. I’ll put a link to one in the description if you want to buy one. Anyways, for those of you who do have an Arduino like this, you can take something thin, like a screwdriver or pencil, and push the chip up from the bottom. And congratulations, you now have a bare ATmega328.
The next logical step is to place the IC onto a breadboard. The best place to start is with the device datasheet. The obvious pins are VCC and GND. AVCC is the VCC for the analog portion, so we will just connect it to VCC for now. We should also put a 10k resistor between Reset and VCC. If you did take the AVR from the arduino, you will need to add a 16 MHz crystal on the XTAL pins, along with two 20pF capacitors. Beyond that, the first thing you may notice is that the pins are not labeled the same as they are on an arduino. For example, instead of pin 13, you’ll find PB5. This is because the numbered pin system is specific to arduino. Just something to keep in mind. For now, we need to make sure that our setup is actually functional by programming it. Many of you may simply program it through the Arduino dev board, but that isn’t always perfect. You may not have that option: your microcontroller might not have the boot loader, or you may need to change the device’s fuses.
Your selection of programmer can get pretty complicated pretty fast, depending on what you need it to do. So I’ll try to keep it simple for everybody. For programming you have three options: the first is the one found in the Arduino which is the bootloader method, which has the weakness of needing the bootloader already installed and it also cannot program fuses. The second is the high voltage programming method which is where the programmer intiates which a 12 volt pulse on the RESET pin. This is by far the most versatile programming method, since you can use it to program everything, including fuses. It also does not depend on the system clock, so it is useful for cases when you accidently “lock youself out” when using another programming method. Its only drawback is the need for both high-voltage and also the very large pin count. This makes it impractical for use inside of an already existing circuit. The final and arguably most useful method is the ICSP programming method. This stands for in-circuit-serial-programming. This method uses the ISP protocol to program the microcontroller. It can program fuses as well. This method’s main drawback is that you can accidentally lock yourself out of the microcontroller. This can be caused either by selecting an inappropriate clock source or by disabling the RESET pin. And the only way to rescue the microcontroller would be to use the aforementioned high-voltage programming method. I will be showing you how to program using the ICSP method since it is the most practical. Stay tuned for the high-voltage programming method though, because I will show you how to do that in a future video.
Anways, how will we transfer our code on the computer through ISP to the microcontroller? Well, for that you will need to get an ICSP programmer. Nowadays, I use this atmel ice programmer because of its versatility. It can program not only the ICSP method we were just talking about, but also UPDI on the new AVR series and also the 32-bit SAM microcontrollers. If you can afford it, I do recommend this one. Although I am aware some of you will not buy something like this, so I am going to offer you a couple alternatives. The avrisp mkii served me well for a long time, and I do recommend it. Microchip no longer officially produces this, but there are so many cheap reproductions on places like aliexpress that it is still fairly avaliable. Going further, you can use an Arduino as an ISP programmer. This method is more prone to error so be prepared if you are going down this route. In all, I recommend either the atmel ice or the avrisp mkii.
You will now need to connect your programmer. The pins involved will be VCC GND, SCK, MOSI, MISO, and RESET. The atmel ice comes with this squid connector, so you can follow along with this table of pins to connect it to your microcontroller. The avrisp mkii does not come with such a connector, so we will have to discern this 3x2 header. Luckily, my avrisp mkii comes with this neat diagram, feel free to pause the video and follow along with it. And if you are wondering where those six pins I talked about were, well we can return to the datasheet to find them. Here they are, the designations are inside of the parenthesis. It is also worth nothing that these two programmers don’t power the circuit themselves, so you will have to power it on your own. Anways, we should now upload an example to make sure everything is working.
I will create the classic blink example. Connect an LED from PB0 to ground. To make this work, you will first need to install the proper tool chain to compile the code. You have a couple of options here, depending on which operating system you are using. Most of you are probably using windows, in that case I recommend that you go to microchip.com and download microchip studio, formerly atmel studio. The installation process should be relatively straightforward and guide itself through it. Afterwards, create a new C project using GCC C. For Linux, you should install two packages through your package manager, avr-gcc and avrdude. Make a new directory and open a new file called main.c. For both systems, the code itself will be the same. Here is the blink example we will be using. I won’t explain the code here, but instead you should go watch part one of this series where I explain how the I/O works.
To upload the code, make sure you plug in your programmer and give power to the microcontroller. Now go to your computer and compile it. For microchip studio, press either the green arrow or F5 to compile everything. You should now go to this menu, where you can select your programmer. Upload it and you should be done. The linux version is different. Run this command: avr-gcc -mmcu=atmega328p -Os -o exe.elf main.c. This will compile the code for the atmega328p using a 16MHz clock, optimized for size into a file named exe.elf. Now this elf file is not in the right format, instead we need to convert it to a hex format. To do that we use this command: avr-objcopy -O ihex -R .eeprom exe.elf out.ihex. The R flag tells the program to leave out any information involving the eeprom section of memory. We are now finally ready to upload. Run avrdude with this command: avrdude -p atmega328p -b 19600 -c avrispmkII -U flash:w:out.ihex:i. The -U flag here specifies where our data is going, and we told avrdude to send it to the flash in hex format. Either way, your LED should now be blinking.
There is one more thing I want to teach you about microcontroller programming for AVRs, and that is the idea of fuses. They basically represent certain device options that can’t be specified by the code itself. The most common use of which is to change the clock speed. Let’s change our clock source from its 16MHz crystal to the internal resonator to reduce part count. Since changing the fuses on the microcontroller is a bit risky, I recommend you use a calculator such as the one I am using. It allows you to select which options you want to select and it will give you the resulting avr-dude flags. Like the calculator says, be very careful changing the options with an asterisk since they could lock you out from ISP programming. As for the calculator, I will put a link to it in the description. To upload the fuses either use microchip studio or avr-dude. For avr-dude, the command is the same as before, but you simply replace those -U flags. Afterwards, we can remove that crystal.
Well, there you have it. A full guide on how to transfer from using the Arduino ecosystem to programming your microcontrollers on your own. The best part is that these skills will have overlap when you use other types of microcontrollers in the future. You can see my other videos for ways that you can program more than just a blinking LED. Anways, I hope you enjoyed this video and learned something new. If you did, please consider subscribing so that you can see my other videos. Have a good one.
My Website: https://sinelab.net
Fuse Calculator: https://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega328p
Microchip Studio: https://www.microchip.com/en-us/tools-resources/develop/microchip-studio
ATmega328p Datasheet: https://sinelab.net/pdf/ATmega328p_datasheet.pdf