Fun with Arduino 02 Digital Input and Output

Now that we have our Arduino Uno up and running (see Fun with Arduino 01) the moment is here to write our first sketch.

Goal

Let’s set ourselves a goal to achieve: we want the on board LED to light up when we push a button.

The video shows everything in detail. If you rather read … see below.

 

Voltages

OK, so we need to connect a push button to an Arduino pin. Now … where shall we connect the other end of the push button to?

The Arduino Uno is powered by either 5V via USB or by a DC power supply in the range of 8-16V, whereby an on board regulator transfers this down to 5V, because the processor works on 5V. By the way, it is perfectly safe to connect both the USB cable and a power adapter at the same time … the on board circuitry separates the two.

With the processor on 5V this means in order for a digital input to be read correctly the supplied voltage should either be:

  • 0V (GND) for a logical ‘LOW’ or ‘0’
  • 5V for a logical ‘HIGH’ or ‘1’

Never connect voltages higher than 5V, this may damage the input.

20Seems we have two choices to wire our push button. Does it matter which one we choose? For our logic, not really, since we can decide in the software if we want to take action when we read a LOW or when we read a HIGH on the input pin.

Let’s connect the push button to pin 2 and the other end to GND. Now when we press the button, the input will be LOW, so let’s write the code such that when we read a LOW on the input, the on board LED turns on.

Configure pins as input or as output

Arduino pins are configurable. The command to tell a pin what it is going to be is ‘pinMode’. We will write our pinMode commands in the in the ‘setup’ section, since that section only runs once after uploading to the Arduino.

void setup() { // put your setup code here, to run once:
  pinMode(2,INPUT); // push button
  pinMode(13,OUTPUT); // on board LED
}

Why use pin 13 for the output? Well, on an Arduino Uno it is the pin to which the on board LED is hard wired.

Everything written behind // is explanatory text … it is always a good practice to document your code well.

Read the input and activate the LED

The section called ‘loop’ contains the code that keeps running over and over. This is where we read our input, on pin 2, and depending on its state we switch pin 13 on or off. The code to accomplish this is:

void loop() { // put your main code here, to run repeatedly:
  if (digitalRead(2)==LOW) digitalWrite(13,HIGH); // turn the LED on
  else digitalWrite(13,LOW); // turn the LED off
}

These lines are quite self explanatory. You could wonder why the double ==? That’s because in Arduino code a single = is reserved for assignments of the type my_variable=15. For comparisons a double == is required.

OK … let’s upload this code and try it out …

HELP!? It does not work … why not?

Oops. After uploading to the Arduino the LED always burns, even when the push button is not pressed. Why does it not work?

Our code is perfect … that’s not it. There is something wrong with our hardware. Let’s look at the schematic again. When we press the button, the input is connected to GND. That is OK. But what if we release the button? Then the input is floating … it is not connected to anything! That is never a good thing … a floating input can read either state, LOW or HIGH, based on accidental circumstances or interference signals.

Pull up or pull down resistor to the rescue

30We may never let an input float, we always need to connect them to either 0V or 5V. This can be done by adding a resistor, like shown in the diagram. If the switch connects to GND, the resistor should be connected to 5V, which is why it is called a pull up resistor … it ‘pulls’ the input to 5V so it never floats. Vice versa a pull down resistor pulls an input to 0V when the switch is left open.

It is not too much work to connect extra resistors, but there was a reason why I chose the push button to connect to GND … the Arduino very conveniently has built in pull up resistors available that we can use. They are just not connected by default … we have to tell an input to use it, which we can do in the pinMode command.

Final code

Change the pinMode command in the setup section to read pinMode(2, INPUT_PULLUP); and we’re all set to go and have another try.

The complete code now looks like this:

void setup() {
  pinMode(2,INPUT_PULLUP); // push button
  pinMode(13,OUTPUT); // on board LED
}
void loop() {
  if (digitalRead(2)==LOW) digitalWrite(13,HIGH); // turn the LED on
  else digitalWrite(13,LOW); // turn the LED off
}

YES! That works. We can pat ourselves on the shoulder.

On to the next project, which is to make the code more flexible and to use external LEDs.

The video shows all the above in detail.

— 0 —

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s