How to setup the Multi-Platform Integrated Development Environment. (Microsoft Windows® version)
How to setup the Multi-Platform Integrated Development Environment. (Mac OS® X version)
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.
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.
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.
This project demonstrates how to use a button to turn a light emitting diode (LED) on or off. You are probably already familiar with traditional light switches that directly control whether or not electrical energy is delivered to a light bulb. In this project, a button is used to control an LED, but the button is not directly connected to the LED. Instead, the button is used to establish a voltage that is “read” via a chipKIT™ digital input/output (I/O) pin. From this voltage, we can determine if the button is pushed or not (i.e., we can determine the button's “state”). The LED is then illuminated in accordance with the state of the button: the LED is only illuminated while the button is pushed.
Qty | Description | Typical Image | Schematic Symbol | Breadboard Image |
---|---|---|---|---|
1 | Two-port button | ![]() |
||
1 | LED | ![]() |
||
1 | 220 Ω resistor | ![]() |
||
1 | 10 kΩ resistor (10 kΩ = 10,000 Ω) |
![]() |
Now, attach one end of a 220 Ω resistor to the cathode of LED A. (Resistors behave the same way regardless of their orientation.) The other end of the resistor should be placed so that it connects to the ground bus strip. This can be done by placing the end of the resistor directly into the bus strip. Alternatively, as shown in Fig. 2, you can insert the other end of the resistor into a different column (or node) in the breadboard and then use a wire to connect from that column to the ground bus strip.
When there is a single path for charge to flow through two components (i.e., any charge that flows through one component must also flow through the other), then we say the components are in series. The LED in this circuit has a resistor in series with it. This “series resistor” is used to limit the current that passes through its respective branch of the circuit and thus, as described in greater detail in the Blinking an External LED project, this is known as a current-limiting resistor. (As a brief reminder, LEDs are non-linear devices and can potentially pass large amounts of current when their threshold voltage is exceeded. The current-limiting resistor keeps the current at a safe level. This helps to protect the LED and chipKIT board from the potential damage of drawing, or trying to supply, too much current.)
Before connecting the button to the circuit, it is appropriate to consider a bit of circuit theory so that we understand how a button can be used to produce a signal that can be read by a chipKIT (digital) I/O pin. To obtain a signal where the signal voltage properly corresponds to the button's state, we cannot simply attach the button directly to the chipKIT board—there's a little more to it than that. We need to also use a pull-up or a pull-down resistor. If you are unfamiliar with using a pull-up or pull-down resistor with a button, please read the information available via the boxes to the right.
With that background out of the way, we are now ready to build the button portion of the circuit.
You may wonder why we used a relatively small resistance of 220 Ω for the current-limiting resistor that is in series with the LED, but a relatively large resistance of 10 kΩ for the pull-down resistor. (Note that k is an abbreviation for kilo, meaning thousands, so that 10 kΩ is another way of writing 10,000 Ω.) The size of the current-limiting resistor was discussed in the Blinking an External LED project. This resistor needs to be large enough to ensure that a damaging amount of current does not flow through the LED, but small enough that a sufficient amount of voltage appears across the LED so that it illuminates. On the other hand, a pull-down resistor primarily needs to provide a path to ground while ensuring the respective circuit does not draw an excessive amount of current. We could, in fact, use a larger resistance for these pull-down resistors. However, for reasons we won't explore here, if the resistance is too large, that may cause problems with the sensing of the voltage.
At this point, you should now have the button and LED in the breadboard as shown in Fig. 4 and the schematic representation is shown in Fig. 5. The next step is to write the sketch that controls the circuit. Because this sketch uses if statements and comparison operators, we'll introduce those programming constructs after first discussing the digitalRead() function.
A PDF version of this schematic is available here.
The sketch for this project uses an if statement and the digitalRead() function. The digitalRead() function takes a single parameter, which is the pin that is “read.” The function reads the electrical state of the pin, and returns either HIGH or LOW, accordingly. In the projects Blinking an Onboard LED and Blinking an External LED we used the pinMode() function to specify that a particular pin should be used as an output pin. We now want to use a pin for input. Assume we want to specify that pin 7 will be used for input. We accomplish this with the following statement:
pinMode(7, INPUT); // Specify that pin 7 will be used for input.
This statement would typically appear in the setup() function. Now assume we want to store the current state of pin 7 in the integer variable val. We can accomplish this using the following statements:
int val; // Declare the integer variable val.
val = digitalRead(7); // Assign the state of pin 7 to val.
If pin 7 is at a “high” voltage when the function is called, val is set to HIGH. Otherwise, val is set to LOW. We can also set the value of the variable val when we declare it as shown in the following statement:
int val = digitalRead(7); // Assign the state of pin 7 to integer variable val.
To make a decision based on the value read from an input signal we need to use a control structure in our sketch that allows the program to do different things based on the outcome of a test. The control structure we will use here is an if statement. Additionally, in the sketch we will write, embedded within the if statement is a comparison expression (or comparison operation). This comparison serves as the test on which a decision is made. If you are not already familiar with if statements and comparison operations, please read the information available via the box on the right.
Provided you understand the code shown above, we merely need to add a bit of initialization to obtain the desired sketch. To this end, the following is the complete sketch for this project. Keep in mind that comments (shown in green) are purely for the sake of information and do not affect the behavior of the sketch. Thus, you do not need to enter the comments shown here, but commenting your code is a good practice!
/***************************************************************
* Button-controlled LED.
* The LED illuminate when the button is pushed.
*/
// LED pin used for output.
const int ledPinA = 12;
// Button pin used for input.
const int btnPinA = 7;
void setup()
{
// Set the LED pins to OUTPUT.
pinMode(ledPinA, OUTPUT);
// Set the button pins to INPUT.
pinMode(btnPinA, INPUT);
}
void loop()
{
/* Read the value of the digital input pin associated with the
* button. Use an if statement to determine whether or not to turn
* on (i.e., set to HIGH) the digital output pin for the LED.
*
* The digitalRead() function returns a value of HIGH if the voltage
* detected at the pin specified by its parameter is HIGH. If the
* detected voltage is not HIGH, the function returns LOW.
*
* If the digitalRead() function returns HIGH, then the LED
* output pin is set to HIGH and the LED is illuminated. If the
* function returns LOW, the LED is set to LOW and the LED is not
* illuminated.
*/
// Read input from button A and compare to HIGH.
if (digitalRead(btnPinA) == HIGH) {
digitalWrite(ledPinA, HIGH);
}
else {
digitalWrite(ledPinA, LOW);
}
}
Once this sketch is uploaded to the chipKIT board, the circuit will be fully functional. Assuming everything is working properly, you press button A to turn on LED A.
Now that you've completed this project, you should: