### Getting Started with Microcontrollers

• Introduction to MPIDE

#### Installing the Multi-Pla...

How to setup the Multi-Platform Integrated Development Environment. (Microsoft Windows® version)

• Introduction to MPIDE (Mac OS® X)

#### Installing the Multi-Pla...

How to setup the Multi-Platform Integrated Development Environment. (Mac OS® X version)

#### Creation of a Sketch to...

Introduction to writing a chipKIT sketch where the goal is to blink an LED on the chipKIT board. This page also points out the existence of the reference material that is included in MPIDE.

#### Interfacing the chipKIT™...

Introduction to using the chipKIT board to interact with external devices. Here the board is programmed to blink an external board and along the way various electrical concepts are discussed.

• Button-Controlled LED

#### Obtaining Input and Gene...

The chipKIT board is used to determine whether a button has been pushed or not. The state of the button determines whether or not an LED is illuminated.

Introduction to MPIDE

#### Installing the Multi-Platform Integrated Development Environment

How to setup the Multi-Platform Integrated Development Environment. (Microsoft Windows® version)

16.9K
×
Introduction to MPIDE (Mac OS® X)

#### Installing the Multi-Platform Integrated Development Environment

How to setup the Multi-Platform Integrated Development Environment. (Mac OS® X version)

6.26K
×

#### Creation of a Sketch to Blink an LED

Introduction to writing a chipKIT sketch where the goal is to blink an LED on the chipKIT board. This page also points out the existence of the reference material that is included in MPIDE.

10.5K
×

#### Interfacing the chipKIT™ Board with an External Circuit

Introduction to using the chipKIT board to interact with external devices. Here the board is programmed to blink an external board and along the way various electrical concepts are discussed.

8.50K
×
Button-Controlled LED

#### Obtaining Input and Generating Output

The chipKIT board is used to determine whether a button has been pushed or not. The state of the button determines whether or not an LED is illuminated.

8.26K
×

## Introduction

In the Blinking an On-Board LED project, we programmed the chipKIT™ microcontroller to control an on-board LED. In that project we mentioned that chipKIT boards interact with the external world through its connectors known as pins. Now we will build our own external LED circuit and control it with the microcontroller (via one of the pins). To do so, we must understand the properties of the circuit and its components. To create the external circuit we will use a breadboard. In this project we will also consider the ways in which variables can be used in a sketch.

If you are not already proficient with basic circuit concepts, have not worked with breadboards before, or are not already familiar with the organization of pins on chipKIT™ boards, please click on the appropriate tabs to the right to learn about these topics.

##### Before you begin, you should:
• Understand how the sketch presented in Blinking an On-Board LED controls the blinking of an LED.
• Understand the basic concepts of voltage, current, and resistance.
• Understand the use of breadboards.
• Understand the general arrangement of pins on a chipKIT board.
##### After you're done, you should:
• Understand how a chipKIT board can interact with an external circuit via its pins.
• Understand the need for and use of current-limiting resistors.
• Understand the use of variables.
• Understand the schematic representation of basic circuits.

## Inventory:

Qty Description Typical Image Schematic Symbol Breadboard Image
1 LED
1 220 Ω resistor

## Step 1: Designing the Circuit

Before actually touching any circuit components, we need to create a circuit design that specifies the components to be used and how they are connected. Conceptually, the circuit we are building here is relatively simple and is comparable to a circuit with a battery, a light switch, and a light bulb. The chipKIT board will act as both the switch and the power source (effectively the battery), while an LED will serve as the “light bulb.” When an LED is illuminated, typically there is (and should be) a voltage drop across its terminals of approximately 2V. The orientation we use when we place an LED in a circuit is important. The LED will illuminate when the voltage drop is from the anode to the cathode. If you are unfamiliar with LEDs, please follow the link available from the box on the right.

It is possible to have a voltage drop across an LED of more than 2V. However, if the voltage drop exceeds the maximum forward voltage of approximately 3V, the LED might be damaged. When they are in a HIGH state, the output “pins” of the chipKIT boards have a voltage level of 3.3V (and 0V when they are in a LOW state). If we attach an LED between one of the output pins and ground (0V), then set the output pin to HIGH, the voltage that appears across the LED will exceed its maximum forward voltage. Because of the non-linear relationship between the current and the voltage in an LED, small changes in voltage can result in large changes in current. This is discussed in greater depth in the Basic Circuit Concepts page mentioned in the Introduction. Thus voltages that exceed the maximum forward voltage cause too much current to flow, damaging the LED and possibly damaging the chipKIT board.

To prevent any damage, we need to add a current-limiting resistor to our circuit. This resistor should be connected in series with the LED, meaning that any current that flows through the resistor must also flow through the LED. A resistance of 220 Ω will ensure that the maximum forward voltage of the LED is not exceeded. A discussion regarding how to determine the size of the current-limiting resistor is available via the first box on the right. The second box provides a discussion of series circuit elements and explains how series resistors can be combined.

## Step 2: Building the Circuit

Grab a breadboard, an LED, and a 220 Ω resistor. (If a 220 Ω resistor isn't available, your circuit should still work properly if you use a resistance in the one-hundred to four-hundred ohm range. If you use too small a resistance you run the risk of damaging the LED and chipKIT board. On other hand, if you use too large a resistance, the amount of current that flows through the LED may decrease to the point where the LED no longer illuminates.) Any breadboard is fine and it doesn't matter what color the LED is. In this project, as shown in Fig. 1, a ground pin is connected to a rail. This rail is, in turn, connected to one end of a resistor. When we say two things are connected, we mean they are electrically connected. Thus, if two components are directly plugged into the same node or if a wire is plugged into two different areas of the board, then these components or areas are said to be “connected.” The other end of the resistor is connected to the cathode of the LED (the shorter “leg” of the LED). The orientation of the resistor doesn't affect its operation, so it doesn't matter which end you connect to the cathode and which end you connect to ground.

Pin 3 is connected directly to the anode of the LED (the longer “leg”). Note that we elected to connect the ground pin to a rail and then the rail to a component, whereas we connected pin 3 directly to a component. For this simple circuit, there isn't a compelling reason to use the rail—we could have connected one end of the current-limiting resistor directly to ground. However, when circuits become more complicated, you will typically find that many components need to be connected to ground and thus using a rail for ground can help reduce the clutter of wires. On the other hand, it is unlikely that several components will need to be connected to an individual pin. That is the reason we attached pin 3 directly to its “final” destination in the circuit of Fig. 1. (If multiple things need to be connected to pin 3, we could either attach pin 3 to a rail or establish the additional connections using the remaining holes in the node to which pin 3 is attached. Note that, in Fig. 1, there are currently three unused holes in the node attached to pin 3.)

Because pin 3 is attached to the anode of the LED (and because the other end of the LED is attached to ground through the current-limiting resistor), when pin 3 is HIGH, the LED will be illuminated. Conversely, when pin 3 is LOW, the LED will be off. Before writing the sketch to control the LED, make sure you have created the circuit as shown in Fig. 1.

## Schematic Diagrams

“Breadboard images,” such as shown in Fig. 1, are great for depicting relatively simple circuits, but they can become cluttered and hard to interpret for more complicated circuits. Furthermore, they don't lend themselves to quick sketches of various circuit designs. Instead, it is much more common for those designing circuits to make schematic representations of circuits. Schematic diagrams use relatively simple, line-based symbols for the various circuit elements. Thus, it is easy to make schematic diagrams by hand.

Figure 2 shows the schematic diagram for this project. The LED and current-limiting resistor have their own individual symbols. (The schematic symbols for an LED and resistor are shown in the Inventory at the top of this page.) Complicated devices, such as a chipKIT board, are often represented as a simple box to which there are electrical connections. Figure 2 shows the Uno32 with all the possible pin connections even though, in the case of this circuit, we are only using two of these pins (ground and pin 3, which is labeled D3—the D is for digital).

## Step 3: Programming the chipKIT Board and the Use of Variables

Now that we have the circuit properly wired, it is time to program the chipKIT board. Open MPIDE. You can start with a new sketch or you can start from the sketch used in the Blinking an On-Board LED project. If you start with that previous sketch, the only change you need to make is to replace pin 13 with pin 3, i.e., replace all occurrences of 13 with 3. Having made that change, you can upload the sketch to the board and now the external LED should blink with a two-second interval.

However, there is one other change we want to make in the sketch. Numbers, by themselves, can be rather meaningless. If you see the number 3 or 13 and are provided no other information, those numbers carry no intrinsic meaning. You might think that at least a number intrinsically conveys a measure of quantity, but perhaps the number was meant to be used as an identifier—such as a pin identifier—rather than a measure of quantity. Nevertheless, for the sake of argument, let's assume you are at least aware that these numbers correspond to pin numbers. Then you are left to wonder what is significant about one pin as opposed to another? Is the pin being used for reading or writing? What is attached to that pin? Is the pin connected to an LED or a motor or a sensor or something else?

To make the code more readable, and hence, more maintainable, we can use variables instead of numbers in our sketches. Because we are free to choose variable names, we can use names that help document the code, i.e., with the use of variables the code itself can provide greater information about what the code does.

Furthermore, when we use variables in our code, as opposed to numbers, if we ever need to change the value of the variable (such as changing it from 13 to 3), we only need to change the code in the one place where the value of the variable is set. Contrast this to using numbers: when using numeric values, to change a particular value we have to find and change each occurrences of the number (while also ensuring the number we've found represents what we think it does—for example, in a large program, 13 might be a pin number or it could represent some other quantity like the number of turns one has in a game). Suffice it to say there are a host of reasons why it is better to use variables instead of numeric values within our sketches!

The use of variables is further explained by clicking on the boxes on the right. The import things about creating a variable are that we must provide a type and an identifier (name) when we declare a variable. Optionally, we can set the value of the variable when it is declared (or, more technically, when it is defined). We do this by assigning (using the equal sign) a value to the variable. Pin numbers are integer values and thus a variable that represents a pin should have a type of int. If the value of a variable will not change throughout a sketch, it is a good practice to also provide what is known as a type specifier that indicates the variable is actually a constant. This type specifier is const.

Putting everything together, the new sketch that blinks the external LED is as follows:

                  // Define the constant integer variable ledPin to be 3.
// This global variable is defined ("visible") in all functions.
const int ledPin = 3;

void setup()
{
// Set LED pin to OUTPUT.
pinMode(ledPin, OUTPUT);
}

void loop()
{
digitalWrite(ledPin, HIGH); // Turn on the LED.
delay(1000);                // LED remains on for 1 second.
digitalWrite(ledPin, LOW);  // Turn off the LED.
delay(1000);                // LED remains off for 1 second.
}


After writing this sketch, upload it to the chipKIT board. The external LED should blink. If you wanted to go back to blinking the internal LED, all you have to do is change 3 to 13 in line 3 (and reload the sketch to the board).