Practical Electronics For Makers

All about Arduino and electronics

Arduino: Just Say No To Shields

Background

I only recently bought an Arduino Uno knock-off board because they finally became inexpensive enough (less than $12 USD).  I'm very cheap.  

Originally when I started with the Arduino (really the AVR ATmega328p-pu) I wanted to be closer to the metal because I thought it would allow me to learn more about what was going on.

I read the book,  AVR Programming: Learning to Write Software for Hardware 1st Edition[^] by Elliott Williams.  That book describes how to program the ATmega328 (and ATmega168) chips directly.  

A Lot of Work

It shows you how to hook up an AVR ISP programmer to a breadboard and how to use a text editor and AVR-GCC.  You write all your programs in straight C.

It's actually a lot of work.  It's a lot more work than you have to do when using the nice Arduino IDE that does everything for you.

You Learn A Lot

However, I also learned a lot about what was going on.  I also learned how to flash a HEX file (AVR machine language file) directly to the ATmega microcontroller using AVRDude.

It was all great information because it put me in a place where I was less dependent upon the Arduino infrastructure.   

Arduino Uno Built-in Headers

Of course the Arduino UNO has those nice built-in headers that make it so easy to wire in your additional components.  For your first Blink program you can literally stick an LED directly into the headers without any other wiring -- yes the 5V is a little high and will overdrive the LED but as an easy first experiment, it is nice.

The Arduino Uno even goes one step further and offers shields you can just plug in directly into it.

What Are Arduino Shields?

If you haven't seen these shields before they are prefabricated extensions you can just plug directly into your Uno that provide functionality.

Here's a good example of one that provides Internet connectivity to your Arduino.

Image from Arduino.cc

Plug It In And Go

You can easily just plug that board into your Arduino Uno's headers and go.  That is really great for prototyping.  However, it is not so great for building out your final product.

Why Shields Aren't Great

Shields aren't great for your final product for a few reasons:

  1. Larger footprint: They make your final product much larger than they would necessarily be.  That's because there is quite a bit more hardware here and it is spread out for ease of use.  Your final product could be far smaller with just the ATmega chip as I showed in my previous article.
  2. Cost: They make your final product cost more.  The ATmega328p-pu only costs about $2. However, the Arduino Uno will cost you anywhere from $12 - $20.  That's a huge difference in price.

Prototype Shields Take This Idea Even Further

Prototyping shields allow you to build your own circuit on a shield that you can easily plug into your Arduino Uno.  

 Image from digikey 

As you can see, you can build your custom circuit on the prototype shield and plug it into your Uno.  However, this means your final project will require that you commit your Uno (which you are really using to provide a programming interface to your ATmega chip) as a part of your project, never to be used in other projects again.  

Break Out Your ATmega Chip

If you'll learn to break out your ATmega chip to the breadboard then you won't have to do this and you'll save money and space.

Here is a great example of a very interesting music synthesizer project that is far larger and far more expensive because it is built around the Arduino Uno and a prototype shield.  Imagine how much smaller and cheaper that project would've been.  

Design Considerations

All of this works into things that we have to think about whenever we design a project.

  1. Footprint - how much space will the final project take up (smaller the better)
  2. Cost - again, the lower the cost, the lower price we can sell the final product
  3. Time - The less time it takes to build, the better.  The shield may save us time up front and if you are simply trying to get to a prototype it may be a great idea. But later you may spend quite a bit of time reverse engineering it into a basic circuit that has a lower cost.

Keep on building, keep on learning.

~ Roger Deutsch

Arduino Uno : Using Your ATMega328p-u As Stand-alone

If you've spent any time at all with an Arduino the you know how to write a program for it.  I uploaded the program (shown below) to simply blink an LED on digital pin 5 on my Arduino Uno (knock-off) board (amazon link).

arduino uno

setup(){
 int LED = 5;
pinMode(LED, OUTPUT); } loop(){
delay(500);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
}

We use the Arduino Uno to program the ATmega328p chip that is connected to the board on the IC (integrated circuit) socket. 

Once You're Done Programming the Chip

But, after you're done programming the chip, it doesn't make sense that you keep it attached to the Arduino Uno board.  The board takes up a lot of space and is really used to provide you with an easy way to program the chip.

Here's what it looks like when you remove the chip from the Uno.

You can see the socket that I removed the chip from and you can see that the chip itself is far smaller than the board.  That means your final product can be much smaller.

atmega328p

But, how do we get the chip to run our program? 

Build the Circuit to Power the ATmega328p

 Here's a quick snapshot of the initial circuit we are going to build.

bread board circuit

It's very simple. It's one GND (ground) wire connected to pin 8 of the ATmega328p.   It's one 5V wire (coming off bread board rail) to pin 7 of the ATmega328p.  

Then you can see I've connected the anode (positive) pin of the LED to PD5 of the ATmega328 because that is the same pin as digital pin 5 on the Arduino Uno.

That _should_ be all you need.  But as you can see, the LED is not lit up.

The ATmega328p is actually powered in the picture, but for some reason it is not running our sketch.  Why not?

How Is The ATmega328p Set Up On Arduino Uno?

This all has to do with the way the ATmega328p is set up to work on the Uno.  When the manufacturer sets up the chip for use with the UNO they have to set some internal switches on the ATmega328p called fuse bits, so that it works properly with the associated Arduino Uno hardware.

These settings are conventions (arbitrarily chosen but are expected by Arduino users).  

You cannot set the fuses unless you have an AVR(Atmel) ISP (In System Programmer/Programming).  

ATMega328p Can Run At Various Speeds

An ATmega328p can run at various clock speeds.  It can run at 1Mhz (one million cycles per second), 8Mhz and 16Mhz.  However, it can only run at 16Mhz if it has an external crystal attached to it.

16Mhz Crystal On Arduino Uno Board

The Arduino Uno board does have a 16Mhz crystal on board.  

I've highlighted the crystal in the following picture.

16Mhz crystal

 Since thee manufacturer has set these chips to run with the external crystal (oscillator), the chip will not run properly without one.

ATmega328p Datasheet

If you take a look at the datasheet and go down to section 5.1 you'll see a nice pinout diagram for the ATmega328p-u (dip dual inline package).

atmega28p-u pinout

Crystal Pins

You can see that pin 7 is VCC and pin 8 is GND just as I mentioned earlier.  But now we need to find the place where we should install the crystal.  If you look closely you'll see pin 9 is marked xtal1 and pin 10 is marked xtal2.  xtal is the abbreviation for crsytal.  

Two 22pF Capacitors

However, by looking at this diagram you would not know that you also need to add two 22pF ceramic (non-polarized) capacitors to those pins also.

You will connect one pin of one cap to the pin 9 and the other pin to ground.  You will then connect one pin of the other cap to pin 10 and the other pin to ground.  Then you will connect the 16Mhz crystal so that one pin is on pin 9 and one pin is on pin 10.

Here's a quick snapshot of it on the breadboard (and then at the bottom of this article, I'll provide you with a schematic so you can more easily see how things are connected).

atmega28p-u with crystal on pins 9 & 10

Now, when you power this circuit up, it will actually run the sketch.

atmega28p-u running stand-alone

Clears Up A Few Things

 Hopefully this will clear up a few things:

  1. You can actually run the chip stand-alone -- and you should since it is a smaller footprint.
  2. If you haven't been successful getting it to work, it may be because you didn't add the 16Mhz crystal: it won't work without it.
  3. You definitely need 22pF caps.  I tried it what 47pF and it didn't work.  Use 22pF caps.

Keep on building, keep on learning.

~raddevus (roger deutsch)