How to Solder – Through-hole Soldering

Introduction

Soldering is one of the most fundamental skills needed to dabble in the world of electronics. The two go together like peas and carrots. And, although it is possible to learn about and build electronics without needing to pick up a soldering iron, you’ll soon discover that a whole new world is opened with this one simple skill. We here at SparkFun believe that soldering should be a skill in everyone’s arsenal. In a world of increasing technological surroundings, we believe it is important that people everywhere be able to not only understand the technologies they use everyday but also be able to build, alter, and fix them as well. Soldering is one of many skills that will empower you to do just that.

alt text

In this tutorial we will go over the basics of through-hole soldering – also known as plated through-hole soldering (PTH), discuss the tools needed, go over techniques for proper soldering, and show you where you can go from there. We will also discuss rework as it pertains to through-hole soldering and give you some tips and tricks that will make fixing any piece of electronics a breeze. This guide will be for beginners and experts alike. Whether you’ve never touched an iron before or are looking for a little refresher, this tutorial has a little something for everyone.

Suggested Reading

As stated earlier, you can learn about and build electronics without touching a soldering iron. If you would like to learn more about electronics theory before learning to solder, we recommend starting with some of these tutorials:

If you would like to know more about building circuits without needing to pick up a soldering iron, check out our solderless breadboard tutorial:

Lastly, we will be building upon some previous tutorials, so it is suggested that you read about and understand these subjects before moving forward in this tutorial:


If you’re all caught up on the above reading, let’s dive right in!

What is Solder?

Before learning how to solder, it’s always wise to learn a little bit about solder, its history, and the terminology that will be used while discussing it.

Solder, as a word, can be used in two different ways. Solder, the noun, refers to the alloy (a substance composed of two or more metals) that typically comes as a long, thin wire in spools or tubes. Solder, the verb, means to join together two pieces of metal in what is called a solder joint. So, we solder with solder!

alt text

Solder wire sold as a spool (left) and in a tube (right). These come in both leaded and lead-free varieties.

Leaded vs. Lead-free Solder – A Brief History

One of the most important things to be aware of when it comes to solder is that, traditionally, solder was composed of mostly lead (Pb), tin (Sn), and a few other trace metals. This solder is known as leaded solder. As it has come to be known, lead is harmful to humans and can lead to lead poisoning when exposed to large amounts. Unfortunately, lead is also a very useful metal, and it was chosen as the go-to metal for soldering because of its low melting point and ability to create great solder joints.

With the adverse effects of leaded soldering known, some key individuals and countries decided it was best to not use leaded solder anymore. In 2006, the European Union adopted the Restriction of Hazardous Substances Directive(RoHS). This directive, stated simply, restricts the use of leaded solder (amongst other materials) in electronics and electrical equipment. With that, the use of lead-free solder became the norm in electronics manufacturing.

Lead-free solder is very similar to its leaded counterpart, except, as the name states, it contains no lead. Instead is is made up of mostly tin and other trace metals, such as silver and copper. This solder is usually marked with the RoHS symbol to let potential buyers know it conforms to the standard.

Choosing the Right Solder for the Job

When it comes to manufacturing electronics, it’s best to use lead-free solder to ensure the safety of your products. However, when it comes to you and your electronics, the choice of solder is yours to make. Many people still prefer the use of leaded solder on account of its superb ability to act as a joining agent. Still, others prefer safety over functionality and opt for the lead-free. SparkFun sells both varieties to allow individuals to make that choice for themselves.

Lead-free solder is not without its downfalls. As mentioned, lead was chosen because it performs the best in a situation such as soldering. When you take away the lead, you also take away some of the properties of solder that make it ideal for what it was intended – joining two pieces of metal. One such property is the melting point. Tin has a higher melting point than lead resulting in more heat needed to achieve flow. And, although tin gets the job done, it sometimes needs a little help. Many lead-free solder variants have what’s called a flux core. For now, just know that flux is a chemical agent that aids in the flowing of lead-free solder. While it is possible to use lead-free solder without flux, it makes it much easier to achieve the same effects as with leaded solder. Also, because of the added cost in making lead-free solder, it can sometimes be more expensive than leaded solder.

Aside from choosing leaded or lead-free solder, there are a number of other factors to consider when picking out solder. First, there are tons of other solder compositions out there aside from lead and tin. Check out the Wikipedia solder page for an extensive list of the different types. Second, solder comes in a variety of gauges, or widths. When working with small components, it’s often better to use a very thin piece of solder – the larger then number, the smaller the gauge. For large components, thicker wire is recommended. Last, solder comes in other forms besides wire. When getting into surface-mount soldering, you’ll see that solder paste is the form of choice. However, since this is a through-hole soldering tutorial, solder paste will not be discussed in detail.

Soldering Irons

There are many tools that aid in soldering, but none are more important than the soldering iron. If nothing else, you need at least an iron and some solder to accomplish the task at hand. Soldering irons come in a variety of from factors and range from simple to complex, but they all function roughly the same. Here, we’ll discuss the parts of an iron and the different types of irons.

alt text

Soldering Iron Anatomy

Here are the basic parts that make up a soldering iron.

  • Soldering Tips – No iron is complete without an iron tip. The tip is the part of the iron that heats up and allows solder to flow around the two components being joined. Although solder will stick to the tip when applied, a common misconception is that the tip transfers the solder. The tip actually transfers heat, raising the temperature of the metal components to the melting point of the solder, and the solder melts accordingly. Most irons give you the option to change your tip, should you need to replace an old tip or if you need to switch to a different style of tip. Tips come in a variety of sizes and shapes to accommodate any component.

tips

Several types of tips. From left to right, the bevel tip (aka hoof tip), two conical tips with varying widths, and the chisel tip.

Changing the tip is a simple process that consists of either unscrewing the wand or simply pushing in and pulling out the tip

changing the tip

  • Wand – The wand is the part of the iron that holds the tip. This is also the part that is handled by the user. Wands are usually made of a variety of insulating materials (such as rubber) to prevent the heat of the tip from transferring to the outside of the wand, but they also house wires and metal contacts that transfer heat from the base or outlet to the tip. This dual role of heating and preventing burns makes a high quality wand much appreciated.

wands

Two varieties of wands. Notice how the tips screw into the wand allowing for interchangeability. Some wands have tips that simply push in and pull out without any attaching mechanism.

Some irons consist of just a wand that plugs into a wall outlet. These irons are as simple as they come, and they do not have any controls to vary the temperature. In these irons, the heating element is built directly into the wand.

wand iron

A simple soldering iron that consists of just the wand. Some of these irons do not offer interchangeable tips.

  • Base – The base of the soldering iron is the control box that allows the adjusting of temperatures. The wand attaches to the base and receives its heat from the electronics inside. There are analog bases, which have a dial that controls the temperature, and there are digital bases, which have buttons to set the temperature and a display that tells you the current temperature. Some bases even have extra features such as heat profiles that allow you to quickly change the amount of heat provided to the tip for soldering a variety of components.

bases

Two variations of a soldering iron base. On the left, a digital base, complete with control buttons and a digital display. On the right, an analog base that uses a dial to control the temperature.

The base typically is comprised of a large transformer and several other control electronics that safely allow you to vary the heat of your tip.

inside iron

The insides of a soldering iron base

  • Stand (Cradle) – The iron stand (often referred to as a cradle) is what houses the iron when it is not in use. The stand may seem trivial, but leaving an unattended iron laying around on your desk or workbench is a potential hazard: it could burn you, or, worse, it could burn your desk and start a fire. Again, they can be as simple as ametal stand, or they can be complex, offering an auto-shutoff feature that reduces the temperature of the tip when the wand is placed in the cradle. This helps prevent the wearing of your tip over time.

alt text

Different types of iron cradles. Notice some allow for a regular sponge while others hold a brass sponge.

  • Brass Sponge – As you solder, your tip will tend to oxidize, which means it will turn black and not want to accept solder. Especially with lead-free solder, there are impurities in the solder that tend to build up on the tip of your iron, which causes this oxidization. This is where the sponge comes in. Every so often you should give your tip a good cleaning by wiping off this build-up. Traditionally, an actual wet sponge was used to accomplish this. However, using a wet sponge can drastically reduce the lifespan of your tip. By wiping your tip on a cool, wet sponge, the tip tends to expand and contract from the change in temperature. This expansion and contraction will wear out your tip and can sometime cause a hole to develop in the side of the tip. Once a tip has a hole, it is no good for soldering. Thus, brass sponges have become the standard for tip cleaning. Brass sponges pull the excess solder from your tip while allowing the tip to maintain its current heat level. If you do not have a brass sponge, a regular sponge is better than nothing.

brass sponge

A brass sponge. If your iron stand doesn’t have a spot for a brass sponge, you can get one with its own base.

Soldering Accessories

Now that you know the ins and outs of a soldering iron, it’s time to discuss the other tools that will aid you on your soldering adventure.

  • Solder Wick – is the eraser to soldering’s pencil. When dealing with issues such as jumpers or the removal of parts (desoldering), solder wick comes in very handy. Solder wick – aka desoldering braid – is comprised of thin copper wire braided together. Solder is soaked (wicked) up by the copper allowing you to “erase” extra globs of solder.

alt text

  • Tip Tinner – is a chemical paste used to clean the tip of your soldering iron. It is composed of a mild acid that helps remove baked on residue (like when you accidentally melt your tip on a component) and helps prevent oxidation (the nasty black stuff) that accumulates on your soldering tip when not in use.

alt text

  • Flux Pen – Flux is a chemical agent that aids in the flowing of lead-free solder. Flux pens allow you to dab stubborn components with liquid flux to create better looking solder joints.

alt text

  • Solder Vacuum (Solder Sucker) – is a great tool for removing solder left behind in through-holes when delsodering components. We’ll go over how to use this tool a little later in the tutorial.

alt text

Other Suggested Tools

These tools aren’t necessary, but they sure do make soldering easier at times.

  • Third Hand (Third Arm) – Third hands are great for holding PCBs, wires, and components in place while you solder.
  • Flush Cutters – Cutters allow you to trim the legs of components you solder to the PCB.
  • Safety Glasses – just in case any clipped leads go flying.

Soldering Your First Component

Let’s put all these tools into action. This first video will go over the basics of soldering your first component – headers!

Check out the Vimeo version here.

Recap

It’s really that easy! Follow Dave’s simple rules to make every solder connection a good one.

  • Be cautious when handling hot irons
  • Use third hands or vices to hold boards while you solder
  • Set your iron at a good medium heat (325-375 degrees C)
  • If you see smoke coming from your solder, turn down the heat
  • Tin your tip with solder before each connection to help prep the joint
  • Use the side of the tip (aka the sweet spot), not the very tip of the iron
  • Heat both the pad and the part you want to solder evenly and at the same time
  • Pull the solder away, then the iron
  • A good solder joint should look like a volcano or Hersey kiss, not a ball or clump

We’ve also put together this digram to help you better understand what makes a good solder joint.

alt text

Click for a larger image.

Advanced Techniques and Troubleshooting

Once you get the basics of creating good solder joints, it’s time to learn some of the more advanced PTH techniques that you can utilize. This video goes over using flux, removing solder jumpers, desoldering components, along with some other tips and tricks.

Here are some other tips for PTH soldering:

  • Desoldering can often be the best way to learn how to solder. There are many reasons to desolder a part: repair, upgrade, salvage, etc. Many of the techniques used in the video aid in the desoldering process.
  • There is another method of removing solder from through-holes that we refer to as the slap method.
  • If you’re ever unsure if the solder joint you created is making an electrical connection, you can use a multimeter to test for continuity.

Cleaning

When working with lead-free solder, flux tends to get everywhere, be it from the flux in the solder or from external flux applied by the user. Flux can corrode the PCB and components over time, thus it’s good to know how to clean your PCBs so they’re free of any flux residue. The simplest way to clean a board is to use a small brush (toothbrushes work great) and some isopropyl alcohol. If you are soldering more than a few boards, it may be necessary to clean them in batches. For this, we recommend a crock pot filled with distilled water. The distilled water keeps other impurities and contaminants away from your circuit.

It’s not 100% necessary to clean your board, however, it will increase the life of your circuit tremendously. For more information on PCB cleaning, click here.

Resources and Going Further

We’ve only just begun to travel down the soldering rabbit hole. Once you have mastered PTH soldering, you can try your hand at these other skills and tutorials.

And, of course, what’s a soldering tutorial without something to solder. SparkFun sells a variety of kits that are great for honing your soldering skills. There’s even a Learn to Solder series of kits that have all the tools necessary to get started.

LED || Light Emitting Diode

Light-Emitting Diodes (LEDs)

The Basics

alt text

LEDs are all around us: In our phones, our cars and even our homes. Any time something electronic lights up, there’s a good chance that an LED is behind it. They come in a huge variety of sizes, shapes, and colors, but no matter what they look like they have one thing in common: they’re the bacon of electronics. They’re widely purported to make any project better and they’re often added to unlikely things (to everyone’s delight).

Unlike bacon, however, they’re no good once you’ve cooked them. This guide will help you avoid any accidental LED barbecues! First things first, though. What exactly is this LED thing everyone’s talking about?

LEDs (that’s “ell-ee-dees”) are a particular type of diode that convert electrical energy into light. In fact, LED stands for “Light Emitting Diode.” (It does what it says on the tin!) And this is reflected in the similarity between the diode and LED schematic symbols:

alt text

In short, LEDs are like tiny lightbulbs. However, LEDs require a lot less power to light up by comparison. They’re also more energy efficient, so they don’t tend to get hot like conventional lightbulbs do (unless you’re really pumping power into them). This makes them ideal for mobile devices and other low-power applications. Don’t count them out of the high-power game, though. High-intensity LEDs have found their way into accent lighting, spotlights and even automotive headlights!

Are you getting the craving yet? The craving to put LEDs on everything? Good, stick with us and we’ll show you how!

Suggested Reading

Here are some other topics that will be discussed in this tutorial. If you are unfamiliar with any of them, please have a look at the respective tutorial before you go any further.

How to Use Them

alt text

So you’ve come to the sensible conclusion that you need to put LEDs on everything. We thought you’d come around. Let’s go over the rule book:

1) Polarity Matters

In electronics, polarity indicates whether a circuit component is symmetric or not. LEDs, being diodes, will only allow current to flow in one direction. And when there’s no current-flow, there’s no light. Luckily, this also means that you can’t break an LED by plugging it in backwards. Rather, it just won’t work.

alt text

The positive side of the LED is called the “anode” and is marked by having a longer “lead,” or leg. The other, negative side of the LED is called the “cathode.” Current flows from the anode to the cathode and never the opposite direction. A reversed LED can keep an entire circuit from operating properly by blocking current flow. So don’t freak out if adding an LED breaks your circuit. Try flipping it around.

2) Moar Current Equals Moar Light

The brightness of an LED is directly dependent on how much current it draws. That means two things. The first being that super bright LEDs drain batteries more quickly, because the extra brightness comes from the extra power being used. The second is that you can control the brightness of an LED by controlling the amount of current through it. But, setting the mood isn’t the only reason to cut back your current.

3) There is Such a Thing as Too Much Power

If you connect an LED directly to a current source it will try to dissipate as much power as it’s allowed to draw, and, like the tragic heroes of olde, it will destroy itself. That’s why it’s important to limit the amount of current flowing across the LED.

For this, we employ resistors. Resistors limit the flow of electrons in the circuit and protect the LED from trying to draw too much current. Don’t worry, it only takes a little basic math to determine the best resistor value to use. You can find out all about it in our resistor tutorial!

Don’t let all of this math scare you, it’s actually pretty hard to mess things up too badly. In the next section, we’ll go over how to make an LED circuit without getting your calculator.

LEDs Without Math

Before we talk about how to read a datasheet, let’s hook up some LEDs. After all, this is an LED tutorial, not a readingtutorial.

It’s also not a math tutorial, so we’ll give you a few rules of thumb for getting LEDs up and running. As you’ve probably put together from the info in the last section, you’ll need a battery, a resistor and an LED. We’re using a battery as our power source, because they’re easy to find and they can’t supply a dangerous amount of current.

The basic template for an LED circuit is pretty simple, just connect your battery, resistor and LED in series. Like this:
alt text
A good resistor value for most LEDs is 330 Ohms. You can use the information from the last section to help you determine the exact value you need, but this is LEDs without math… So, start by popping a 330 Ohm resistor into the above circuit and see what happens.

The interesting thing about resistors is that they’ll dissipate extra power as heat, so if you have a resistor that’s getting warm, you probably need to go with a smaller resistance. If your resistor is too small, however, you run the risk of burning out the LED! Given that you have a handful of LEDs and resistors to play with, here’s a flow chart to help you design your LED circuit by trial and error:
alt text
Another way to light up an LED is to just connect it to a coin cell battery! Since the coin cell can’t source enough current to damage the LED, you can connect them directly together! Just push a CR2032 coin cell between the leads of the LED. The long leg of the LED should be touching the side of the battery marked with a “+”. Now you can wrap some tape around the whole thing, add a magnet, and stick it to stuff! Yay for throwies!

alt text

Of course, if you’re not getting great results with the trial and error approach, you can always get out your calculator and math it up. Don’t worry, it’s not hard to calculate the best resistor value for your circuit. But before you can figure out the optimal resistor value, you’ll need to find the optimal current for your LED. For that we’ll need to report to the datasheet…

Get the Details

Don’t go plugging any strange LEDs into your circuits, that’s just not healthy. Get to know them first. And how better than to read the datasheet.

As an example we’ll peruse the datasheet for our Basic Red 5mm LED.

LED Current

Starting at the top and making our way down, the first thing we encounter is this charming table:

alt text

Ah, yes, but what does it all mean?

The first row in the table indicates how much current your LED will be able to handle continuously. In this case, you can give it 20mA or less, and it will shine its brightest at 20mA. The second row tells us what the maximum peak current should be for short bursts. This LED can handle short bumps to 30mA, but you don’t want to sustain that current for too long. This datasheet is even helpful enough to suggest a stable current range (in the third row from the top) of 16-18mA. That’s a good target number to help you make the resistor calculations we talked about.

The following few rows are of less importance for the purposes of this tutorial. The reverse voltage is a diode property that you shouldn’t have to worry about in most cases. The power dissipation is the amount of power in milliWatts that the LED can use before taking damage. This should work itself out as long as you keep the LED within its suggested voltage and current ratings.

LED Voltage

Let’s see what other kinds of tables they’ve put in here… Ah!

alt text

This is a useful little table! The first row tells us what the forward voltage drop across the LED will be. Forward voltage is a term that will come up a lot when working with LEDs. This number will help you decide how much voltage your circuit will need to supply to the LED. If you have more than one LED connected to a single power source, these numbers are really important because the forward voltage of all of the LEDs added together can’t exceed the supply voltage. We’ll talk about this more in-depth later in the delving deeper section of this tutorial.

LED Wavelength

The second row on this table tells us the wavelength of the light. Wavelength is basically a very precise way of explaining what color the light is. There may be some variation in this number so the table gives us a minimum and a maximum. In this case it’s 620 to 625nm, which is just at the lower red end of the spectrum (620 to 750nm). Again, we’ll go over wavelength in more detail in the delving deeper section.

LED Brightness

The last row (labeled “Luminous Intensity”) is a measure of how bright the LED can get. The unit mcd, ormillicandela, is a standard unit for measuring the intensity of a light source. This LED has an maximum intensity of 200 mcd, which means it’s just bright enough to get your attention but not quite flashlight bright. At 200 mcd, this LED would make a good indicator.

Viewing Angle

alt text

Next, we’ve got this fan-shaped graph that represents the viewing angle of the LED. Different styles of LEDs will incorporate lenses and reflectors to either concentrate most of the light in one place or spread it as widely as possible. Some LEDs are like floodlights that pump out photons in every direction; Others are so directional that you can’t tell they’re on unless you’re looking straight at them. To read the graph, imagine the LED is standing upright underneath it. The “spokes” on the graph represent the viewing angle. The circular lines represent the intensity by percent of maximum intensity. This LED has a pretty tight viewing angle. You can see that looking straight down at the LED is when it’s at its brightest, because at 0 degrees the blue lines intersect with the outermost circle. To get the 50% viewing angle, the angle at which the light is half as intense, follow the 50% circle around the graph until it intersects the blue line, then follow the nearest spoke out to read the angle. For this LED, the 50% viewing angle is about 20 degrees.

Dimensions

alt text

Finally, the mechanical drawing. This picture contains all of the measurements you’ll need to actually mount the LED in an enclosure! Notice that, like most LEDs, this one has a small flange at the bottom. That comes in handy when you want to mount it in a panel. Simply drill a hole the perfect size for the body of the LED, and the flange will keep it from falling through!

Now that you know how to decipher the datasheet, let’s see what kind of fancy LEDs you might encounter in the wild…

Types of LEDs

Congratulations, you know the basics! Maybe you’ve even gotten your hands on a few LEDs and started lighting stuff up, that’s awesome! How would you like to step up your blinky game? Let’s talk about makin’ it fancy.

Here’s the cast of characters:

alt text

RGB (Red-Green-Blue) LEDs are actually three LEDs in one! But that doesn’t mean it can only make three colors. Because red, green, and blue are the additive primary colors, you can control the intensity of each to create every color of the rainbow. Most RGB LEDs have four pins: one for each color and a common pin. On some, the common pin is the anode, and on others, it’s the cathode.
alt text

Some LEDs are smarter than others. Take the flashing LED, for example. Inside these LEDs, there’s actually anintegrated circuit that allows the LED to blink without any outside controller. Simply power it up and watch it go! These are great for projects where you want a little bit more action but don’t have room for control circuitry. There are even RGB flashing LEDs that cycle through thousands of colors!
alt text

SMD LEDs aren’t so much a specific kind of LED but a package type. As electronics get smaller and smaller, manufacturers have figured out how to cram more components in a smaller space. SMD (Surface Mount Device) parts are tiny versions of their standard counterparts. SMD LEDs come in several sizes, from fairly large to smaller than a grain of rice! Because they’re so small, and have pads instead of legs, they’re not as easy to work with, but if you’re tight on space they might be just what the doctor ordered.
alt text

High-Power LEDs, from manufacturers like Luxeon and CREE, are crazy bright. Generally, an LED is considered High-Power if it can dissipate 1 Watt or more of power. These are the fancy LEDs that you find in really nice flashlights. Arrays of them can even be built for spotlights and automobile headlights. Because there’s so much power being pumped through the LED, these often require heatsinks. A heatsink is basically a chunk of heat conducting metal with lots of surface area whose job is to transfer as much waste heat into the surrounding air as possible. High-Power LEDs can generate so much waste heat that they’ll damage themselves without proper cooling. Don’t let the term “waste heat” fool you, though, these devices are still incredibly efficient compared to conventional bulbs.
alt text

There are even LEDs that emit light outside of the normal visible spectrum. You probably use Infrared LEDs every day, for instance. They’re used in things like TV remotes to send small pieces of information in the form of invisible light! On the opposite end of the spectrum you can also get Ultraviolet LEDs. Ultraviolet LEDs will make certain materials fluoresce, just like a blacklight! They’re also used for disinfecting surfaces, because many bacteria are sensitive to UV radiation.
With fancy LEDs like these at your disposal, there’s no excuse for leaving anything un-illuminated. However, if your thirst for LED knowledge hasn’t been slaked, then read on, and we’ll get into the nitty-gritty on LEDs, color, and luminous intensity!

Delving Deeper

So you’ve graduated from LEDs 101 and you want more? Oh, don’t worry, we’ve got more. Let’s start with the science behind what makes LEDs tick… err… blink. We’ve already mentioned that LEDs are a special kind of diode, but let’s delve a little deeper into exactly what that means:

What we call an LED is really the LED and the packaging together, but the LED itself is actually tiny! It’s a chip of semiconductor material that’s doped with impurities which creates a boundary for charge carriers. When current flows into the semi-conductor, it jumps from one side of this boundary to the other, releasing energy in the process. In most diodes that energy leaves as heat, but in LEDs that energy is dissipated as light!

The wavelength of light, and therefore the color, depends on the type of semiconductor material used to make the diode. That’s because the energy band structure of semiconductors differs between materials, so photons are emitted with differing frequencies. Here’s a table of common LED semiconductors by frequency:

alt text

Truncated table of semiconductor materials by color. The full table is available on the Wikipedia entry for “LED”

While the wavelength of the light depends on the band gap of the semiconductor, the intensity depends on the amount of power being pushed through the diode. We talked about luminous intensity a little bit in a previous section, but there’s more to it than just putting a number on how bright something looks.

The unit for measuring luminous intensity is called the candela, although when you’re talking about the intensity of a single LED you’re usually in the millicandela range. The interesting thing about this unit is that it isn’t really a measure of the amount of light energy, but an actual measure of “brightness”. This is achieved by taking the power emitted in a particular direction and weighting that number by the luminosity function of the light. The human eye is more sensitive to some wavelengths of light than others, and the luminosity function is a standardized model that accounts for that sensitivity.

The luminous intesity of LEDs can range from the tens to the tens-of-thousands of millicandela. The power light on your TV is probably about 100 mcd, whereas a good flashlight might be 20,000 mcd. Looking straight into anything brighter than a few thousand millicandela can be painful; don’t try it.

Forward Voltage Drop

Oh, I also promised that we’d talk about the concept of Forward Voltage Drop. Remember when we were looking at the datasheet and I mentioned that the Forward Voltage of all of your LEDs added together can’t exceed your system voltage? This is because every component in your circuit has to share the voltage, and the amount of voltage that every part uses together will always equal the amount that’s available. This is called Kirchhoff’s Voltage Law. So if you have a 5V power supply and each of your LEDs have a forward voltage drop of 2.4V then you can’t power more than two at a time.

Kirchhoff’s Laws also come in handy when you want to approximate the voltage across a given part based on the Forward Voltage of other parts. For instance, in the example I just gave there’s a 5V supply and 2 LEDs with a 2.4V Forward Voltage Drop each. Of course we would want to include a current limiting resistor, right? How would you find out the voltage across that resistor? It’s easy:

5 (System Voltage) = 2.4 (LED 1) + 2.4 (LED 2) + Resistor

5 = 4.8 + Resistor

Resistor = 5 – 4.8

Resistor = 0.2

So there is .2V across the resistor! This is a simplified example and it isn’t always this easy, but hopefully this gives you an idea of why Forward Voltage Drop is important. Using the voltage number you derive from Kirchhoff’s Laws you can also do things like determine the current across a component using Ohm’s Law. In short, you want your system voltage equal to the expected forward voltage of your combined circuit components.

Resources and Going Further

You’ve made it! You know, like, almost everything… about LEDs. Now go forth and put LEDs on whatever you please!

If you’d like to learn more about some LED related topics, visit these other tutorials:

AVR Microcontroller ?

AVR is a family of microcontrollers developed by Atmel beginning in 1996. These are modified Harvard architecture 8-bit RISC single-chip microcontrollers. AVR was one of the first microcontroller families to use on-chip flash memory for program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM used by other microcontrollers at the time.

AVR microcontrollers find many applications as embedded systems; they are also used in the popular Arduino line of open source board designs.

Brief history

The AVR architecture was conceived by two students at the Norwegian Institute of Technology (NTH), Alf-Egil Bogen and Vegard Wollan.

The original AVR MCU was developed at a local ASIC house in Trondheim, Norway, called Nordic VLSI at the time, now Nordic Semiconductor, where Bogen and Wollan were working as students.[citation needed] It was known as a μRISC (Micro RISC)[citation needed] and was available as silicon IP/building block from Nordic VLSI.[citation needed] When the technology was sold to Atmel from Nordic VLSI,[citation needed] the internal architecture was further developed by Bogen and Wollan at Atmel Norway, a subsidiary of Atmel. The designers worked closely with compiler writers at IAR Systems to ensure that the AVR instruction set provided efficient compilation of high-level languages.

Atmel says that the name AVR is not an acronym and does not stand for anything in particular. The creators of the AVR give no definitive answer as to what the term “AVR” stands for.However, it is commonly accepted that AVR stands for Alf (Egil Bogen) and Vegard (Wollan)’s RISC processor. Note that the use of “AVR” in this article generally refers to the 8-bit RISC line of Atmel AVR Microcontrollers.

Among the first of the AVR line was the AT90S8515, which in a 40-pin DIP package has the same pinout as an 8051 microcontroller, including the external multiplexed address and data bus. The polarity of the RESET line was opposite (8051’s having an active-high RESET, while the AVR has an active-low RESET), but other than that the pinout was identical.

The AVR 8-bit microcontroller architecture was introduced in 1997. By 2003, Atmel had shipped 500 million AVR flash microcontrollers.The popular Arduino platform for simple electronics projects was released in 2005 and featured ATmega8 AVR microcontrollers.

Device overview

The AVR is a modified Harvard architecture machine, where program and data are stored in separate physical memory systems that appear in different address spaces, but having the ability to read data items from program memory using special instructions.

Basic families

AVRs are generally classified into following:

  • tinyAVR – the ATtiny series
    • 0.5–16 KB program memory
    • 6–32-pin package
    • Limited peripheral set
  • megaAVR – the ATmega series
    • 4–256 KB program memory
    • 28–100-pin package
    • Extended instruction set (multiply instructions and instructions for handling larger program memories)
    • Extensive peripheral set
  • XMEGA – the ATxmega series
    • 16–384 KB program memory
    • 44–64–100-pin package (A4, A3, A1)
    • 32-pin package: XMEGA-E (XMEGA8E5)
    • Extended performance features, such as DMA, “Event System”, and cryptography support
    • Extensive peripheral set with ADCs
  • Application-specific AVR
    • megaAVRs with special features not found on the other members of the AVR family, such as LCD controller, USB controller, advanced PWM, CAN, etc.
  • FPSLIC (AVR with FPGA)
    • FPGA 5k to 40k gates
    • SRAM for the AVR program code, unlike all other AVRs
    • AVR core can run at up to 50 MHz
  • 32-bit AVRs
Main article: AVR32
In 2006, Atmel released microcontrollers based on the 32-bit AVR32 architecture. They include SIMD and DSP instructions, along with other audio- and video-processing features. This 32-bit family of devices is intended to compete with the ARM-based processors. The instruction set is similar to other RISC cores, but it is not compatible with the original AVR or any of the various ARM cores.

Device architecture

Flash, EEPROM, and SRAM are all integrated onto a single chip, removing the need for external memory in most applications. Some devices have a parallel external bus option to allow adding additional data memory or memory-mapped devices. Almost all devices (except the smallest TinyAVR chips) have serial interfaces, which can be used to connect larger serial EEPROMs or flash chips.

Program memory

Program instructions are stored in non-volatile flash memory. Although the MCUs are 8-bit, each instruction takes one or two 16-bit words.

The size of the program memory is usually indicated in the naming of the device itself (e.g., the ATmega64x line has 64 KB of flash, while the ATmega32x line has 32 KB).

There is no provision for off-chip program memory; all code executed by the AVR core must reside in the on-chip flash. However, this limitation does not apply to the AT94 FPSLIC AVR/FPGA chips.

Internal data memory

The data address space consists of the register file, I/O registers, and SRAM.

Internal registers

Atmel ATxmega128A1 in 100-pinTQFP package

The AVRs have 32 single-byte registers and are classified as 8-bit RISC devices.

In the tinyAVR and megaAVR variants of the AVR architecture, the working registers are mapped in as the first 32 memory addresses (000016–001F16), followed by 64 I/O registers (002016–005F16). In devices with many peripherals, these registers are followed by 160 “extended I/O” registers, only accessible as memory-mapped I/O (006016–00FF16).

Actual SRAM starts after these register sections, at address 006016 or, in devices with “extended I/O”, at 010016.

Even though there are separate addressing schemes and optimized opcodes for accessing the register file and the first 64 I/O registers, all can still be addressed and manipulated as if they were in SRAM.

The very smallest of the tinyAVR variants use a reduced architecture with only 16 registers (r0 through r15 are omitted) which are not addressable as memory locations. I/O memory begins at address 000016, followed by SRAM. In addition, these devices have slight deviations from the standard AVR instruction set. Most notably, the direct load/store instructions (LDS/STS) have been reduced from 2 words (32 bits) to 1 word (16 bits), limiting the total direct addressable memory (the sum of both I/O and SRAM) to 128 bytes. Conversely, the indirect load instruction’s (LD) 16-bit address space is expanded to also include non-volatile memory such as Flash and configuration bits; therefore, the LPM instruction is unnecessary and omitted.

In the XMEGA variant, the working register file is not mapped into the data address space; as such, it is not possible to treat any of the XMEGA’s working registers as though they were SRAM. Instead, the I/O registers are mapped into the data address space starting at the very beginning of the address space. Additionally, the amount of data address space dedicated to I/O registers has grown substantially to 4096 bytes (000016–0FFF16). As with previous generations, however, the fast I/O manipulation instructions can only reach the first 64 I/O register locations (the first 32 locations for bitwise instructions). Following the I/O registers, the XMEGA series sets aside a 4096 byte range of the data address space, which can be used optionally for mapping the internal EEPROM to the data address space (100016–1FFF16). The actual SRAM is located after these ranges, starting at 2000.

GPIO ports

Each GPIO port on a tiny or mega AVR drives up to eight pins and is controlled by three 8-bit registers: DDRx, PORTx and PINx, where x is the port identifier.

  • DDRx: Data Direction Register, configures the pins as either inputs or outputs.
  • PORTx: Output port register. Sets the output value on pins configured as outputs. Enables or disables the pull-up resistor on pins configured as inputs.
  • PINx: Input register, used to read an input signal. On some devices (but not all, check the datasheet), this register can be used for pin toggling: writing a logic one to a PINx bit toggles the corresponding bit in PORTx, irrespective of the setting of the DDRx bit

xmegaAVR have additional registers for push/pull, totem-pole and pullup configurations.

EEPROM

Almost all AVR microcontrollers have internal EEPROM for semi-permanent data storage. Like flash memory, EEPROM can maintain its contents when electrical power is removed.

In most variants of the AVR architecture, this internal EEPROM memory is not mapped into the MCU’s addressable memory space. It can only be accessed the same way an external peripheral device is, using special pointer registers and read/write instructions, which makes EEPROM access much slower than other internal RAM.

However, some devices in the SecureAVR (AT90SC) family use a special EEPROM mapping to the data or program memory, depending on the configuration. The XMEGA family also allows the EEPROM to be mapped into the data address space.

Since the number of writes to EEPROM is limited – Atmel specifies 100,000 write cycles in their datasheets – a well designed EEPROM write routine should compare the contents of an EEPROM address with desired contents and only perform an actual write if the contents need to be changed.

Note that erase and write can be performed separately in many cases, byte-by-byte, which may also help prolong life when bits only need to be set to all 1s (erase) or selectively cleared to 0s (write).

Program execution

Atmel’s AVRs have a two-stage, single-level pipeline design. This means the next machine instruction is fetched as the current one is executing. Most instructions take just one or two clock cycles, making AVRs relatively fast among eight-bit microcontrollers.

The AVR processors were designed with the efficient execution of compiled C code in mind and have several built-in pointers for the task.

Instruction set

The AVR instruction set is more orthogonal than those of most eight-bit microcontrollers, in particular the 8051 clones and PIC microcontrollers with which AVR competes today. However, it is not completely regular:

  • Pointer registers X, Y, and Z have addressing capabilities that are different from each other.
  • Register locations R0 to R15 have different addressing capabilities than register locations R16 to R31.
  • I/O ports 0 to 31 have different addressing capabilities than I/O ports 32 to 63.
  • CLR affects flags, while SER does not, even though they are complementary instructions. CLR set all bits to zero, and SER sets them to one. (Note that CLR is pseudo-op for EOR R, R; and SER is short for LDI R,$FF. Math operations such as EOR modify flags, while moves/loads/stores/branches such as LDI do not.)
  • Accessing read-only data stored in the program memory (flash) requires special LPM instructions; the flash bus is otherwise reserved for instruction memory.

Additionally, some chip-specific differences affect code generation. Code pointers (including return addresses on the stack) are two bytes long on chips with up to 128 KB of flash memory, but three bytes long on larger chips; not all chips have hardware multipliers; chips with over 8 KB of flash have branch and call instructions with longer ranges; and so forth.

The mostly regular instruction set makes programming it using C (or even Ada) compilers fairly straightforward. GCC has included AVR support for quite some time, and that support is widely used. In fact, Atmel solicited input from major developers of compilers for small microcontrollers, to determine the instruction set features that were most useful in a compiler for high-level languages.

MCU speed

The AVR line can normally support clock speeds from 0 to 20 MHz, with some devices reaching 32 MHz. Lower-powered operation usually requires a reduced clock speed. All recent (Tiny, Mega, and Xmega, but not 90S) AVRs feature an on-chip oscillator, removing the need for external clocks or resonator circuitry. Some AVRs also have a system clock prescaler that can divide down the system clock by up to 1024. This prescaler can be reconfigured by software during run-time, allowing the clock speed to be optimized.

Since all operations (excluding multiplication and 16-bit add/subtract) on registers R0–R31 are single-cycle, the AVR can achieve up to 1 MIPS per MHz, i.e. an 8 MHz processor can achieve up to 8 MIPS. Loads and stores to/from memory take two cycles, branching takes two cycles. Branches in the latest “3-byte PC” parts such as ATmega2560 are one cycle slower than on previous devices.

Development

AVRs have a large following due to the free and inexpensive development tools available, including reasonably priced development boards and free development software. The AVRs are sold under various names that share the same basic core, but with different peripheral and memory combinations. Compatibility between chips in each family is fairly good, although I/O controller features may vary.

See external links for sites relating to AVR development.

Features

AVRs offer a wide range of features:

  • Multifunction, bi-directional general-purpose I/O ports with configurable, built-in pull-up resistors
  • Multiple internal oscillators, including RC oscillator without external parts
  • Internal, self-programmable instruction flash memory up to 256 KB (384 KB on XMega)
    • In-system programmable using serial/parallel low-voltage proprietary interfaces or JTAG
    • Optional boot code section with independent lock bits for protection
  • On-chip debugging (OCD) support through JTAG or debugWIRE on most devices
    • The JTAG signals (TMS, TDI, TDO, and TCK) are multiplexed on GPIOs. These pins can be configured to function as JTAG or GPIO depending on the setting of a fuse bit, which can be programmed via ISP or HVSP. By default, AVRs with JTAG come with the JTAG interface enabled.
    • debugWIRE uses the /RESET pin as a bi-directional communication channel to access on-chip debug circuitry. It is present on devices with lower pin counts, as it only requires one pin.
  • Internal data EEPROM up to 4 KB
  • Internal SRAM up to 16 KB (32 KB on XMega)
  • External 64 KB little endian data space on certain models, including the Mega8515 and Mega162.
    • The external data space is overlaid with the internal data space, such that the full 64 KB address space does not appear on the external bus and accesses to e.g. address 010016 will access internal RAM, not the external bus.
    • In certain members of the XMega series, the external data space has been enhanced to support both SRAM and SDRAM. As well, the data addressing modes have been expanded to allow up to 16 MB of data memory to be directly addressed.
    • AVRs generally do not support executing code from external memory. Some ASSPs using the AVR core do support external program memory.
  • 8-bit and 16-bit timers
    • PWM output (some devices have an enhanced PWM peripheral which includes a dead-time generator)
    • Input capture that record a time stamp triggered by a signal edge
  • Analog comparator
  • 10 or 12-bit A/D converters, with multiplex of up to 16 channels
  • 12-bit D/A converters
  • A variety of serial interfaces, including
  • Brownout detection
  • Watchdog timer (WDT)
  • Multiple power-saving sleep modes
  • Lighting and motor control (PWM-specific) controller models
  • CAN controller support
  • USB controller support
    • Proper full-speed (12 Mbit/s) hardware & Hub controller with embedded AVR.
    • Also freely available low-speed (1.5 Mbit/s) (HID) bitbanging software emulations
  • Ethernet controller support
  • LCD controller support
  • Low-voltage devices operating down to 1.8 V (to 0.7 V for parts with built-in DC–DC upconverter)
  • picoPower devices
  • DMA controllers and “event system” peripheral communication.
  • Fast cryptography support for AES and DES

Programming interfaces

There are many means to load program code into an AVR chip. The methods to program AVR chips varies from AVR family to family. Most of the methods described below use the RESET line to enter programming mode. In order to avoid the chip accidentally entering such mode, it is advised to connect a pull-up resistor between the RESET pin and the positive power supply.

ISP

6- and 10-pin ISP header diagrams

The in-system programming (ISP) programming method is functionally performed through SPI, plus some twiddling of the Reset line. As long as the SPI pins of the AVR are not connected to anything disruptive, the AVR chip can stay soldered on a PCB while reprogramming. All that is needed is a 6-pin connector and programming adapter. This is the most common way to develop with an AVR.

The Atmel AVRISP mkII device connects to a computer’s USB port and performs in-system programming using Atmel’s software.

AVRDUDE (AVR Downloader/UploaDEr) runs on Linux, FreeBSD, Windows, and Mac OS X, and supports a variety of in-system programming hardware, including Atmel AVRISP mkII, Atmel JTAG ICE, older Atmel serial-port based programmers, and various third-party and “do-it-yourself” programmers.

PDI

The Program and Debug Interface (PDI) is an Atmel proprietary interface for external programming and on-chip debugging of XMEGA devices. The PDI supports high-speed programming of all non-volatile memory (NVM) spaces; flash, EEPROM, fuses, lock-bits and the User Signature Row. This is done by accessing the XMEGA NVM controller through the PDI interface, and executing NVM controller commands. The PDI is a 2-pin interface using the Reset pin for clock input (PDI_CLK) and a dedicated data pin (PDI_DATA) for input and output.

High-voltage serial

High-voltage serial programming (HVSP) is mostly the backup mode on smaller AVRs. An 8-pin AVR package does not leave many unique signal combinations to place the AVR into a programming mode. A 12-volt signal, however, is something the AVR should only see during programming and never during normal operation. The high voltage mode can also be used in some devices where the reset pin has been disabled by fuses.

High-voltage parallel

High-voltage parallel programming (HVPP) is considered the “final resort” and may be the only way to correct bad fuse settings on an AVR chip.

Bootloader

Most AVR models can reserve a bootloader region, 256 B to 4 KB, where re-programming code can reside. At reset, the bootloader runs first and does some user-programmed determination whether to re-program or to jump to the main application. The code can re-program through any interface available, or it could read an encrypted binary through an Ethernet adapter like PXE. Atmel has application notes and code pertaining to many bus interfaces.

The AT90SC series of AVRs are available with a factory mask-ROM rather than flash for program memory.Because of the large up-front cost and minimum order quantity, a mask-ROM is only cost-effective for high-production runs.

aWire

aWire is a new one-wire debug interface available on the new UC3L AVR32 devices.

Debugging interfaces

The AVR offers several options for debugging, mostly involving on-chip debugging while the chip is in the target system.

debugWIRE

debugWIRETM is Atmel’s solution for providing on-chip debug capabilities via a single microcontroller pin. It is particularly useful for lower pin count parts which cannot provide the four “spare” pins needed for JTAG. The JTAGICE mkII, mkIII and the AVR Dragon support debugWIRE. debugWIRE was developed after the original JTAGICE release, and now clones support it.

JTAG

The Joint Test Action Group (JTAG) feature provides access to on-chip debugging functionality while the chip is running in the target system. JTAG allows accessing internal memory and registers, setting breakpoints on code, and single-stepping execution to observe system behaviour.

Atmel provides a series of JTAG adapters for the AVR:

  1. The Atmel-ICE is the latest adapter. It supports JTAG, debugWire, aWire, SPI, TPI, and PDI interfaces.
  2. The JTAGICE 3 is a midrange debugger in the JTAGICE family (JTAGICE mkIII). It supports JTAG, aWire, SPI, and PDI interfaces.
  3. The JTAGICE mkII replaces the JTAGICE and is similarly priced. The JTAGICE mkII interfaces to the PC via USB, and supports both JTAG and the newer debugWIRE interface. Numerous third-party clones of the Atmel JTAGICE mkII device started shipping after Atmel released the communication protocol.
  4. The AVR Dragon is a low-cost (approximately $50) substitute for the JTAGICE mkII for certain target parts. The AVR Dragon provides in-system serial programming, high-voltage serial programming and parallel programming, as well as JTAG or debugWIRE emulation for parts with 32 KB of program memory or less. ATMEL changed the debugging feature of AVR Dragon with the latest firmware of AVR Studio 4 – AVR Studio 5 and now it supports devices over 32 KB of program memory.
  5. The JTAGICE adapter interfaces to the PC via a standard serial port.Although the JTAGICE adapter has been declared “end-of-life” by Atmel, it is still supported in AVR Studio and other tools.

JTAG can also be used to perform a boundary scan test, which tests the electrical connections between AVRs and other boundary scan capable chips in a system. Boundary scan is well-suited for a production line, while the hobbyist is probably better off testing with a multimeter or oscilloscope.

Development tools and evaluation kits

Atmel STK500 development board

Official Atmel AVR development tools and evaluation kits contain a number of starter kits and debugging tools with support for most AVR devices:

STK600 starter kit

The STK600 starter kit and development system is an update to the STK500. The STK600 uses a base board, a signal routing board, and a target board.

The base board is similar to the STK500, in that it provides a power supply, clock, in-system programming, an RS-232 port and a CAN (Controller Area Network, an automotive standard) port via DE9 connectors, and stake pins for all of the GPIO signals from the target device.

The target boards have ZIF sockets for DIP, SOIC, QFN, or QFP packages, depending on the board.

The signal routing board sits between the base board and the target board, and routes the signals to the proper pin on the device board. There are many different signal routing boards that could be used with a single target board, depending on what device is in the ZIF socket.

The STK600 allows in-system programming from the PC via USB, leaving the RS-232 port available for the target microcontroller. A 4 pin header on the STK600 labeled ‘RS-232 spare’ can connect any TTL level USART port on the chip to an onboard MAX232 chip to translate the signals to RS-232 levels. The RS-232 signals are connected to the RX, TX, CTS, and RTS pins on the DB-9 connector.

STK500 starter kit

The STK500 starter kit and development system features ISP and high voltage programming (HVP) for all AVR devices, either directly or through extension boards. The board is fitted with DIP sockets for all AVRs available in DIP packages.

STK500 Expansion Modules: Several expansion modules are available for the STK500 board:

  • STK501 – Adds support for microcontrollers in 64-pin TQFP packages.
  • STK502 – Adds support for LCD AVRs in 64-pin TQFP packages.
  • STK503 – Adds support for microcontrollers in 100-pin TQFP packages.
  • STK504 – Adds support for LCD AVRs in 100-pin TQFP packages.
  • STK505 – Adds support for 14 and 20-pin AVRs.
  • STK520 – Adds support for 14 and 20, and 32-pin microcontrollers from the AT90PWM and ATmega family.
  • STK524 – Adds support for the ATmega32M1/C1 32-pin CAN/LIN/Motor Control family.
  • STK525 – Adds support for the AT90USB microcontrollers in 64-pin TQFP packages.
  • STK526 – Adds support for the AT90USB microcontrollers in 32-pin TQFP packages.

STK200 starter kit

The STK200 starter kit and development system has a DIP socket that can host an AVR chip in a 40, 20, or 8-pin package. The board has a 4 MHz clock source, 8 light-emitting diode(LED)s, 8 input buttons, an RS-232 port, a socket for a 32k SRAM and numerous general I/O. The chip can be programmed with a dongle connected to the parallel port.

Supported microcontrollers (according to the manual)
Chip Flash size EEPROM SRAM Frequency
[MHz]
Package
AT90S1200 1k 64 0 12 PDIP-20
AT90S2313 2k 128 128 10 PDIP-20
AT90S/LS2323 2k 128 128 10 PDIP-8
AT90S/LS2343 2k 128 128 10 PDIP-8
AT90S4414 4k 256 256 8 PDIP-40
AT90S/LS4434 4k 256 256 8 PDIP-40
AT90S8515 8k 512 512 8 PDIP-40
AT90S/LS8535 8k 512 512 8 PDIP-40

ATmega2560-Arduino Pin Mapping

Arduino Mega 2560 PIN mapping table

Pin Number Pin Name Mapped Pin Name
1 PG5 ( OC0B ) Digital pin 4 (PWM)
2 PE0 ( RXD0/PCINT8 ) Digital pin 0 (RX0)
3 PE1 ( TXD0 ) Digital pin 1 (TX0)
4 PE2 ( XCK0/AIN0 )
5 PE3 ( OC3A/AIN1 ) Digital pin 5 (PWM)
6 PE4 ( OC3B/INT4 ) Digital pin 2 (PWM)
7 PE5 ( OC3C/INT5 ) Digital pin 3 (PWM)
8 PE6 ( T3/INT6 )
9 PE7 ( CLKO/ICP3/INT7 )
10 VCC VCC
11 GND GND
12 PH0 ( RXD2 ) Digital pin 17 (RX2)
13 PH1 ( TXD2 ) Digital pin 16 (TX2)
14 PH2 ( XCK2 )
15 PH3 ( OC4A ) Digital pin 6 (PWM)
16 PH4 ( OC4B ) Digital pin 7 (PWM)
17 PH5 ( OC4C ) Digital pin 8 (PWM)
18 PH6 ( OC2B ) Digital pin 9 (PWM)
19 PB0 ( SS/PCINT0 ) Digital pin 53 (SS)
20 PB1 ( SCK/PCINT1 ) Digital pin 52 (SCK)
21 PB2 ( MOSI/PCINT2 ) Digital pin 51 (MOSI)
22 PB3 ( MISO/PCINT3 ) Digital pin 50 (MISO)
23 PB4 ( OC2A/PCINT4 ) Digital pin 10 (PWM)
24 PB5 ( OC1A/PCINT5 ) Digital pin 11 (PWM)
25 PB6 ( OC1B/PCINT6 ) Digital pin 12 (PWM)
26 PB7 ( OC0A/OC1C/PCINT7 ) Digital pin 13 (PWM)
27 PH7 ( T4 )
28 PG3 ( TOSC2 )
29 PG4 ( TOSC1 )
30 RESET RESET
31 VCC VCC
32 GND GND
33 XTAL2 XTAL2
34 XTAL1 XTAL1
35 PL0 ( ICP4 ) Digital pin 49
36 PL1 ( ICP5 ) Digital pin 48
37 PL2 ( T5 ) Digital pin 47
38 PL3 ( OC5A ) Digital pin 46 (PWM)
39 PL4 ( OC5B ) Digital pin 45 (PWM)
40 PL5 ( OC5C ) Digital pin 44 (PWM)
41 PL6 Digital pin 43
42 PL7 Digital pin 42
43 PD0 ( SCL/INT0 ) Digital pin 21 (SCL)
44 PD1 ( SDA/INT1 ) Digital pin 20 (SDA)
45 PD2 ( RXDI/INT2 ) Digital pin 19 (RX1)
46 PD3 ( TXD1/INT3 ) Digital pin 18 (TX1)
47 PD4 ( ICP1 )
48 PD5 ( XCK1 )
49 PD6 ( T1 )
50 PD7 ( T0 ) Digital pin 38
51 PG0 ( WR ) Digital pin 41
52 PG1 ( RD ) Digital pin 40
53 PC0 ( A8 ) Digital pin 37
54 PC1 ( A9 ) Digital pin 36
55 PC2 ( A10 ) Digital pin 35
56 PC3 ( A11 ) Digital pin 34
57 PC4 ( A12 ) Digital pin 33
58 PC5 ( A13 ) Digital pin 32
59 PC6 ( A14 ) Digital pin 31
60 PC7 ( A15 ) Digital pin 30
61 VCC VCC
62 GND GND
63 PJ0 ( RXD3/PCINT9 ) Digital pin 15 (RX3)
64 PJ1 ( TXD3/PCINT10 ) Digital pin 14 (TX3)
65 PJ2 ( XCK3/PCINT11 )
66 PJ3 ( PCINT12 )
67 PJ4 ( PCINT13 )
68 PJ5 ( PCINT14 )
69 PJ6 ( PCINT 15 )
70 PG2 ( ALE ) Digital pin 39
71 PA7 ( AD7 ) Digital pin 29
72 PA6 ( AD6 ) Digital pin 28
73 PA5 ( AD5 ) Digital pin 27
74 PA4 ( AD4 ) Digital pin 26
75 PA3 ( AD3 ) Digital pin 25
76 PA2 ( AD2 ) Digital pin 24
77 PA1 ( AD1 ) Digital pin 23
78 PA0 ( AD0 ) Digital pin 22
79 PJ7
80 VCC VCC
81 GND GND
82 PK7 ( ADC15/PCINT23 ) Analog pin 15
83 PK6 ( ADC14/PCINT22 ) Analog pin 14
84 PK5 ( ADC13/PCINT21 ) Analog pin 13
85 PK4 ( ADC12/PCINT20 ) Analog pin 12
86 PK3 ( ADC11/PCINT19 ) Analog pin 11
87 PK2 ( ADC10/PCINT18 ) Analog pin 10
88 PK1 ( ADC9/PCINT17 ) Analog pin 9
89 PK0 ( ADC8/PCINT16 ) Analog pin 8
90 PF7 ( ADC7 ) Analog pin 7
91 PF6 ( ADC6 ) Analog pin 6
92 PF5 ( ADC5/TMS ) Analog pin 5
93 PF4 ( ADC4/TMK ) Analog pin 4
94 PF3 ( ADC3 ) Analog pin 3
95 PF2 ( ADC2 ) Analog pin 2
96 PF1 ( ADC1 ) Analog pin 1
97 PF0 ( ADC0 ) Analog pin 0
98 AREF Analog Reference
99 GND GND
100 AVCC VCC

Arduino MEGA 2560 ||Genuino MEGA 2560

Overview

  • 5V
  • 8-bit
  • 16 MHz
  • AVR
The Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button. It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started. The Mega 2560 board is compatible with most shields designed for the Uno and the former boards Duemilanove or Diecimila.

Technical specs

Microcontroller ATmega2560
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V
Digital I/O Pins 54 (of which 15 provide PWM output)
Analog Input Pins 16
DC Current per I/O Pin 20 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 256 KB of which 8 KB used by bootloader
SRAM 8 KB
EEPROM 4 KB
Clock Speed 16 MHz
LED_BUILTIN 13
Length 101.52 mm
Width 53.3 mm
Weight 37 g

Programming

The Mega 2560 board can be programmed with the Arduino Software (IDE). For details, see thereference and tutorials.

The ATmega2560 on the Mega 2560 comes preprogrammed with a bootloader that allows you to upload new code to it without the use of an external hardware programmer. It communicates using the original STK500 protocol (reference, C header files).
You can also bypass the bootloader and program the microcontroller through the ICSP (In-Circuit Serial Programming) header using Arduino ISP or similar; see these instructions for details.
The ATmega16U2 (or 8U2 in the rev1 and rev2 boards) firmware source code is available in theArduino repository. The ATmega16U2/8U2 is loaded with a DFU bootloader, which can be activated by:

  • On Rev1 boards: connecting the solder jumper on the back of the board (near the map of Italy) and then resetting the 8U2.
  • On Rev2 or later boards: there is a resistor that pulling the 8U2/16U2 HWB line to ground, making it easier to put into DFU mode. You can then use Atmel’s FLIP software (Windows) or the DFU programmer (Mac OS X and Linux) to load a new firmware. Or you can use the ISP header with an external programmer (overwriting the DFU bootloader). See this user-contributed tutorial for more information.

Warnings

The Mega 2560 has a resettable polyfuse that protects your computer’s USB ports from shorts and overcurrent. Although most computers provide their own internal protection, the fuse provides an extra layer of protection. If more than 500 mA is applied to the USB port, the fuse will automatically break the connection until the short or overload is removed.

Power

The Mega 2560 can be powered via the USB connection or with an external power supply. The power source is selected automatically.

External (non-USB) power can come either from an AC-to-DC adapter (wall-wart) or battery. The adapter can be connected by plugging a 2.1mm center-positive plug into the board’s power jack. Leads from a battery can be inserted in the GND and Vin pin headers of the POWER connector.
The board can operate on an external supply of 6 to 20 volts. If supplied with less than 7V, however, the 5V pin may supply less than five volts and the board may become unstable. If using more than 12V, the voltage regulator may overheat and damage the board. The recommended range is 7 to 12 volts.

The power pins are as follows:

  • Vin. The input voltage to the board when it’s using an external power source (as opposed to 5 volts from the USB connection or other regulated power source). You can supply voltage through this pin, or, if supplying voltage via the power jack, access it through this pin.
  • 5V. This pin outputs a regulated 5V from the regulator on the board. The board can be supplied with power either from the DC power jack (7 – 12V), the USB connector (5V), or the VIN pin of the board (7-12V). Supplying voltage via the 5V or 3.3V pins bypasses the regulator, and can damage your board. We don’t advise it.
  • 3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA.
  • GND. Ground pins.
  • IOREF. This pin on the board provides the voltage reference with which the microcontroller operates. A properly configured shield can read the IOREF pin voltage and select the appropriate power source or enable voltage translators on the outputs for working with the 5V or 3.3V.

Memory

The ATmega2560 has 256 KB of flash memory for storing code (of which 8 KB is used for the bootloader), 8 KB of SRAM and 4 KB of EEPROM (which can be read and written with the EEPROM library).

Input and Output

Each of the 54 digital pins on the Mega can be used as an input or output, using pinMode(),digitalWrite(), and digitalRead() functions. They operate at 5 volts. Each pin can provide or receive 20 mA as recommended operating condition and has an internal pull-up resistor (disconnected by default) of 20-50 k ohm. A maximum of 40mA is the value that must not be exceeded to avoid permanent damage to the microcontroller.
In addition, some pins have specialized functions:

  • Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX). Used to receive (RX) and transmit (TX) TTL serial data. Pins 0 and 1 are also connected to the corresponding pins of the ATmega16U2 USB-to-TTL Serial chip.
  • External Interrupts: 2 (interrupt 0), 3 (interrupt 1), 18 (interrupt 5), 19 (interrupt 4), 20 (interrupt 3), and 21 (interrupt 2). These pins can be configured to trigger an interrupt on a low level, a rising or falling edge, or a change in level. See the attachInterrupt() function for details.
  • PWM: 2 to 13 and 44 to 46. Provide 8-bit PWM output with the analogWrite() function.
  • SPI: 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS). These pins support SPI communication using theSPI library. The SPI pins are also broken out on the ICSP header, which is physically compatible with the Arduino /Genuino Uno and the old Duemilanove and Diecimila Arduino boards.
  • LED: 13. There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it’s off.
  • TWI: 20 (SDA) and 21 (SCL). Support TWI communication using the Wire library. Note that these pins are not in the same location as the TWI pins on the old Duemilanove or Diecimila Arduino boards.

See also the mapping Arduino Mega 2560 PIN diagram.

The Mega 2560 has 16 analog inputs, each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and analogReference() function.
There are a couple of other pins on the board:

  • AREF. Reference voltage for the analog inputs. Used with analogReference().
  • Reset. Bring this line LOW to reset the microcontroller. Typically used to add a reset button to shields which block the one on the board.

Communication

The Mega 2560 board has a number of facilities for communicating with a computer, another board, or other microcontrollers. The ATmega2560 provides four hardware UARTs for TTL (5V) serial communication. An ATmega16U2 (ATmega 8U2 on the revision 1 and revision 2 boards) on the board channels one of these over USB and provides a virtual com port to software on the computer (Windows machines will need a .inf file, but OSX and Linux machines will recognize the board as a COM port automatically. The Arduino Software (IDE) includes a serial monitor which allows simple textual data to be sent to and from the board. The RX and TX LEDs on the board will flash when data is being transmitted via the ATmega8U2/ATmega16U2 chip and USB connection to the computer (but not for serial communication on pins 0 and 1).

A SoftwareSerial library allows for serial communication on any of the Mega 2560’s digital pins.
The Mega 2560 also supports TWI and SPI communication. The Arduino Software (IDE) includes a Wire library to simplify use of the TWI bus; see the documentation for details. For SPI communication, use the SPI library.
Physical Characteristics and Shield Compatibility

The maximum length and width of the Mega 2560 PCB are 4 and 2.1 inches respectively, with the USB connector and power jack extending beyond the former dimension. Three screw holes allow the board to be attached to a surface or case. Note that the distance between digital pins 7 and 8 is 160 mil (0.16″), not an even multiple of the 100 mil spacing of the other pins.

The Mega 2560 is designed to be compatible with most shields designed for the Uno and the older Diecimila or Duemilanove Arduino boards. Digital pins 0 to 13 (and the adjacent AREF and GND pins), analog inputs 0 to 5, the power header, and ICSP header are all in equivalent locations. Furthermore, the main UART (serial port) is located on the same pins (0 and 1), as are external interrupts 0 and 1 (pins 2 and 3 respectively). SPI is available through the ICSP header on both the Mega 2560 and Duemilanove / Diecimila boards. Please note that I2C is not located on the same pins on the Mega 2560 board (20 and 21) as the Duemilanove / Diecimila boards (analog inputs 4 and 5).
Automatic (Software) Reset

Rather then requiring a physical press of the reset button before an upload, the Mega 2560 is designed in a way that allows it to be reset by software running on a connected computer. One of the hardware flow control lines (DTR) of the ATmega8U2 is connected to the reset line of the ATmega2560 via a 100 nanofarad capacitor. When this line is asserted (taken low), the reset line drops long enough to reset the chip. The Arduino Software (IDE) uses this capability to allow you to upload code by simply pressing the upload button in the Arduino environment. This means that the bootloader can have a shorter timeout, as the lowering of DTR can be well-coordinated with the start of the upload.

This setup has other implications. When the Mega 2560 board is connected to either a computer running Mac OS X or Linux, it resets each time a connection is made to it from software (via USB). For the following half-second or so, the bootloader is running on the ATMega2560. While it is programmed to ignore malformed data (i.e. anything besides an upload of new code), it will intercept the first few bytes of data sent to the board after a connection is opened. If a sketch running on the board receives one-time configuration or other data when it first starts, make sure that the software with which it communicates waits a second after opening the connection and before sending this data.
The Mega 2560 board contains a trace that can be cut to disable the auto-reset. The pads on either side of the trace can be soldered together to re-enable it. It’s labeled “RESET-EN”. You may also be able to disable the auto-reset by connecting a 110 ohm resistor from 5V to the reset line; see this forum thread for details.
Revisions

The Mega 2560 does not use the FTDI USB-to-serial driver chip used in past designs. Instead, it features the ATmega16U2 (ATmega8U2 in the revision 1 and revision 2 Arduino boards) programmed as a USB-to-serial converter.
Revision 2 of the Mega 2560 board has a resistor pulling the 8U2 HWB line to ground, making it easier to put into DFU mode.
Revision 3 of the Arduino board and the current Genuino Mega 2560 have the following improved features:

  • 1.0 pinout: SDA and SCL pins – near to the AREF pin – and two other new pins placed near to the RESET pin, the IOREF that allow the shields to adapt to the voltage provided from the board. In future, shields will be compatible both with the board that use the AVR, which operate with 5V and with the board that uses ATSAM3X8E, that operate with 3.3V. The second one is a not connected pin, that is reserved for future purposes.
  • Stronger RESET circuit.
  • Atmega 16U2 replace the 8U2.