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:
- USB Connection
- Pin Connections
To 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.
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.
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
We 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 volt) – 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.
Which value should this resistor have? A good value is around 10k Ohm. Sparkfun has a good article on how to choose a value for the pull-up resistor.
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 seems 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.
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.