본문 바로가기

사물인터넷

From Arduino Uno to ATmega328 – Shrinking your Arduino Projects

https://dronebotworkshop.com/arduino-uno-atmega328/

 

From Arduino to ATmega328 | DroneBot Workshop

Learn how to take your Arduino Uno projects and transfer them to an ATmega328 chip so you can build a permanent version. In this article, you will learn how to wire and program an ATmega328.

dronebotworkshop.com

Introduction

The Arduino Uno is probably the most popular Arduino board, and for good reason.  It’s relatively small, very inexpensive (especially clone boards) and has a wealth of support code and documentation. It’s also likely the first Arduino board you started working with, for some of you it may even be the only Arduino board you own.

You’ll find that the majority of projects and demonstrations that I perform on this website and on the YouTube channel use an Arduino Uno. It’s also my favorite Arduino board.

But the real value of the Arduino Uno is as a development or prototyping board.  With an Arduino Uno and a solderless breadboard you can quickly bring ideas and concepts to life, its connectors work well with jumper wires and the wealth of code available for the Arduino platform makes development tasks a breeze.

After you finish you’ll have a working project but it may not be a practical one to use in the real world. It has no enclosure and as it consists of an Arduino Uno, a solderless breadboard and various wires, modules, and components. It’s actually pretty fragile.

Now it’s the time to move that project off the breadboard and into something more permanent. And there are many ways of doing just that.

From Prototype to Production

After you finish your prototype you’ll want to give consideration for making a practical, permanent version of it.  You have a lot of choices and the correct one will depend a lot upon the nature of your project.

  • Will your project be used in harsh environments, like outdoors in the rain or snow?
  • Does your project need to be fit into a specific enclosure or existing object (i.e. a toy)?
  • Will it be AC or battery powered?
  • Will you ever need to reprogram or update it?
  • Will you be the only person using it or does it need to be designed so that anyone can use it, regardless of their technical abilities?
  • Will you be mass-producing your project, or is it just a one-off run?

The answers to these questions will determine the best way to take your prototype and turn it into a permanent device.

Once you have determined the design requirements for the finished project you will want to start planning how to build the final product. You might be creating a printed circuit board for your device or, if it is just a one-off run, you may just decide to wire it up on perfboard.

One big thing that you’ll need to factor into your decision is the Arduino Uno itself. It may be perfect for your permanent project, but again, it may not be.

Fortunately, you have several alternatives to using a full-sized Arduino Uno in your project.

Arduino Uno Alternatives

The Arduino Uno is a wonderful device. With a generous helping of digital I/O ports, 8 analog-to-digital converters, I2C, Serial and SPI interfaces and onboard voltage regulation it is suitable for powering a wide variety of devices. It has a built-in USB port and onboard LEDs and can provide both 5-volt and 3.3-volt low-current power supplies for your peripheral devices.

But it’s a bit large. Not huge, but not small either. Yes, it can fit inside an Altoids mint container (the classic DIY enclosure) but you won’t have room for very much else.

It’s not very expensive, but it also isn’t very cheap either. Maybe not a concern for a one-off device but if you are mass-producing them then the cost of an Uno board (genuine or clone) can become a major factor in your devices price point.

Even if you don’t plan to mass-produce your project you may not want to give up your Uno board, especially if you only own one of them.

The Arduino Uno is an excellent prototyping device, and in many cases, it can also be used in a final product. But there are alternatives.

  • The Arduino Nano – This small board can do everything an Arduino Uno can, it even has two additional analog to digital converters. It has a mini USB connector instead of the bulky Type B connector used on the Arduino Uno. Cost wise it’s a bit less expensive.
  • The Pro Mini – This is an even smaller version of the Nano and shares the same features, except for the USB port which it lacks. To program it you’ll need an FTDI adapter. Great when your project will never need reprogramming. It is also less expensive than the Uno and is also available in 3.3-volt configuration for use with batteries and low-powered logic chips.
  • The ATmega328 – This is the solution you’ll be reading about in this article.  It’s the microcontroller that powers the Arduino Uno, and it can be used all on its own.

Each of these alternatives has its own advantages (and disadvantages), there is no universally correct choice.

The ATmega328

The ATmega328 is a single-chip microcontroller with the following features:

  • 8-bit RISC (Reduced Instruction Set Computer) processor core.
  • Runs at clock speeds from 1MHz to 20MHz.
  • 32Kb Flash Memory.
  • 2Kb SRAM (Static Random Access Memory).
  • 1Kb EEPROM (Electrically Erasable Read Only Memory).
  • 23 GPIO (General Purpose Input-Output) lines.
  • 32 general purpose registers.
  • I2C, SPI, and Serial interfaces.
  • 10-bit Analog to Digital converters – 6 in DIP package, 8 in surface-mount package.
  • Internal and External Interrupts.
  • Available in DIP and Surface Mount packages.

The ATmega328 was originally developed by Atmel.  Atmel was purchased by Microchip Technology in 2016 and now holds the patent to the ATmega328.

The original Arduino Uno and its clones used the 28-pin DIP (Dual Inline Package) version of the ATmega328. Other clones and boards like the Nano and Pro Mini make use of surface mount versions, this explains why the Nano and Pro Mini have two additional analog to digital converters.

We will be working with the 28-pin DIP version for our experiments. It’s an ideal chip for most experimenters as it fits nicely on a solderless breadboard and on a perfboard.

Of course, if you are creating printed circuit boards for your project you may also use the surface mount version of the ATmega328 to reduce the size of your project even further.

ATmega328 Pinouts

Let’s take a look at the pinouts of the 28-pin DIP package version of the ATmega328.

As you can see from the diagram above the ATmega328 has several pins that have two, or even three, functions. You can change the functions of these pins programmatically within your sketch, the same is true of the Arduino Uno (which makes perfect sense as the Uno is based upon the ATmega328).

When converting your design from an Arduino Uno to a raw ATmega328 chip it is helpful to be able to relate the pinouts on the ATmega328 to the connections on an Arduino Uno.  The following pinout diagram has been relabeled to show the Arduino Uno equivalent functions:

An example might be helpful to help you equate the pins on an Arduino to those on an ATmega328.  For that reason, I’ve put together a very simple project that can be quickly prototyped on an Arduino Uno. We will then look at what is required to move our simple project to an ATmega328.

Star Wars Arduino Code & Project

For my simple project I decided to build a “music box” with a Star Wars theme!  

The design is very simple and the sketch, which I will show you in a moment, was readily available on GitHub. It took just a few minutes to put the prototype together, but nonetheless, I think it will serve to illustrate how to move code to the ATmega328 quite well.

After building this you’ll realize that using an entire Arduino Uno is a real overkill, as only three digital I/O pins are put to use. It also would lend itself well to being mounted into a Star Wars toy or a custom 3D-printed enclosure.

Star Wars Music Box Hookup

Our “music” box only requires a few components:

  • An Arduino Uno (which we will be replacing with a stand-alone ATmega328).
  • Two LEDs. If you want to use more LEDs you’d need to add a driver transistor to prevent pulling too much current. Otherwise, any two LEDs will suffice, I used a red and green one.
  • Two dropping resistors. I used 220 ohms for each of these but any value from 150 to 330 ohms will work fine.
  • A Piezo Buzzer. This might be the only part that you don’t have in your parts drawer, it’s readily available from Amazon, eBay or just about any electronics store. It is very inexpensive.

Gather together the required components and hook them up as per the following diagram:

As you can see this is a very simple project to hook up. Make sure to observe proper polarity on the LEDs and on the Piezo buzzer.

Star Wars Music Sketch

Once you have the “music box” hooked up you’ll need a sketch to make it work.

I took the easy road and used an excellent sketch that has been on GitHub for over 6 years. It is the Arduino Song sketch by Nick Sort, you can take a look at it here:

<INSERT ARDUINO MUSIC SKETCH>

The sketch is both brilliant and simple. If you are musically inclined you could modify it to play a different song.

The sketch begins by defining constants that represent the musical notes. Each note is given a value that represents its frequency, for example, an “A” is assigned a value of 440Hz (this is “A above Middle C”, a common tuning reference).

If you plan on playing a different song you could extend the range of notes, keep in mind that the piezo buzzer used in the sketch doesn’t have a very wide frequency response.

After defining the musical notes we define three integers to represent the pins that the piezo buzzer and two LEDs are connected to.

Finally, the last definition is an integer used as a counter, this will be used to determine which LED to flash while the tune is playing.

In the Setup section we simply define the I/O pins as outputs.

Before we move on to the Loop it will be helpful to jump down and examine a function that is the key to making all of this work.

The beep function takes two inputs, both integers. The first input is the note as defined at the beginning of the sketch. The second input is the duration of the note in milliseconds.

In the function, the Arduino tone function is used to play the desired note on the piezo buzzer for the desired amount of time.

Next, the counter is examined to see which LED to turn on. The LEDs alternate, when the counter variable is an even number the first LED is illuminated, otherwise the second one is used. The LED is turned on for the same duration of the note and then turned off. The result is that the LEDs follow the music.  

After that the noTone function is called to reset the piezo pin so that it goes silent and is ready for the next note.

A 50-millisecond delay is added to provide a break between notes.

And finally, the counter is incremented by 1 so that ton the next iteration the opposite LED will be flashed.

Now that you understand the beep function understanding the rest of the sketch is pretty easy.

You’ll notice two additional functions, firstSection and secondSection. These functions just call the beep function many times, they each represent a repeatable pattern of musical notes used in our song.

Back to the loop. It looks just like the firstSection and secondSection functions. It just keeps calling the beep function, as well as the two section functions, with the occasional time delay.

This is how we play our song!

Load up the sketch and listen to the buzzer. If you are a Star Wars fan you should notice a rather raspy version of “The Imperial March”, also known as Darth Vader’s Theme.

May the force be with you!

Now that we have our project working let’s see what is involved in rebuilding it with an ATmega328 instead of an Arduino Uno.

Building an “Arduino” with ATmega328

If you look closely an an Arduino Uno board you’ll notice that aside from the ATmega328 there really are not very many components. Most of the “extra” parts have to do with either the USB to serial interface or with the internal 5-volt and 3.3-volt regulators.

In actual fact, you can put together a functional equivalent of an Arduino Uno using only an ATmega328 and five parts. You can even wire on up without any additional parts if you are willing to run it at a lower clock frequency.

But before we get out our ATmega328 chip and start breadboarding we need to discuss one important aspect that we haven’t covered yet – the bootloader.

The Arduino Bootloader

A “bootloader” is code that is burned onto the ATmega328 chips EEPROM. This code is loaded when the processor is powered up or reset and it sets up things like the clock frequency and a number of the internal registers. It also gets the ATmega328 ready to accept programs from the Arduino IDE on its serial RX and TX pins.

The Arduino Bootloader is essentially what makes an ATmega328 into an “Arduino”. It is open source code that is already available in your Arduino IDE, and you can use the Arduino IDE to load a bootloader into a blank ATmega328 chip.

You might not need to do this though. You can purchase an ATmega328 with the bootloader already installed. That is what I did for this project. Look for an ATmega328 chip that has the letters “PU” at the end of the part number, i.e. ATmega328P-PU.

If you do need to burn your own bootloader there are many resources that will show you how, including the one on the official Arduino website. You can also get dedicated devices that burn bootloaders automatically, useful if you have dozens or hundreds of chips to set up.

Personally, I always purchase the chips with the bootloader already burned, they are only slightly more expensive than blank chips and the extra cost is worth it, at least to me it is.

ATmega328 build Hookup

Assuming that you have an ATmega328 with a bootloader burned onto it you are now ready to wire it up. You will need a few extra components for your “homemade Arduino”.

  • A 16MHz crystal.
  • A 10K resistor.
  • Two 22pf capacitors
  • A 10uf capacitor. This is actually optional and is used just for power supply filtering.

The wiring is shown below:

As you can see the wiring is very simple. If you are going to create a printed circuit board for your project eventually the only consideration is that you should try and keep the crystal and two 22pf capacitors reasonably close to the chip.

Of course, you’ll also want to hook up the two LEDs and the piezo buzzer to it, as follows:

  • The LED /dropping resistor connected to Arduino pin 13 is connected to pin 19 on the ATmega328.
  • The LED /dropping resistor connected to Arduino pin 12 is connected to pin 18 on the ATmega328.
  • The piezo buzzer connected to Arduino pin 8 is connected to pin 14 on the ATmega328.

You will also require a 5-volt DC power supply.

Now that we have our ATmega328 wired up we are ready to load our sketch onto it. There are actually several ways to do this, I will cover three of them.

Loading the Program – Using Arduino

You can use an Arduino Uno to load the sketch onto the ATmega328, but only if you have the right kind of Arduino Uno.  Let me explain what I mean by “right kind”:

The original Arduino Uno, as well as several clones, use a 28-pin DIP version of the ATmega328. The same type of ATmega328 that we are going to be programming.

This chip was mounted in an IC socket and thus can be removed. And the ability to remove the chip from its socket provides us with two methods of using the Arduino Uno to program an ATmega328 for our stand-alone configuration.

  1. We can program the chip in the Uno, then remove it to use in our project.
  2. We can remove the chip from the Uno and use the Uno to do the USB to serial conversion to program our ATmega328 on a breadboard.

Of course, if your Arduino Uno clone uses a surface mount chip then you’re out of luck. But fear not, there is a third method of programming an ATmega328 that doesn’t require an Arduino Uno at all.

First, let’s examine the two methods I just listed in a bit more detail.

Method 1 – Using ATmega328 from Arduino

This is the easiest method of them all.

Simply use the Arduino Uno as you normally would and program your sketch onto it.  Then carefully remove the ATmega328 from your Arduino Uno and use it on the breadboard, wired as we have seen earlier.

The ATmega328 holds your sketch in its Flash Memory. So when it is powered up it will start running it, just as if it were in an Arduino Uno.

It really doesn’t get any easier than that. Of course this method, while simple, has a few obvious disadvantages:

  • You now have an Arduino Uno without an ATmega328.
  • The IC socket on the Arduino Uno is not really made for repeated removals and insertions. Eventually, you will degrade or damage it if you do this a lot.
  • You run the risk of damaging the ATmega328 if you aren’t careful.

The last point can be avoided by using a chip removal tool and by observing anti-static precautions. Be very careful to examine the ATmega328 to be sure that you haven’t accidentally bent one or more pins when removing it or inserting it into the IC socket or the breadboard.

Now let’s look at the second method of using an Arduino Uno to program an ATmega328.

Method 2 – Using Arduino as a Serial Connection

The second method also requires an Arduino Uno that has a 28-pin DIP version of the ATmega328. As with the first method you’ll need to remove the processor from the Arduino board. Unlike the first method, however, you will just be putting the ATmega328 aside. After we are done you can put it back into the IC socket.

This method will make use of the USB to serial converter that is built into the Arduino Uno. The outputs of this converter are always available on pins 0 and 1 of the Uno’s digital I/O connecter.  With this method we will just send those outputs to our ATmega328 on a breadboard.

Method 2 Hookup

To start off you will need to wire up an ATmega328 on a breadboard exactly like you did before, with the crystal, two 22pf capacitors, 10k resistor and the (optional) 10uf electrolytic capacitor.

You will then connect the Arduino Uno, without its onboard ATmega328, to your breadboard as shown in the following diagram:

Again remember, although it isn’t shown in the diagram above you need to have all of the other wiring to the ATMega328 as well. This diagram only shows the additional connections you need to make using the Arduino Uno!

What you are doing is tying the RX (receive) pin of the Arduino Uno to the RX pin on the ATmega328 (pin2). You are also connecting the TX (transmit) pin on the Uno to the ATmega328 TX pin (pin 3). And, finally, the Reset pin on the Arduino Uno is connected to the Reset pin on pin 1 of the ATmega328.

Remember, there is already a 10k resistor attached to pin 1, you need to leave that in place.

You can use the 5-volt output and Ground connections on the Arduino Uno to power your breadboard, you don’t need a seperate power supply.

Programming Method 2

There seems to be a lot of conflicting advice on the web regarding how you setup the Arduino IDE to program your ATmega328 using this method. Some suggest that you change the board type.

I have not found that to be necessary. I just leave my Arduino IDE set exactly as it was when programming my Arduino Uno normally. And it worked every time.

So once you have this setup load your sketch up to the ATmega328 and you should be good. In my case I loaded our Star Wars Music sketch up to the ATmega328 and it worked just fine.

When you are loading your sketch you should notice that a few of the LEDs on the Arduino Uno wil blink, just like they do normally. It’s a good indication that everything is working properly.

This method has many advantages over the first one, the main advantage being that you can still use your Arduino Uno after you are done.

But what do you do if your Arduino clone uses a surface mount chip, or if you just don’t want to pull the chip out of the socket on your board? Let’s look at another method that doesn’t use an Arduino Uno at all.

Loading the Program – Using FTDI Adapter

The third, and final, method we will look at for programming an ATmega328 requires a couple of additional components.

  • An FTDI adapter.
  • A 100nf capacitor.

The first of these components requires a bit of explanation.

FTDI Adapter

The FTDI Adapter is a USB to serial convertor that is commonly used to program an Arduino Pro Mini and other microcontrollers that do not have a USB port. The “FTDI” abbreviation comes from the original manufacturers’ name, a Scottish company called Future Technology Devices International.

These adapters allow you to convert USB level signals to TTL level, which is usually 5-volt logic levels. Some designs also allow the use of 3.3-volt logic, usually selectable using either a jumper or a solder pad on the circuit board.

I show a couple of these adapters in the above image. One interesting thing about the two adapters I’m showing here is that the connectors on them are wired backward to each other, in other words one of them is actually upside down!

When using your FTDI adapter pay attention to the markings on the connector to be sure that it is connected correctly.

FTDI Adapter Wiring

The following diagram shows how you connect an FTDI adapter to your breadboard with an ATmega328. Once again I’m not showing the ATmega328 breadboard components but remember they still need to be there!

Note that the DTR (Data Terminal Ready) lead on the FTDI adapter connects to the Reset pin (pin 1) on the ATmega328 through a 100nf capacitor.  If your FTDI adapter does not have a lead labeled RTS then look for one called DTR (Data Terminal Ready), in this case it should perform an equivalent function.

Once again remember to leave the 10k resistor that is already attached to poin 1 on the ATmega328 in place, as well as all the other ATmega328 components.

Also, note that unlike the previous method which used the Arduino Uno the FTDI method requires you to reverse transmit and receive. So the RX output from the FTDI adapter connects to the TX pin (pin 3) on the ATmega328 and the TX output from the FTDI adapter is attached to the RX pin (pin 2) on the ATmega328.

Again you may use the VCC and GROUND outputs from the FTDI adapter to power your breadboard instead of using a seperate power supply.

Programming with the FTDI Adapter

Once again I found I didn’t need to make any changes in my Arduino IDE, it just worked as it always did. One thing to note however is that the computer in my workshop is LINUX (Ubuntu 16.04) and if you are using a Windows computer you may need to install a driver to use the FTDI adapter.

Otherwise I kept the board type as an Arduino or Genuino Uno and it worked correctly.

You can observe the LEDs on the FTDI adapter when you upload code to it, you should see some activity here. And your ATmega328 should work as it did before.

This method of programming an ATmega328 has a lot of advantages once you get it working, the primary one being that it is completely independent and does not require you to modify an Arduino Uno.

Conclusion

The ATmega328 chip is an ideal solution when you want to make a permanent version of your latest design.  If you are planning to mass produce your design it is a perfect method to reduce both size and cost.

I hope you enjoyed this tutorial. I’d be interested to hear how you have taken your Arduino Uno designs and “shrunk” them down onto an ATmega328.

 

Resources

Arduino to Breadboard – The official instruction from Arduino for burning a bootloader and putting an ATmega328 chip on a breadboard.

Arduino Star Wars Sketch – The original sketch by Nick Sort on GitHub.

Microchip – Manufacturers of the ATmega328.

Future Technology Devices International – Developers of the FTDI adapter.