Microcontroller Tutorial 3/5: How To Design a Microcontroller Circuit

In this tutorial I will teach you how to build your own microcontroller circuit. This way, you can easily add microcontrollers to your own projects. We are now at part three of this tutorial.

In part one of the microcontroller tutorial, we looked at what a microcontroller is. We saw that a microcontroller is like a small computer, and that you can use it to build amazing things like cell phones or even your own handheld game-console.

Then in part two, we looked at different types of microcontrollers, and we chose one for our purpose. We chose the ATmega32U2 because we can program it through USB and it is reasonably easy to solder at home.

In this third part of the microcontroller tutorial, we are going to design our circuit from scratch. So we need to design a schematic diagram with all the components and connections necessary to make our circuit work.

Let’s build this thing!

What Do We Need?

How do we decide which components to connect to our microcontroller?

Let’s think about it. We need power for the chip, otherwise it won’t work. And we need a USB connection, since we are going to program it through USB. And we need some physical pins where we can easily connect things to our circuit and test it.

So, we need:

  • Power
  • USB Connection
  • Pin Connections

The Datasheet

ATmega32U2 Datasheet FrontpageTo figure out exactly which components we need, and how to connect them, we look in the datasheet of the chip.

The datasheet is a comprehensive document with a lot of technical data on how the microcontroller works and how you can control different parts of it.

If you are not used to reading a datasheet, it might feel a bit overwhelming. But after looking at a few datasheets, you will sooner or later start to understand how it is laid out.

It is not necessary to read the datasheet from start to the end. You only need to read the parts that are interesting for what you want to make.

So if you want to make a timer with your microcontroller, you read the timer section. If you want to use the UART, you look up the UART section.

One thing to notice – in the datasheet of the ATmega32U2, the table of contents is placed at the end.

Power and USB

We can choose if we want the circuit to be powered by the USB cable or with an extra power cable.

To keep things simple and with less components, let’s make this a USB powered device. This means it will only work when it is connected to the USB.

So, what does this mean? Which components do we need?

To find out, let’s look in the USB-section of the datasheet, and see what we find.

USB Module Powering Options

From the table of contents at the end of the datasheet, we find a section called USB Module Powering Options at page 186. Here we can find the following figure that show us how to connect the USB section of the chip to make it powered by USB.

USB powered microcontroller
How to connect the ATmega32U2 as a USB powered 5V device. (Source: Atmel.com)

From Design guidelines on page 189 we learn that the resistors should be 22 Ohm resistors, and that it is also highly recommended to have a 10 µF capacitor on the VBUS line. So let’s add the 10µF capacitor too.

Crystal

Older crystal oscillatorIn the image above, we can see that there is also a crystal and two capacitors connected to the XTAL1 and XTAL2 pins. Why is this?

A microcontroller needs a clock to work. Most microcontrollers have an internal RC-oscillator that creates a clock signal. And so do the ATmega32U2.

But the thing is that the USB part of the microcontroller is not able to run from that internal clock. So to make USB work, we need an external crystal.

Exactly what kind of crystal we need, we can find at page 30 in the datasheet. Here, the capacitor values are also listed.

Pin Connections and LED

light-emitting-diodeWe want to be able to connect stuff to our microcontroller circuit. So, we’ll add 16 physical pins that are connected to the I/O pins PB0-PB7 and PD0-PD7. This makes it easy to connect something to our circuit.

Another thing we should add, is an LED. This way, we can easily check if the circuit and our code works. To control the current through the LED, we also add a current limiting resistor in series with it.

Push-Button for Reset

It is very common to have a reset button on a microcontroller circuit. This makes it easy to reset the microcontroller without having to unplug the USB cable.

From page 47 of the datasheet we can read:
«The MCU is reset when a low level is present on the RESET pin for longer than the minimum pulse length.»

So we need to make sure the reset pin is normally pulled high, and only pull it low when the reset button is pushed.

To do this, we use something called a pull-up resistor. This is a resistor connected between the reset pin and VCC (5 volts) – which “pulls” the reset pin high.

We connect the reset button in a way so that the reset pin is connected to ground (0 volt) if the button is pushed.

A good value for this resistor is around 10k Ohm.

The Complete Microcontroller Circuit Diagram

If we combine all the parts that we’ve discussed above, we’ll end up with the following circuit diagram. You’ll notice that some parts seem to not be connected to the rest. But, when there is a name on a wire, it means this wire is connected to all the other wires with the same name.

Microcontroller Tutorial ATmega32U2 circuit with values

Designing a Circuit Board and Getting it Made

Wow, it’s all coming together now. All we have to do now is to create a circuit board. So, that’s exactly what we’ll do in the next part of the tutorial.

Next up, we need to create this circuit and have it made.

Click here to continue to part 4 in the microcontroller tutorial >>

More Microcontrollers Tutorials

33 thoughts on “Microcontroller Tutorial 3/5: How To Design a Microcontroller Circuit”

  1. nice tutorial,,,,is there any way we can use this component for temperature controller for an electric heater to switch ON/OFF automatically

    Reply
  2. Thank you, I believe its the best thing you contributed.

    How to make a simple radio control for a toy car.

    I really enjoyed the tutorial, it is eye- opener to me as a beginner in this field.

    Yakudala

    Reply
  3. I cannot find the capacitor values on page 36 of the datasheet. Page 186 in the datasheet suggests a crystall of 16MHz if the power supply exceeds 4.5V. Since we have a USB connection with a 5V signal voltage, should we not use the 16MHz crystal?

    I was also wondering about the maximum power of the 22 Ohm resistors that we use for the USB D+,D- connections. USB 2.0 has a maximum current of 0.5A, thus a maximum power of P=U*I=R*I*I=(22*0.5*0.5)W=5.5W. This current would never be demanded by the circuitry, but is there any hint about the current or power that the resistors has to withstand?

    Reply
    • Hey Markus,

      Great questions. I like that you are diving into it properly.

      Looks like crystal and capacitor values are on page 30 now.

      Page 186 suggest a crystal of MAX 16 MHz for 5V. But less is also ok.

      About the max power of the 22 ohm resistors: The resistors are on the data lines. The 0.5 A is the max power from the VBUS line. I’m not sure how much power the resistors need to be able to handle, but since I’ve never seen it specified I assume 1/4W resistors are good enough.

      Cheers!
      Oyvind

      Reply
  4. Is it necessary to memorize what VCC, UVCC, AVCC etc. and to know it’s meaning? it makes me a lit’l confusion about the diagram.

    Reply
    • These power different parts of the chip. You can find information about these pins in the datasheet. But often, they can all be connected to one power source.

      Best,
      Oyvind

      Reply
  5. Is it possible for circuit to be powered by an external power source such as a button battery as well as be programmable through USB?

    Reply
  6. As soon as you(admin) reached at the 3rd part of the tutorial, where you, started talking about the usage of resistors and capacitors, I kind of started getting a hunch of complexity, because I am not good at basics (why, where, how and what type of resistors and capacitors do we use) in multiple applications/circuits.
    If you clear my doubt that I will be very helpful for me to pursue my career.
    Regards
    Dhaval Taneja

    Reply
  7. Great tutorial, I plan on starting it soon.. but my question is, what kind of USB should I use? (Micro, Mini, Type A/B, etc)? Or doesn’t it matter?

    Reply

Leave a Comment