RobotGeek 101 : Digital Output 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 Output! This lesson will show you how to control lights, sounds and other devices using the Geekduino's Digital Output Pins. This lesson will also help get you acquainted with the Arduino IDE and programming fo the Geekduino. The following video for this lesson will walk you through everything you need to do to get started with Digital Output.

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 just what is digital output? When a human wants to interact with the human world, they will 'Output' sounds and motions. When your Geekduino/Arduino wants to interact with the world, it primarily uses Digital Outputs.Instead of making a sound, the Geekduino switches an electric voltage on one of it's pins. This output is considered 'digital' because the output voltage can only be in two states - LOW or HIGH.

LOW means that the pin is being switched to 0 volts, essentially turning the pin off. HIGH means that the pin is being held to the operating voltage - in the case of the Geekduino and most Arduino boards, 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

If you were to read the voltage on your Geekduino's pin, it could 'send' you information. For example, if the pin changed from HIGH to LOW you would know that the Geekduino was on and changing the pin status. Now imagine that you programmed the Geekduino to switch its pin on and off at different intervals - setting the pin HIGH for short or long periods before setting the pin LOW again. If you get your timing right, you can make the geekduino send messages in Morse Code!

Now imagine a light that turns on when you connect it to 5 volts of power, and that turns off when you connect it to 0 volts. Now imagine attaching the light to the pin on your Geekduino. Suddenly the Geekduino can send morse code out to the world using a simple light!

The Geekduino can even switch its digital pins at very high speeds - speeds faster than you can see with the naked eye. By switching its pins at these high speeds, the Geekduino can create all communication protocols that other electronic devices can send. Think of it like Morse Code on steroids.

In this lesson we're going to start at the basics - turning a light on and off. We're going to use a Light Emitting Diode or an LED. An LED is an electronic component that generated light when electricity is applied to it. LEDs come in many different forms as we're about to see.

Though we'll use an LED in this video, you can use any digital output board, like the RobotGeek Buzzer or the RobotGeek Relay with this lesson.

Just about every Arduino board has a built in LED pin (usually on pin 13) and the Geekduino is no different. A tiny surface mount LED sits on the Geekduino board that you can control right away. We'll start by controlling the built in LED, and then move on to controlling the RobotGeek LED

Back to Contents

A-digitalOutputOn

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 turn on a single digital pin. Open the following sketch in your Arduino IDE

File > Sketchbook > RobotGeek sketches > RG101 > 01 - digitalOutput > A-digitalOutputOn

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

Every digital pin on the Arduino can be configured as an Output or an Input. 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 13 and set it as an Output. To set the pin we'll use the code

pinMode(13, OUTPUT);

This line tells the Geekduino that we want to use pin #13 as a 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.

Now that the pin is setup, we need a way to control it. That's where the digitalWrite() function comes in. We'll use the keywords LOW and HIGH to set the pin HIGH or LOW. By default a digital pin will be set to LOW , so to turn the LED on we'll set pin# 13 HIGH

digitalWrite(13, HIGH)

If we had wanted to set the pin to be off/LOW we would use the line

digitalWrite(13, LOW)

For this simple sketch we're just going to turn the LED on once so we'll put the digitalWrite() line in the setup() function. By putting the code in the setup() function, we only call the digitalWrite() function once. So let's put our code together into a basic function that will turn the LED on

void setup()
    {
        pinMode(13, OUTPUT);
        digitalWrite(13, HIGH);
    }

    void loop()
    {
        //do nothing
    }
    

If you load this onto your Geekduino, the LED will turn on when the Arduino gets power. Now that's not very exciting is it? Let's add in some extra code to get our LED to blink!

Back to Contents

B-blinkBasic

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

To make the LED blink, we'll need to make the Geekduino wait in-between setting the pin HIGH and setting it LOW (otherwise we won't be able to see the LED blink!).

Open the following sketch in your Arduino IDE
File > Sketchbook > RobotGeek sketches > RG101 > 01 - digitalOutput > B-blinkBasic
void setup()
    {
        pinMode(13, OUTPUT);
    }

    void loop()
    {
        digitalWrite(13, HIGH); 
        delay(1000);            
        digitalWrite(13, LOW);  
        delay(1000);            
    }
    

In this code you will notice that we've moved the digitalWrite() function into the loop() function - this means that our digital write will execute multiple times instead of just once.

You'll also notice that we've added a second digitalWrite() that turns the LED off.

Finally you'll notice the delay() function after each digitalWrite() line. Each time we change the pin on the Geekduino, we want to wait before we change it again so that we can actually see the blink. The easiest way to make the Geekduino wait is to use the delay() function. This function works by waiting for a certain number of milliseconds before proceeding to the next operation. In our example we are waiting 1000 milliseconds, or 1 second, after each digitalWrite().

Load this sketch onto your Geekduino and you should see the you should see the LED turn on for one second, then off for one second, then on for one second. then off for one second...until forever! since our code is in the loop() function, it will keep running the code until you power off the Geekduino or you reprogram the Geekduino.

It is very easy to change the speed on this sketch. By changing both of the 1000 values to a smaller number (like 250) we can make the blink 4 times faster. By making the values bigger (like 4000) we can make the blink 4 times slower! We can even use different two different numbers (like 250 and 4000) to make the LED blink on quickly for 250ms then stay off for 4000ms.

Now let's add the RobotGeek Sensor Shield onto our Geekduino and an LED module. Place the sensor shield onto the Geekduino making sure the shield's pins seat into the Geekduino. Now you'll want to take one of the LEDs included with your LED driver and put it in one of the 2-pin headers on tour LED driver (keep an eye on the image of the LED on the driver to make sure you put the LED in the right way.) Finally plug in the LED module to pin 13 using a 3-pin sensor cable. You should see the LED blinking, just like you saw the LED on the Geekduino blinking.

In fact you can control more than just the LED module. Any Digital Output board can be controlled in the exact same way. This includes the RobotGeek Relay and Buzzer. Just plug any of these boards into pin 13 and they should activate/deactivate just like the LED.

Next, let's take a look at integrating variables into our sketch.

Back to Contents

C-blinkWithVariables

video.png Click here to expand the video

code.png Click here to expand the code

diagram.png Click here to expand the diagram

Variables can help make your life a lot easier when programming. Variables can be very powerful tools, but in this example we're going to use them purely for organization. Open the following sketch in the Arduino IDE.

File > Sketchbook > RobotGeek sketches > RG101 > 01 - digitalOutput > C-blinkWithVariables

You'll notice two new lines at the top of our code

int digitalOutputPin = 13;   
int delayTime = 1000;       

These lines are variable. Think of variables as boxes that can hold numbers. By giving the box a name (like digitalOutputPin) we can easily call on the contents of the box/variable.

digitalOutputPin will hold the pin number that we want to control while delayTime will hold the number of milliseconds to wait between digitalWrite()s

The rest of the code looks similar to our previous example - only instead of directly calling pin 13 and 1000ms, we've replaced the numbers with the variable names digitalOutputPin and delayTime.

void setup()  
{
  pinMode(digitalOutputPin, OUTPUT);  
} 

void loop()
{
  digitalWrite(digitalOutputPin, HIGH);    
  delay(delayTime);                         
  digitalWrite(digitalOutputPin, LOW);     
  delay(delayTime);                        
}

The major advantage of this method is that it is now very easy to change the pin number or delay time. Before, if we wanted to change the delay time, we would have had to change the value in multiple lines of code - but now we can just change a single line to

int delayTime = 250;       

and the delay will change in both places that we called delayTime. This may seem like a small amount of work saved, but as out code gets more and more complex, this can save a lot of time and headaches.

Before we move on, let's try to change the pin that we're controlling. To do this, change 13 to 2 on line 20

int digitalOutputPin = 2;  

Now upload your code to the Geekduino. You should see your LED driver stop blinking - this is because the LED driver is still on pin 13. But if you unplug the LED driver from pin 13 and plug it into pin 2, you should see the LED start to blink again! Previously you would have had to change the pin number in 3 places to make this work, but with variables, you were able to do it in just one line of code.

Back to Contents

Digital Output Reference

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

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

Back to Contents

Feedback Form


Search