In the Button-Controlled LED project, you used a button to turn an LED on or off. In this project, two buttons will be used to implement the digital logic “AND” function. The input logic will be controlled with a simple two-button circuit. The logic operation will then be implemented in the sketch that we upload to the chipKIT™ board. When a certain combination of the two buttons is pushed, a separate output pin on the chipKIT board is set to logic HIGH. This pin will be connected to another LED circuit so, when the pin is HIGH, the LED will light up.
In short, digital logic “reads” several inputs and sends a single resulting output signal. The output will be either logic HIGH or logic LOW, depending on the specific combination of input signals. For example, in an “AND” function reading two inputs, which we will be implementing, the output signal is logic HIGH if the first and the second inputs are both logic HIGH. If you want to know more about how digital logic works, follow the red tab at the right.
The circuit in this project will be similar to the circuit in the Button-Controlled LED project. If you are not familiar with using buttons in a circuit, visit the orange tab at the right.
Qty | Description | Typical Image | Schematic Symbol | Breadboard Image |
---|---|---|---|---|
2 | Two-port button | ![]() |
||
1 | LED | ![]() |
||
1 | 220Ω resistor | ![]() |
||
2 | 10 kΩ resistor (10 kΩ = 10,000Ω) |
![]() |
In the circuit for this project, each button will allow a signal to be sent to a corresponding input pin on the chipKIT board. Looking at Fig. 1 below, when button A is not pressed, all current is flowing through the 10 kΩ resistor to ground. No current is reaching chipKIT pin 7. However, when button A is pressed, it allows current to flow through to the lower side of the button and to the chipKIT board. This drives pin 7 on the chipKIT board to logic HIGH. The same principles apply with button B.
The other portion of this circuit works as follows. Depending on the combination of buttons pressed, pin 11 will be logic HIGH or logic LOW. From that point, this is just a simple LED circuit. If pin 11 is HIGH, the LED will light up. The specifics of what determines the status of pin 11 will be covered when we go over the sketch that will be uploaded to the board.
The schematic for the circuit can be seen in Fig. 2 above.
As was done in the Button-Controlled LED project, if statements and comparison operators will be used to control when the LED turns on or off. Within the if statements of this sketch, logical operators will also be used to implement the digital logic “AND” function mentioned above. Follow the green tab at the right to read about how to use logical operators in C/C++. For more information on if statements and comparison operators, follow the blue tab.
The rest of the code required for this sketch may begin to seem familiar. The chipKIT pins that will be used need to first be defined and then configured appropriately for input or output in the setup() function. The signals coming through the buttons will be read in the loop() function and the appropriate if statements will determine if the LED illuminates or remains off.
// Define button controlled pins
const int btnPinA = 6;
const int btnPinB = 7;
// Define the LED-controlling pin
const int LED_Pin = 11;
void setup ()
{
// Configure buttons for input
pinMode (btnPinA, INPUT);
pinMode (btnPinB, INPUT);
// Configure LED pin for output
pinMode (LED_Pin, OUTPUT);
}
void loop ()
{
// Read the signals from each button
const int btnStateA = digitalRead (btnPinA);
const int btnStateB = digitalRead (btnPinB);
if ((btnStateA == HIGH) && (btnStateB == HIGH)) // BOTH buttons are pressed
{
digitalWrite (LED_Pin, HIGH); // LED turns on
}
else // Neither of the two buttons are pressed or only one is pressed
{
digitalWrite (LED_Pin, LOW); // LED is off
}
}
The digital logic implemented above is “AND” logic; the LED illuminates only when button A and button B are pressed.