Button-Controlled LED

Obtaining Input and Generating Output

Button-Controlled LED:

Obtaining Input and Generating Output


Figure 1. Button-controlled LED circuit.

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.

Before you begin, you should:
  • Understand the material presented in the Blinking an External LED project, i.e., using the state of chipKIT pins to control an external circuit.
After you're done, you should:
  • Understand the use of pull-up and pull-down resistors.
  • Understand relational operators and comparison statements.
  • Understand how a chipKIT board “reads” an external (digital) voltage.


Qty Description Typical Image Schematic Symbol Breadboard Image
1 Two-port button
1 220 Ω resistor
1 10 kΩ resistor

(10 kΩ = 10,000 Ω)

Step 1: Setting up the LEDs

  1. Place the LED in the breadboard as shown in Fig. 2, keeping in mind that the anode is the longer of the two “legs” and the cathode is the shorter. (The cathode in Fig. 2 is to the left and the anode is to the right.) In a subsequent project we'll add more LEDs to the circuit, so, despite the fact that we only have a single LED here, we'll identify this as LED A. (Note that, other than the chipKIT pin used to drive the LED, this portion of the overall circuit is functionally identical to the circuit described in the Blinking an External LED project.)
    Figure 2. LED configuration.
  2. Using a jumper wire, connect the anode of LED A to pin 12.
  3. Connect the chipKIT ground pin (labeled “GND”) to a rail at the bottom of the breadboard. Rails are often referred to as bus strips. There are multiple GND pins and they are all equivalent. In Fig. 2, the jumper wire is connected to the GND pin closest to the 5V pin.
    • If you are unfamiliar with the use of breadboards, please read the breadboard basics that are covered in the page available via the box to the right. The breadboard rails/bus strips are the two bottom-most and two top-most horizontal rows. All the holes along a row are electrically connected. The bus strips are usually separated from the main section of the board and are sometimes marked in red and blue.
    • Bus strips provide a convenient way to connect a particular voltage (or ground) to various points within the main section of the breadboard and/or to the chipKIT board.
    • For the remainder of this project, the bus strip connected to the GND pin will be referred to as the ground bus strip.
  4. 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.)

Buttons and Pull-Down Resistors

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.

Step 2: Setting up the Button

With that background out of the way, we are now ready to build the button portion of the circuit.

  1. Connect the chipKIT 3.3V pin to the bus strip (rail) above the ground bus strip that was set up in Step 1. For the sake of simplicity, the LED is not shown in Fig. 3.
    Figure 3. Button configuration.
  2. Place the button into the breadboard as shown in Fig. 3. Since we'll add more buttons in a subsequent project, we will label this button A. Note the orientation of the button: one pair of legs is inserted to one side of the “valley” and the other pair is inserted on the other side. Most breadboards have a gap or valley that separates columns in the main section of the board (with the board placed as shown in Fig. 3). Even if your breadboard does not have the aforementioned valley, the circuit will still be functional provided that you retain this button orientation.
  3. Using jumper wire, connect the right side of the button to the 3.3V bus strip.
  4. Using a separate wire, connect chipKIT digital I/O pin 7 to the left side of button A.
  5. Connect one end of a 10 kΩ resistor to the left side of the button. (This resistor acts as a pull-down resistor in this circuit.)
  6. Connect the other end of the resistor to the ground bus strip (you can use a wire to connect the resistor to the bus strip, as shown in Fig. 3, or you can directly connect the end of the resistor to the bus strip).
  7. When the button is pushed, it supplies 3.3V to the attached digital I/O pin. The chipKIT Max32 and Uno32 boards are designed to recognize a range of voltages—from a minimum of 2.4V to a maximum of 5.5V —as a HIGH input for the digital I/O pins. That is, when the software function digitalRead() is called, it returns HIGH if the voltage on the pin being read is within this range. The digitalRead() function will be discussed later in this project.

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.

Complete Circuit

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.

Figure 4. Complete circuit showing the button and LED.
Figure 5. Schematic representation of circuit.

A PDF version of this schematic is available here.

digitalRead() Function

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.

if Statements and Comparison Operations

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.

Step 3: Writing the Sketch

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.

Test Your Knowledge!

Now that you've completed this project, you should:

  • Changing only the comparison operation in the if statement, make it so that the LED is on when the button is not pushed and off when the button is pushed.
  • Starting with the sketch as given above, again make it so that the LED is on when the button is not pushed and off when the button is pushed. However, this time do not modify the comparison expression. Instead, modify the statements in the bodies associated with the if and else clauses.
  • Add another LED circuit to the breadboard. Attach the anode of the LED to pin 11 and its associate current-limiting resistor to ground. Modify the sketch so that both LEDs turn on when the button is pushed and both turn off when the button is not pushed.
  • With the additional LED attached to pin 11 as described in the previous item, modify the sketch so that the LED attached to pin 12 is on only when the button is not pushed while the LED attached to pin 11 is on only when the button is pushed.

  • 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.