To control a circuit, here specifically LEDs, using pushbuttons. Here, the user gets to control the circuit personally with touch.
Equipment
- 1 x red LED
- 2 x pushbuttons
- 1 x 220Ohm Resistor
- 2 x 10kOhm Resistor
- 7 x Wires
- 1 x Breadboard & reference sheet
- 1 x Arduino Uno
Program Details
This experiment is especially cool because we are controlling the circuit with our own hands, we determine the output, LED on or off. This is why the coding is simply a matter of reading our signals. Although there are two buttons attached, only one is of use right now. However, refer to 'Next Steps & Associated Program Modifications' to utilize the other button. Assembling this circuit took around 5-10 minutes. It is simply a matter of carefully following the attachments, so as to minimize any sources of error. See the assembled circuit below.
![]() |
Assembled circuit |
The code for this is all straightforward. The only new concept is using a variable to read the pushbutton and control the state of the LED. First, the code declares the inputs, outputs, and variable 'val', which reads the state. The repetitive part 'loop()', the main function, uses the function 'digitalread' to read the state of the button, input. If the input value is high, unpressed, the LED is turned off. In any other time (else) the input value is low, pressed, the LED is turned on.
Results
First we did not notice that the second button didn't do anything, so we were worried. Soon after, we looked at the guide and realized all was good. This experiment worked as the program suggested. It wasn't as exciting as it sounded, but it was cool to learn how our own movements translate into electrical input/outputs.
Tips
The biggest tip would be to assemble the resistors and pushbuttons carefully, and look over the circuit before uploading the program. Since there are wires passing overhead, make sure the pushbuttons, especially close to the breadboard, is placed correctly the first time.
Next Steps & Associated Program Modifications
Now, to utilize the second button, simply make one button turn the LED on, and the other turn the LED off. This means there will be 2 inputs. See the modified program below, as directed by the kit.
//start by introducing the pins. Order below: LED, button 1, button 2
int ledPin=13;
int inputPin1=3;
int inputPin2=2;
void setup()
{
pinMode(ledPin,OUTPUT); //the LED is now output
pinMode(inputPin1, INPUT); //the first button is now input
pinMode(inputPin2, INPUT); //the second button=input
}
void loop()
{
if(digitalRead(inputPin1) == LOW) //if button 1 is pressed
{
digitalwrite (ledPin, LOW); //turn the LED off
}
else if (digitalRead(inputPin2)==LOW) //OR if button 2 is pressed
{
digitalwrite (ledPin, HIGH); //turn the LED on
}
}
Reference: Spark Fun Inventor's Guide
Now, to utilize the second button, simply make one button turn the LED on, and the other turn the LED off. This means there will be 2 inputs. See the modified program below, as directed by the kit.
//start by introducing the pins. Order below: LED, button 1, button 2
int ledPin=13;
int inputPin1=3;
int inputPin2=2;
void setup()
{
pinMode(ledPin,OUTPUT); //the LED is now output
pinMode(inputPin1, INPUT); //the first button is now input
pinMode(inputPin2, INPUT); //the second button=input
}
void loop()
{
if(digitalRead(inputPin1) == LOW) //if button 1 is pressed
{
digitalwrite (ledPin, LOW); //turn the LED off
}
else if (digitalRead(inputPin2)==LOW) //OR if button 2 is pressed
{
digitalwrite (ledPin, HIGH); //turn the LED on
}
}
Reference: Spark Fun Inventor's Guide
No comments:
Post a Comment