Beginning Arduino (Ports, Pins and Programming)

68K848

Intro: Beginning Arduino (Ports, Pins and Programming)

All right, so I am learning the art of Arduinoing and thought I'd take you along for the ride...

In this tutorial: 
Pins -- the holes on your Arduino and how to use them
Ports--Collections of pins
Programming--Basic programming with the arduino

STEP 1: What You Need:

Arduino (mine's a duemilanove, but other versions should work as well)
LEDs -- at least one
Resistors --as many as you have LEDs (use this calculator to calculate what you need.  Voltage = 5)
Breadboard (optional)
Wire (optional)
Cable to connect Arduino to Computer
Software (not all that hard to install, not going over it in this tutorial, see this for how to install and other arduino usages get it here)


STEP 2: Programming

The Arduino program consists of three basic parts:  Declaration Section, setup() and loop()
The Declaration Section is where variables are declared.  It is also where any include files are included.
Syntax for declaring variables:  [= ]
   <> and the text they contain should be replaced with the proper code
   [ ] and the text it contains is optional

The setup() subroutine is where you put all code to initialize variables and registers etc.

The loop() subroutine is the main loop of the program.  Like its name sounds, it loops whatever code is in it.


STEP 3: Programming Pins

The Arduino has a lot of pins (14 Digital I/O, 6 Analog In, power and reset)
This tutorial will go over using the Digital I/O pins (specifically pin 13 but they all behave relatively the same)

Making a Blinking Light:
we will use pin 13 because it is connected to an LED that is on the board (near the pin, labeled L)
//Declaration Section    
//--note that any text after the // is not seen by the compiler.  It is only
//      used to make the code easier to read

int ledPin = 13;  //the pin that we will be using

void setup()
{
      pinMode(ledPin, OUTPUT); //Sets the ledPin to output
}

void loop()
{
    digitalWrite(ledPin, HIGH);  //sets pin ledPin High (so pin 13 is set to 1)
    delay(333);                             //waits 333ms or ~1/3 sec
    digitalWrite(ledPin, LOW);  //sets pin ledPin to Low (so pin 13 is now equal to 0)
    delay(333);                             //waits another 333 ms
}   //end of the loop subroutine, so it will now go back to the beginning


STEP 4: Setting Up for Some Port Fun

Now you actually get to attach stuff to your Arduino!!!!!
These instructions use a breadboard, but it should be possible if you have good wire twisting skills to not use one

Step 1:  Place resistors into pins 3-7 of the Arduino
Step 2:  Place the other end of the resistors into the bread board in adjacent rows
Step 3:  Place the long pin of the LEDs into the same row as the resistor (1 LED per resistor)
Step 4:  Place the other pin of the LEDs into one of the rails on the side
Step 5:  Attach a wire from GND to the rail

STEP 5: Programming Ports

Ports are groups of pins.  In the arduino, pins 0-7 are controlled by port D and pins 8-13 are controlled by port B.
Advantages of using ports:  Faster than going per pin, takes up less code for a smaller program
Disadvantages:  Harder to use and debug

I arbitrarily chose port D as my port.
Some basic things to keep in mind for using ports
Variables (X should be replaced by the correct port letter)
DDRX -- Data Direction Register -determines which way data should flow for each pin on the port (0 is input, 1 is output)
PORTX--Data register --holds what data is being output/input to the pins
PINX    --Input pins register

The pins in the register are arranged from lower number = Least Significant Bit to Highest number - Most Significant Bit (so to get pins 0-3 to equal 0 and 4-7 to equal 1 it would be PORTD = B11110000)

Example Code
int delayTime = 333; //It's better coding style to not have any hard-coded constants like in the previous example
byte portD_HIGH = B11111000;
byte portD_LOW =  B00000000;

void setup()
{
     DDRD = DDRD | B11111100;  //Sets up the pins for output
    //a look at what we just did
   //the | symbol is used as bitwise OR (if either bit is 1, result will be 1)
   //this goes through the register, ORing each bit with the binary to the right of the |
   //the B makes the compiler read the 11111100 as binary
   //So in summary it sets pins 2-7 to Output and leaves 1 and 2 (which sometimes
   //have special functions) alone
}

void loop()
{
    PORTD = portD_HIGH;  //sets pin ledPin High (so pin 13 is set to 1)
    delay(delayTime);                             //waits 333ms or ~1/3 sec
    PORTD = portD_LOW;  //sets pin ledPin to Low (so pin 13 is now equal to 0)
    delay(delayTime);                             //waits another 333 ms
}   //end of the loop subroutine, so it will now go back to the beginning



All 5 LEDs should now be blinking merrily away.


STEP 6: Fun With Pulsing

So blinky lights are pretty cool, but how about one that Pulses?

The new code we will need to use here is a for loop
the syntax for a for loop is
for(int = ; ; ){

}
i.e.
for(int i = 0; i < 9; i++){

}
will set i to 0, go through the loop, then do i++, which makes i equal 1... until i equals 9.  When this happens, it will jump to the code after the closing bracket (so the code in the for loop will not be executed with i equal to 9)


Here's the code, with no comments so you get to figure out what's going on...:


int ledPin = 13;
byte portD_HIGH = B11111000;
byte portD_LOW =  B00000000;

void setup()                  
{
  pinMode(ledPin, OUTPUT);
  DDRD = B11111100;
}


void loop()
{
  for(int i=0; i<10; i++){
    digitalWrite(ledPin, HIGH);
    PORTD = portD_HIGH;
    delay(i);
    digitalWrite(ledPin, LOW);
    PORTD = portD_LOW;
    delay(10-i);
  }
  for(int i=10; i>0; i--){
    digitalWrite(ledPin, HIGH);
    PORTD = portD_HIGH;
    delay(i);
    digitalWrite(ledPin, LOW);
    PORTD = portD_LOW;
    delay(10-i);
  }

}



8 Comments

Why do you use a variable to define the LED pin (int ledPin = 13; ) when you could simply use #define (#define ledPin 13)? Using a variable wastes memory space that could otherwise be used for code (in a pinch).
so int ledPin = 13;
can be changed
for example become
int Col1 = 5
this tells the arduino that the pin 5 will be called from now on Col1 ?
thanks phevos
Sort of....  This is a bit long-winded, but it should give you a better idea of what is going on here.
saying int Col1 = 5 is only assigning the number 5 to a variable which is of type integer.
The digitalWrite command has two inputs:  The pin number and the value.  So, digitalWrite(Col1, HIGH), takes the pin at the value of Col1, in this case 5, and sets it to HIGH.

"this tells the arduino that the pin 5 will be called from now on Col1"
A bit of ambiguous wording there, you might have the right idea in your head but the way I read it, it is partially wrong.
int Col1 = 5
simply says that for the time being, until Col1 is changed, the value in Col1 will be 5.  We then choose to use this value of Col1 to drive a specific pin.
The part of your interpretation that I don't like is that the way you said it, you implied that pin 5 is directly tied to Col1.  If, for example, we did the following after the int Col1=5:
Col1 = Col1 + 1;
Col1 now will equal 6, and
digitalWrite(Col1, HIGH);
will drive a logic high to whatever is connected to pin 6.

Out of curiosity, what are you working on?  Col1 implies something with columns, like a display...
ok so to sum up
int col1=5 tells arduino that when i write col1 i mean 5 , and when i i type
digitalWrite(col1,HIGH) it will be like digitalWrite(5,HIGH) with 5 representing the pin but if the variable col1 is changed ...ahmmmm destruction?
hehe exactly, messing around with a home made 4x5 led matrix ;P

Nice instructable - one minor point:
In step 5 you have the following line:

The pins in the register are arranged from lower number = Least Significant Bit to Highest number - Most Significant Bit (so to get pins 0-3 to equal 0 and 4-7 to equal 1 it would be PORTX = B11110000)

The last part should say PORTD=B11110000 not PORTX


The code that follows is correct - but a newbie learning arduino from this example might be confused by that line.

Thanks for the catch

I was trying to write in a way that was ambiguous so that people could see how it worked for all ports, but it probably does make more sense writing it your way..
So in case any of you were wondering, yes this is my first arduino project where I actually wrote the code.  The one other I have done is a capacitance meter project found here.  Instructable on how to make the capacitor I was testing coming soon (it involves aluminum foil and waxed paper wrapped around a dowel)