Tag Archives: MCU Tutorial

Tutorial: Building cool projects with MCUs (Part 4)

I still remember the first time I created my own circuit board. It was such an amazing feeling to be able to see it working. It was actually also a microcontroller circuit. But it couldn’t be programmed through USB, you needed a JTAG programmer. That’s why I’m really excited about the circuit we are building in this tutorial – no programmer or debugger required!

But before we begin, let’s recap: In part one we covered the basics of microcontrollers. Then, in part two we chose a microcontroller for our purpose. Later in part three, we selected which other components we needed, and designed a circuit diagram.

Now, in part four, we are ready to create a circuit board.

How to Create Your Own Circuit Board

The basic process for creating a circuit board is as follows:

  • Design schematics
  • Design board layout
  • Create a board from your layout by etching, CNC milling or using a board manufacturer

We need some design software to do this. There are many available alternatives, but in this post we’re going to use Cadsoft Eagle, which is available for Windows, Mac and Linux. And, another bonus, it’s available as a free version for hobbyists.

Creating Schematics in Eagle

In the previous post, we decided which components to use, and how to connect them. Here is the result:

ATmega32U2 circuit diagram

Now, we need to decide if we want through-hole components or surface mount components, then create a schematic diagram in Eagle.

Through-hole components are easier to solder, but surface mount devices takes less space. Also, they don’t require any drilling, which simplifies the manual work that needs to be done if the board is etched.

I decided to go for mostly large SMD components and a few through-hole components.

Some of the components were not available in Eagle’s default component library; the ATmega32U2 and the USB connector. Instead of spending time making custom versions of these, I did some Googling and found a USB connector in the Sparkfun Eagle library. And I also found a library with the ATmega32U2 chip.

We place all the components in the schematics view of Eagle, and connect them according to the image above.

Designing The Circuit Board

When we have our circuit ready in the schematics view of Eagle, all we have to do is to click the «Board» button on the toolbar to create a board layout from the schematics.

Getting from scheamtics to board layout in Eagle

If you want to learn the exact steps on how to design your own circuit boards from scratch, I’ve written a lot of tutorials on PCB design on my personal blog.

Let’s start by setting our board size. I chose 5cm x 5cm (1.9685 in x 1.9685 in). Why? Because I know it’s possible to find really cheap prototype boards that are this size.

Next, we move all the components onto the board. Since everything is designed around the MCU, we’ll place the MCU in the middle. If we want to etch or CNC mill a board, it’s easier if everything is on one side. Therefore, we’ll stay on the top layer of the board on this design.

Circuit board layout for ATmega32U2 circuit tutorial

You can download the Eagle and gerber files here: Microcontroller-tutorial-files.zip

Getting The Circuit Board Design Made

Now, with a board design ready, we need to make this into a physical board. There are several ways to go about doing this — it can be done through etching, CNC milling or by ordering from a prototype manufacturer.

I usually order prototypes from a PCB manufacturer. This is just so easy and you don’t have to worry about manually drilling holes or inserting vias into your board. Everything is just taken care of. And usually with much better precision than you can expect from etching or CNC milling.

Many people think it’s expensive to order prototypes from prototype manufacturer, but this is not the case. Many offer prices as low as $9.99 for 10 boards. That’s 99 cents per board! And if you choose the cheapest shipping option it will only cost a few dollars.

A great tool for finding the best price is pcbshopper.com. Here you can enter board size and other requirements, along with your country – and you’ll find the best price and delivery option for you.

Ordering Components

It’s not much fun with a circuit board without any components; so, the last step we’re going to do today is ordering the components. There are many available shops online that sells components, including digikey.com, farnell.com, jameco.com and mouser.com.

Most of the components we have used in this tutorial are available everywhere. The only component that might not be in stock everywhere is the ATmega32U2. But Atmel has a great tool to check the inventory of several online shops.

Here are the components used for this board:

Part Description Value Package
C1 Capacitor 1µF SMD 1206
C2, C3 Capacitor 12-22pF SMD 1206
C4 Capacitor Polarized 10µF Through-hole
JP1 USB Connector USB Type B Receptacle Through-hole
JP2, JP3 Header 8 pin Through-hole
LED1 Light Emitting Diode 1.8V Through-hole
Q1 Crystal 8 MHz SMD C49UP
R1, R2 Resistor 22 Ohm SMD 1206
R3 Resistor 200 Ohm SMD 1206
R4 Resistor 10k Ohm SMD 1206
S1 Momentary Switch Through-hole
U1 Microcontroller ATmega32U2 TQFP-32

Next Steps

When we have ordered everything we need, it’s time to sit back and relax! Hopefully it won’t take too long before the components arrive at our door. I am already super excited to start soldering the circuit. However, there is no guarantee it’ll work – we will just have to wait and see in the next (and last) part of this tutorial.

Stay tuned for Part 5 in the coming days…

Tutorial: Building cool projects with MCUs (Part 3)

As we proceed onto the third portion of this microcontroller tutorial, let’s first revisit what we have accomplished thus far.

In Part 1, we defined what a microcontroller actually was. I wanted to get everybody on-board (no pun intended) for this, so I started from scratch. Feel free to jump back there if you need a refresher.

Then in Part 2, we looked at the various types of MCUs. And, it turns out that there were a lot! However, by using Atmel’s selection tool, we managed to narrow it down to a few different ones, before choosing a winner — an ATmega32U2. Why? Mainly because it can be programmed through the USB interface without any extra components or tools, and since it did not have too many pins – so we should be able to solder it at home.

Now, the time has come for us to design the circuit diagram. We’ll start with outlining what we need, then we’ll dive into the datasheet to figure out exactly how we can do this.

Microcontroller in a circuit

What Components Do We Need?

If you’ve never done it before, putting all the necessary components onto the circuit diagram is not a super easy task. Well, the process is not that hard; but until you know how to do it, it will seem a bit difficult. So, let me take you through my thought process:

We need to power the chip somehow. There are some pins on the microcontroller that need power. We have to figure out which pins that need power, and what voltage they need.

We also want to program the chip trough USB, so we need a USB connector and learn how to connect it to the chip.

Then, there is extra stuff. Like maybe an LED to play around with, and definitely some connection pins that we can connect other stuff to later when we want to experiment.

So, we need to figure out:

  • How to power the circuit
  • How to connect the USB part
  • How to connect pins to the chip

Using the Datasheet to Find the Details of Our Circuit

The datasheet for the ATmega32U2 is a must when we are designing our circuit. Here, we can find all the necessary technical details.

We don’t have to read it from beginning to end; instead, we simply look up the parts we are curious about, and study them in more depth. We can find a table of contents at the end of the document.

For instance, if we wanted to use the timer of the MCU, we’d look up the part about timers. If we wanted to use the SPI communication part, we would look that up.

Connecting Power and USB

As you probably know, USB devices can be powered through, well, USB. By doing this, we simplify our circuit and require less components. So, let’s do this.

To figure out exactly HOW we can do this, we’re going to read the datasheet. By going to the table of contents, we find that the section that describes the USB part of the microcontroller is on page 186.

Here, there are different images showing how to connect the USB connector to the microcontroller to make it a USB powered device. For our circuit, we’ll use the bus-powered 5V version:

How to setup the MCU as a USB-powered device

Under Design Guidelines on page 189, we find the exact values we need for the resistors. It’s also there that we learn it’s highly recommended to use a 10µF capacitor on the VBUS, so let’s add that too.

Adding a Crystal

Another interesting thing to note from the image of how to connect the USB, is that there is also a crystal and a couple of capacitors connected to the XTAL1 and XTAL2 pins. This gives us the idea that we probably need a crystal too. But why?

Microcontrollers rely on clock signals to work. Every time the clock signal gives a pulse, something will happen in the processor. Most MCUs come with an internal RC-oscillator that can create this clock signal; however, the USB part of the microcontroller cannot operate from this internal clock — it needs a crystal oscillator to work.

To create a crystal oscillator, we need to connect a crystal and two capacitors to XTAL1 and XTAL2. By looking up “Crystal Oscillator” in the datasheet on page 36, we can also identify exactly what crystal and which capacitors are required.

Connecting an LED And Some Pins

By now, we have everything we need for the microcontroller circuit to work. But, we won’t be able to have much fun with it if we don’t add some connections to the input and output pins. And, it’s also good to add an LED. Everything gets better with an LED!

The LED needs to be connected through a resistor. We call this a current limiting resistor — this is to control the amount of current going through it.

From there, we will need a few physical pins that we can use to connect other stuff to our circuit. There are 22 I/O pins on the ATmega32U2, yet some of the pins are used for other purposes (like XTAL2), so we can’t use them all. For this circuit, we’ll add 16 I/O pins, as it’s a nice and round number.

What Else Do We Need?

On MCU circuits, it’s very common to include a RESET button. This makes it possible to reset the MCU without removing the power connection. This will add a couple of more components to our circuit and isn’t necessary to make it work, but it’s very handy, so let’s add it.

In the datasheet, we can see that pin 24 on the chip is the reset signal. It has a line over itself, which means that it’s activated when pulled low.

To make the reset signal stay in a high-state when the button is not pushed, we’ll add a pull-up resistor. This is a resistor up to VCC. And, we connect the button so that it will pull the reset-pin to ground when pushed.

The pull-up resistor should have a value of around 10k Ohm. For reference, SparkFun has a good article on how to choose a pull-up value.

Our Microcontroller Circuit Diagram

Now that we have figured out the different pieces of the circuit, it’s time to put them all into one circuit diagram. By adding everything into one circuit diagram, we end up with this:

Complete microcontroller circuit

Next, we need to create a circuit board out of this. Creating a circuit board does not have to be very complicated. In the subsequent part of the microcontroller tutorial series, we’ll design and make the circuit board.

Stay tuned for Parts 4-5 in the coming days…

Tutorial: Building cool projects with MCUs (Part 1)

I don’t remember exactly when I learned programming, but I played around with computers from an early age. I remember that it was such an amazing thing to be able to make my own programs. I made games and I made websites. And, it was so fun. When I was around 14 years old, I also started playing with electronic circuits. I made simple circuits of blinking lights and such. Also, a lot of fun.

Then at one point, I discovered microcontrollers. Oh, my God. I realized that by using microcontrollers I could combine programming and electronics to build robots, music players and what-not. It was a shockingly cool discovery. Wow!

electronics-project-with-dev-board-and-7-segment-1024x682

But understanding how to use a microcontroller wasn’t straightforward. There were programmers, EEPROM’s, debuggers, integrated development environments and a lot of different types of microcontrollers. It took me a few years before I started building projects using microcontrollers. Not because it took that long to learn it – but because the whole thing felt so overwhelming. And, I didn’t know where to start. But once I committed to just figure it out, it was actually quite fast to get up and running.

In this five-part microcontroller tutorial series, I will take you from knowing nothing – to being able to build and program a microcontroller circuit from home. To make sure we have everybody on board – let’s start from scratch by taking a look at what a microcontroller is.

Like a Tiny Computer

A microcontroller is somewhat like a tiny little computer. And, simiar to a computer, you can hook up things like a display, a motor, buttons and much more to it. Then, you put programs onto it and run them to make it do something.

OLYMPUS DIGITAL CAMERA

The things you can use a microcontroller for are endless! Among the countless possibilities include building robots, flying quadcopters, music players, or even smartphones.

Learning how to use microcontrollers opens so many opportunities for building new cool projects. Throughout this microcontroller tutorial, you will learn all you need to know, step-by-step, in order to get started with MCUs.

Getting Started With Microcontrollers

Let’s start by taking a closer look at the microcontroller chip. The chip has several pins, most of them input and output pins. The microcontroller uses these pins to interact with the outside world.

Atmel-microcontroller-pinout

However, a microcontroller doesn’t really do anything without getting any instructions. So to make it do something, you need to write a program and load the program onto the chip. This process is often called programming the microcontroller.

In the program, you use code to read from input pins, and to control output pins. By connecting something, such as a light-emitting diode (LED) to an output pin, you will be able to switch the light on and off from your program. If you connect a push-button to an input pin, you can use code to read the pin and see if the button is pushed or not.

Then, you can create code that turns the light on when the button is pushed, and turns the light off when it is not pushed. Not a very exciting example, I agree, but a very good place to get started. Think of it as the “Hello World” of microcontrollers.

We could easily turn this into a much more interesting application.

Connect a temperature sensor to an input pin instead of the button. And keep the LED on the output. Create a program that reads the temperature and if the temperature is above a certain level, start blinking the light.

Now, attach this to your beer, and a light will start flashing to warn you that your beer is getting warm. Now that’s useful!

Programming a Microcontroller

Okay, so how do we get from having this idea of what to make, into actually creating it? First of all, we have to create a circuit, then we need to program the microcontroller.

Creating the circuit is a matter of creating a schematic with the components we need, then turning this into a circuit board. I’ll show you how to do this later in this tutorial.

When you have your circuit ready, you need to program your microcontroller. I found this a bit confusing in the beginning. There are many ways of doing this, and not all methods work on all microcontrollers.

The steps you need to go through in order to get your program onto the microcontroller can be divided into the following:

1. Create program code
2. Compile the code into machine code
3. Upload the machine code onto your microcontroller

These steps will be different for different types of microcontrollers. When we get to the programming part of this tutorial, I will show you exactly how to do it for the microcontroller we choose.

But choosing a microcontroller can be a very overwhelming task, especially if you are just starting out. In the next part, I’ll show you what is going through my head when I choose a microcontroller – so that you’ll know how to choose one for your next project.

Stay tuned for Parts 2-5 in the coming days…