Connect an external LED and button

This invention demonstrates how to connect an external LED and button to your ThinkerShield. The button is an arcade button and the LED is 10mm. For smaller LEDs you may need to add a resistor to your circuit.

Step 1

Step 1

Gather your ThinkerShield; button; LED; and 4 x alligator clip.

Step 2

Step 2

Connect first alligator clip to GND (ground) on your ThinkerShield and to the negative (usually shorter leg) leg of your LED.

Step 3

Step 3

Connect second alligator clip to external pin D2 on your ThinkerShield and to the positive (usually longer leg) leg of your LED.

Step 4

Step 4

Connect third alligator clip to GND (you may use the same ground as before if you like) on your ThinkerShield and to the negative leg of button (in my case it is the very bottom pin).

Step 5

Step 5

Connect fourth alligator clip to external pin D5 on your ThinkerShield and to the positive leg of button (in my case it is the very top pin).

Step 6

Add code: Declare variables

// declare the variables
int led = 2;
int button = 5;

 

Add code: Setup pin modes

void setup() {
// set your led as in OUTPUT and button as an INPUT_PULLUP
pinMode (led, OUTPUT);
pinMode (button, INPUT_PULLUP);
}

 

Add code: Put if/else statement into loop and turn light on and off.

void loop() {

// check if your button has been pressed
 if (digitalRead(button) == HIGH)
 {
 // turn the led on
 digitalWrite (led, LOW);
 }

else
 {
 // turn the led off
 digitalWrite (led, LOW);
 }

}

Add a comment