Arduino Basics Lesson 1-2: “Input!”

Arduino Basics Lesson 1-2: “Input!”

For the “Arduino For Kooks” course, I recommend you get the Arduino Starter Kit available here.

In the previous lesson, we learned how to have an Arduino “talk” to us by blinking an LED. Now, we’re going to send signals to the Arduino by pressing a button. After completing this lesson, you should be able to attach any digital sensor to the Arduino and get some usable result.

To verify that the Arduino is receiving our signals properly, we’re going to use those signals to trigger an LED and a message in the serial monitor.

The Circuit:

Connect a jumper wire from the +5V pin to one side of the tactile switch. The adjacent (unswitched) leg of the switch is connected to the anode (+) of the LED. A 330R resistor provides over-current protection for the LED and is connected from the cathode to ground.

The switched side of the tactile switch connects with a jumper wire to pin 2 on the Arduino.

Connect the resistor back to the ground pin of the Arduino with another jumper.

The Sketch:

/*
  Input!

  Reads a digital input on pin 2, lights an LED, and prints the result to the Serial Monitor

  This example code is in the public domain.

*/

// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  // make the pushbutton's pin an input:
  pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState = digitalRead(pushButton);
  // print out the state of the button:
  Serial.println(buttonState);
  delay(1);        // delay in between reads for stability
}

One thought on “Arduino Basics Lesson 1-2: “Input!””

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.