RobotGeek 101: Digital Input Introduction

This lesson is part of the RobotGeek 101 Learning Series. If you haven't already, please download the example sketches, and follow through the Geekduino Getting Started Guide to handle the foundations of using the Arduino IDE in conjunction with the Geekduino.

Welcome to RobotGeek 101 : Digital Input. This lesson will show you how to read data from the Geekduino's Digital Input pins and use those signals to control digital outputs. The following video for this lesson will walk you through everything you need to do to get started with Digital Input.

Parts used in this lesson:

These parts and everything you need to complete chapters 1-7 in RobotGeek 101 are available in the RobotGeek Basic Experimenter's Kit.

Contents:

Introduction

So what is digital input? In an analogy comparing humans to machines, a human experiences the world through sensory inputs, such as sight, smell, hearing, and touch. In chapter 3, we likened outputs to human speech. To continue the metaphor, microprocessors can experience the world through the use of sensors, which it uses to "listen" for different voltages across a set of pins to determine the state of the input. This input is considered 'digital' because the input voltage can only be in two states - LOW or HIGH.

LOW means that the input pin is carrying 0 volts, essentially meaning that the pin is off. HIGH means that the pin is carrying the operating voltage - in the case of the Geekduino and most Arduino boards, this is 5 volts.

The following table shows some synonymous terms for HIGH / LOW pin statuses.

Pin Status Voltage Code / Logic Bit Value Schematic Value
Off 0 Volts LOW 0 GND
On 5 Volts HIGH 1 Vcc

The RobotGeek Pushbutton has a built in resistor on the PCB. If you're using a pushbutton with no resistor, check out D-buttonWithPullup on our Digital Input reference page

Back to Contents

A-buttonDirect

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's use the Arduino IDE to read a pushbutton on one digital pin and turn on an LED on another. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 02 - digitalInput > A-buttonDirect

There are two components to using a Digital Input - setting the pin mode, then writing a change to the pin.

In this example, we are using int again, but this time, we're setting it as a 'const', or constant, since it will not change during code, and this saves Arduino memory. To set these as constant, we'll use the code

const int BUTTON_PIN = 2;
const int LED_PIN = 4;

Now that the pins are setup, we need something to pass the state between them. That's where the bool, which stands for boolean, variable comes in. A boolean can only store two values: 0-OFF/LOW 1-ON/HIGH.

bool buttonState;

We will use this boolean to store whether or not the button has been pressed. An 'int' would also work for this, however, since the button only has two states a boolean is more efficient, making an 'int' less appropriate.

Every digital pin on the Arduino can be configured as an Input or an Output. For this lesson, you will first need to set the pin mode for the pin we want to control. In this example, we'll use pin 2 and set it as an Input and pin 4 to Output. To set the pins we'll utilize the definitions we set earlier and use the code

  pinMode(BUTTON_PIN, INPUT); 
  pinMode(LED_PIN, OUTPUT);

These lines tell the Geekduino that we want to use pin #2 as a digital input, and pin #4 as our digital output - it's that easy! We only need to set the pin mode once, so we can put this code in out setup() function.

In the loop, we're going to use a 'digitalRead' function to continuously read whether the button pin is high or low, and store it in the buttonState boolean. We'll do this in the line

buttonState = digitalRead(BUTTON_PIN);

Once the state of the button has been read, we can now use buttonState to tell digitalWrite whether to write a HIGH or LOW state to the LED pin with the following code

digitalWrite(LED_PIN, buttonState);

Now that we have everything set up, the code should have the 'const int' and 'bool' written before the setup, the pinMode lines in setup, and the 'digitalRead' and 'digitalWrite' lines in the loop.

With this code loaded on your Geekduino and the button and LED wired to the proper pins, the LED should light up when the button is pressed.

Back to Contents

B-buttonSerial

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Let's use the Arduino IDE to read a pushbutton on a digital pin on the Geekduino and show us the state of the button in the Serial Monitor on the computer. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 02 - digitalInput > B-buttonSerial

Since we will be sending serial data, we need to initialize the serial port and set the baud rate in the setup with the following code

Serial.begin(9600);

This tells the board to set the baud rate of the serial port to the default 9600bps baud rate.

In the loop, just like in A-buttonDirect, we are using the buttonState boolean in conjunction with the 'digitalRead' of the button pin, but this time, we will be using an if statement to compare the buttonState to the state that we want it to be to run the code contained within the brackets of the if statement. Instead of directly pushing the state of the button to the state of an LED, we will be sending a line of text over the serial connection with the following code

 if(buttonState == HIGH)
  {
    Serial.println("Button is being pressed!"); 
  }  

Notice that there are two equal signs next to each other. This is how a comparison is written. A comparison checks to see if the things to either side of the == match each other. This is different than the single equal sign, as seen in use during our digitalRead, which assigns buttonState to whatever is being read on the button pin.

The serial print line is contained within the if statement. If the condition of buttonState is not HIGH, the code contained within the if statement will not run. If this condition is met, the code contained within the if statement will run as long as the if statement is met.

When you upload and run the code, open the Serial Monitor in the Arduino IDE. Make sure that it is set for 9600 baud rate and "No line ending", and the first thing that should show up is "Serial Test: Print Data when the Button is Pressed" as written in the setup section of the code. When you press the button, the Arduino will send the line written in the if statement, "Button is being pressed!" for as long as the button is held down. You'll notice this line repeats in the Serial Monitor. This is part of how a loop works. As long as the Geekduino is on, it will run the code contained in the loop over and over.

Back to Contents

Digital Input Reference

Now that you've completed the RG101 Digital Input Introduction, you're ready to go onto the next topic : 5.1 Analog Input .

If you are interested in learning more about digital input, check out our Digital Input Reference Lesson. In this lesson we will look at the remaining RG101 examples and provide you with links to advanced digital input topics.

Back to Contents

Feedback Form


Search