Tag Archives: Microcontroller 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 2)

I’ve always found microcontrollers really cool. After learning how to use MCUs, along with a few other electronics skills – I can’t really think of anything I am NOT able to build.

Okay, maybe not a time machine…

But when it comes to the electronics part of a project, knowing microcontrollers gives you the ability to build A LOT of different things.

Microcontroller circuit

In the previous part of this tutorial, we looked into the basics, and we established that a microcontroller is kind of like a tiny computer. You can connect things like a display, buttons or Wi-Fi, not to mention put programs onto it and run them.

In this segment, we are going to choose the microcontroller to use in our circuit. There are a gazillion options, so we’re going to use a tool to make things easier. But first, we need to figure out our requirements. Choosing a microcontroller becomes much easier once we know what we actually need.

Differences Between Microcontrollers

8-bit, 16-bit and 32-bit

A microcontroller comes in several bit sizes. You can find microcontrollers with 8-, 16- and 32-bit. This size refers to the size of the databus. With a larger databus, you can do more heavy calculations. In the hobbyist world, the 8-bit microcontroller is probably the most common of them all.

Usually, the 8-bit MCUs have fewer pins, and is therefore easier to solder onto a circuit board. In this microcontroller tutorial series, we’ll be keeping it as simple as possible, so we’re going to use an 8-bit.

Memory, Pins and Peripherals

Even an 8-bit microcontroller comes in a lot of different versions. Some have more memory. Some have more input and output pins. Some have USB interface. Some have analog to digital conversion. Some have SPI communication.

These extra functions – like USB or SPI – are called peripherals.

Chip on finger

Finding a Microcontroller for Our Circuit

My goal with this tutorial is to show you how to build a super simple microcontroller circuit.

One thing that affects how simple it is going to be is the programming method. How are we going to get the program we make onto the chip?

Often times, people use an external programmer or debugger that they connect to their microcontroller circuit, but I don’t want you to have to buy an extra tool for this. I want us to be able to just connect the circuit to a USB port on a computer and program it directly.

This leaves us with the following requirements:

  • Can be programmed through USB
  • Few components necessary
  • Possible to solder at home

We can make our circuit programmable through USB in a few different ways. For example, we can do this by adding a USB-to-serial chip onto our circuit – like they do on some of the Arduino boards.

However, since this will increase the number of components, a better solution is to find an MCU with USB interface included. It would also need a bootloader that can accept programming through USB.

Conveniently, some of Atmel’s microcontrollers come pre-loaded with a bootloader called USB DFU Bootloader.

To be able to solder the circuit at home, we want a chip with few pins. The fewer pins a microcontroller has, the easier it is to solder it. So let’s look for a chip with as few pins as possible.

The Microcontroller Selector

With our requirements in place, we can start looking for the perfect microcontroller. The Microcontroller Selector is a very useful tool that Atmel made to help you choose a microcontroller.

Using this tool, we can select what we need from our microcontroller, and it will show us which models that fit our criteria.

Using the Microcontroller Selector

We decided earlier to use an 8-bit MCU, so we select «8-bit AVR» under the «CPU» option.

Also, we want a USB interface. Specifically, we want a USB device interface. So under «USB» we select all the options that include «device».

Since we also want as few pins as possible, we’ll sort the results by number of pins.

Microcontroller Selector Tool

Doing this leaves us with five options that have 32 pins. No microcontroller matching our criteria has fewer than 32 pins., while a few options have more than 32 pins, so we can discard those.

One more criteria we are seeking is if the chip comes preloaded with the DFU bootloader. Unfortunately, we can’t select this criteria in the tool; yet, by looking into the documentation for the DFU bootloader, we can find a list of which microcontrollers are preloaded with this feature.

Here, we can see that one of our options, the ATmega8U2, does not have the bootloader. That leaves us with 4 options:

All of these match our requirements, so let’s just choose the one with the most amount of flash. The more flash we have, the bigger programs we can load onto the chip.

So, we’ll choose the ATmega32U2.

Designing the Circuit

Next, we will be designing the circuit. With the chip selected, I believe this will be pretty straightforward. Not too many components are necessary. Nevertheless, I am writing this as I am doing it – and I’ve never used this chip before – so there is always a chance of some surprises..

Stay tuned for Parts 3-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…