This is the first part of the series of articles for improving Arduino programming skills.

This is the first part of the series of articles for improving the Arduino programming skills, in this part, you will learn, what is Port Manipulation and how you should use it.

Materials Required:

Arduino (Any ATMEGA328 Compatible)

What is Port Manipulation

As you know that Arduino has several digital and analog I/O pins, these input and output are categorized into ports, wherein, each port has been assigned some set of pins.

While using the Arduino IDE, we make use of pins in a discrete way, that is if we want to turn the LED on Pin 13 on and off, we do something like this:

void setup() {

// initialize digital pin LED_BUILTIN as an output.

pinMode(LED_BUILTIN, OUTPUT);

}

// the loop function runs over and over again forever

void loop() {

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

By looking at the above code it is clear that the only pin whose state needs to be defined in the program is just Pin 13, this makes the programming really easy and beginner-friendly but, if you really want to get a gist of what goes on under the hood, or just curious and want to learn more, read on....

Let us take an example of Arduino Uno, this Arduino development board uses ATMEGA328 Atmel AVR Microcontroller as a central processing unit, and according to the datasheet of the ATMEGA328, the Digital Inputs and Outputs are categorized into the three Ports and are as follows:

B (digital pin 8 to 13)

C (analog input pins)

D (digital pins 0 to 7)

So, Let's say you want to get access to the pin 13th to make it turn on and off,  you have to get access to the pin via the Port B as the pin exists in the port B of the microcontroller along with the 5 other pins, similarly, in port D there are 8 pins from digital pin 0 to 7.


What are the Ports?

We have talked about ports, but what are they?

Ports are nothing but an 8-Bit "Register" in our case, where each bit refers to an individual pin.

For example, you want to access digital pin 5, and as described above, this pin exists in the Port D, now as mentioned earlier that each pin is represented by a bit in the Port so the pin 5 in Port D is sixth-Bit from the left, sixth because the pin count in Port D is starting from 0 and ends on 7, so 8 pins represented by 8 bits where count for the pins starts from the left in ascending order.

Why and How to Manipulate the Ports?

Finally, we got to the crux, which is Port Manipulation.

The reason why we need to manipulate the port is to control the pins, as the pins are the part of Ports so, in order to get access to pins we need to change the bits in Ports, this is, in a nutshell, Port Manipulation.

Now we will write the same program to make the LED blink but in a different way.

Let us start with the conventional Setup:

void setup() {

// initialize digital pin 5 as an output.

pinMode(5, OUTPUT);

}

In the above code, the line " pinMode(5, OUTPUT); " sets the pin 5 as an output, now to do the same thing with Port Manipulation, we need to get the register of the Port that contains Pin 5 (Port D) and manages if the pin will be declared as input or output. The register for the same is :

DDRD - The Port D Data Direction Register - read/write

The Data Direction Register for Port D or DDRD contains 8 bit, for 8 pins of Port D, that is, from pin 0 to 7. To do what  " pinMode(5, OUTPUT); " which is, to declare pin 5 as output, we need to set the 6th bit from the left (why sixth? explained in the previous section) to "1".

Let's do it!

void setup() {

// initialize digital pin 5 as an output.

DDRD = B00000100;

}

As simple as that!

Now let's have a look at what is going on in the loop.

void loop() {

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(5, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

Basically, two functions: "digitalWrite()" and "delay()" are being used in the loop section, now for simplicitie's sake, we will only use Port Manipulation for the digitalWrite() function and leave the delay() untouched.

Now to accomplish what digitalWrite(5,HIGH) does with port manipulation, we need to have a look at another register inside Port D, that is:

PORTD - The Port D Data Register - read/write

This register controls the high or low state of the pins that are declared as outputs, so we have already declared the pin 5 as output with the help of DDRD now we will turn on and off the pin by setting the corresponding bit for pin 5 in PORTD as 1 or 0 for turning output High or Low respectively.

Following is the entire code converted to Port Manipulation:

void setup() {

// initialize digital pin LED_BUILTIN as an output.

DDRD = B0000010;

}

// the loop function runs over and over again forever

void loop() {

PORTD = B0000010; // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

PORTD = B0000010; // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

Congratulation! for making it till the end!

I hope you enjoyed the article, if you did, please like and share it, and stay tuned for more upcoming "Improve Your Arduino Skills" tutorials.

        // Your code here
    
Utsource Parts
share how to, diy, homemade, circuit design and new project ideas.

Categories