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)

  • Blinking an On-Board LED

    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.

  • Blinking an External LED

    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)

19.8K
×
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)

7.80K
×
Blinking an On-Board LED

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.

12.7K
×
Blinking an External LED

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.

10.8K
×
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.

11.5K
×

Blinking an On-Board LED

Creation of a Sketch to Blink an LED

Blinking an On-Board LED:

Creation of a Sketch to Blink an LED

Introduction

In the Introduction to MPIDE project, you used the example Blink sketch that was provided in MPIDE to blink one of the on-board LEDs (light emitting diodes). The LED that blinked on the Uno32 board is depicted in Fig. 1. Although Fig. 1 shows an Uno32, the code you write for this project can be used equally well with other chipKIT™ boards such as the uC32 and Max32. A chipKIT board interacts with the external world through its connectors known as pins. For this project, we will blink an on-board LED which is tied to one of these pins. Thus, by controlling whether the pin tied to the LED is HIGH or LOW, we can control whether or not the LED is illuminated. To learn more about the pins on a chipKIT board, please see the information available via the first tab on the right. (In the Blinking an External LED project we will use one of these pins to blink an LED that is not built into the board.)

In this project, the goal is to write and understand a sketch that will blink the same on-board LED as the Blink sketch. You will also become more familiar with MPIDE along the way. Before beginning, you should be aware that chipKIT boards are digital devices. If you don't know what distinguishes digital devices and digital signals from analog ones, please read the material available via the second tab on the right.

Figure 1. Uno32 with pin 13 set to HIGH. This causes the LED tied to this pin to be illuminated.
Before you begin, you should:
  • Work through the Introduction to MPIDE project.
  • Understand how to download a sketch from MPIDE to a chipKIT board.
  • Understand the difference between digital and analog signals.
After you're done, you should:
  • Understand basic C syntax.
  • Understand what a pin is.
  • Understand the minimum requirements for a sketch.
  • Be familiar with some of the common functions used to interact with the chipKIT board, such as pinMode() and delay().
  • Know how #define can be used to create named constants.
  • Know how to access the chipKIT reference material.
  • Be able to blink an on-board LED at different rates.

Programming Basics

The language that MPIDE uses is based on the C++ programming language (read C++ as “C-plus-plus”). C++ was developed from the language C, which dates back to the late 1960's. C++ subsumes C, meaning that everything you can do in C, you can do with equivalent statements in C++. However, the converse is not true. If you are not familiar with either C or C++, you should read the introductory material available via the tabs to the right.

In C++, every program must have a function named main(). However, unlike in C++, in MPIDE you do not write a main() function yourself because one has already been written for you. In fact, not only has one been written for you, but you do not have access to it. Instead, you must write two functions: setup() and loop(). When we write the name of a function, the name will be followed by a set of parentheses. As you will see, when you “call” or invoke a function in your sketches, the function name must be followed by parentheses (that may or may not include something). Thus, in the text, we use parentheses to emphasize that we are talking about a function.

The setup() function is run once at the start of execution of your sketch (when your sketch first starts to run). The statements in this function are typically used to perform “one-time” set-up operations, such as specifying which pins should be used for input or output or specifying the speed of communication between your chipKIT board and the computer.

Figure 2. Depiction of the flow of execution in a sketch.

In contrast, the loop() function is called repeatedly. By repeatedly, we mean that after all the statements in the function have been executed and the function returns, the function is immediately called again. This cycle repeats until the board loses power or until it is reprogrammed. The flow of execution of a sketch is illustrated in Fig. 2. Each rectangular box represents a function. After all the statements in the setup() function have been executed, the flow of execution moves to the loop() function. After all the statements in this function have been executed, they are executed again. As you will soon see, functions can be “called” from within other functions. So, for example, any number of functions can be called from within the loop() function and thus these functions will be executed each time loop() is executed.

You have undoubtedly noticed that certain words or pieces of computer code have been highlighted using different colors. This highlighting is done within MPIDE to help indicate the way in which the particular term is used. For example, the blue that is used for HIGH and LOW indicates that these are “constants” whose values cannot be changed. The highlighting used within MPIDE is used throughout the text of these projects as well. (Although the details of the highlighting are somewhat outside of the scope of our current interests, roughly speaking, blue is used for “defined constants”, red is used for keywords in the C++ language, orange is used for various functions, and purple is used for “objects” [which are not considered in this project].)

Step 1: Writing the setup() Function

After starting MPIDE, you'll begin this project by creating the setup() function. The setup() function doesn't return anything, so its return type is void. Also, the setup() function has no inputs, so nothing should go between the parentheses that follow the function's name. Thus, a template for the setup() function in which the body is empty is simply:

        
                  void setup()
                  {
                    // Body of function goes here.
                  }
                

Within the setup() function, you will use the function pinMode() to tell the board that you wish to use a pin in a certain way. This function takes two input parameters, called arguments, corresponding to a pin (we must specify the pin number) and the mode for that pin. The mode is either INPUT or OUTPUT. When set to INPUT, we can “read” signals from the pin. Conversely, when the mode is set to OUTPUT, we can “write” to the pin. Keep in mind that the only output we can write is HIGH or LOW. When we write one of these values (using a function we'll describe below), that establishes the pin's state. If we write HIGH, the pin will be maintained at a “high” voltage of 3.3V. On the other hand, if we write LOW, the pin will be maintained at a “low” voltage of 0V. These voltages are maintained until a different value is written to the pin.

On the chipKIT boards, one of the on-board LEDs is tied to pin 13. When pin 13 is HIGH, this LED is illuminated. When pin 13 is LOW, this LED is off. We want to configure pin 13 for OUTPUT so that you can effectively “write” to the LED. Thus, we must have the following statement in the body of the setup() function:

				  pinMode(13, OUTPUT);
                

Within MPIDE, OUTPUT and INPUT are constants. Hidden from us is the fact that they have numerical values that cannot be changed while the program is running. Instead of using these named constants, we could use the underlying numerical values directly in our program. However, by using these named constants, the code is more descriptive, i.e., more readable and self-documenting. The use of named constants in this way is considered good programming style.

Note that we are using the numeric value 13 in our program and it might be difficult for a person reading this code to know what this particular number represents or means. In the Blink External LED project we demonstrate how we can provide our own names for numeric values.

Combining the pinMode() statement with the template of the setup() function, the complete function is as follows:

        
                  void setup()
                  {
                    pinMode(13, OUTPUT);
                  }
                

Be sure you have entered this function into MPIDE as it appears here. However, you are, of course, free to add comments as you see fit. Furthermore, since the amount of whitespace that is included in a sketch does not affect what it ultimately does, you could include or exclude blank spaces. Nevertheless, it is considered good programming style to indent the body of a function, as shown in line 3.

You may have noticed that the sketch we are writing is slightly different from the Blink sketch provided as an example with MPIDE. The example sketch uses a predefined label, i.e., a named constant (PIN_LED1), for the pin associated with the LED. Although we don't rely on that definition here, it is a good idea for you to be familiar with the way that named constants are created. This is accomplished with a preprocessor directive, specifically a #define statement.

Step 2: Writing the loop() Function

Now that you've finished writing the setup() function, you can focus on creating the loop() function that the board will run repeatedly (after first running the setup() function once). To make the LED blink, you will need two more functions. The first is digitalWrite() and the second is delay(). The digitalWrite() function requires two arguments. The first argument is the pin to which you're writing, and the second argument is the pin's state, which is either HIGH or LOW. Thus, the function call that sets pin 13 to HIGH should look like this:

                  digitalWrite(13, HIGH);
                

Similarly, to set pin 13 to LOW, it should look like this:

        
                  digitalWrite(13, LOW);
                

The delay() function has only a single argument, which specifies the amount of delay in milliseconds. By “delay” we mean the board simply does nothing for the specified amount of time. There are one thousand milliseconds in one second. Thus, to obtain a delay of one second, you would write:

                  delay(1000);
                

We can now combine these functions in the body of the loop() function such that the LED attached to pin 13 blinks at a two-second interval (i.e., it is on for one second, then off for one second):

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

Step 3: Putting it all Together

At this point we have all the code necessary to obtain a blinking LED. The following is the complete sketch that is now ready to be uploaded to your chipKIT board:

                  void setup()
                  {
                    // Set pin 13, the LED, to OUTPUT.
                    pinMode(13, OUTPUT);
                  }
                  
                  void loop()
                  {
                    digitalWrite(13, HIGH); // Turn on the LED.
                    delay(1000);            // LED remains on for 1 second.
                    digitalWrite(13, LOW);  // Turn off the LED.
                    delay(1000);            // LED remains off for 1 second.
                  }
                

If you need a refresher on how to upload your sketch to the board, see the Introduction to MPIDE project.

Test Your Knowledge!

After you have completed this project, you should:

  • Change the amount of delay so that the LED is off for a quarter second (but still on for one second; there are 250 milliseconds in a quarter second). Keep in mind that after modifying the code in MPIDE, you will have to download it to the board.
  • Change the amount of delay so that the LED blinks with a half-second interval (on for a quarter second; off for a quarter second).
  • While using the same amount of time both for how long the LED is on and for how long it is off, experiment with the delay to determine how short the delay has to be before you can no longer detect that the LED is actually blinking.
  • Have the LED turn on for one second, then turn off for a half second, then turn on for a half second, then turn off for a half second, and then repeat. This will require that you add two statements to the loop() function (another digitalWrite() and delay()).
  • In the listing in Step 3, remove (or comment out) the delay() function in line 10. What happens? Try to anticipate what happens and then test your guess by downloading the modified sketch to the board. Then, restore the delay() function in line 10 but comment out or remove the one in line 13. Again, try to anticipate what will happen and test your guess.

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.
  • Circuit and breadboard images were created using Fritzing.