# Port Manipulation

**URL:** <https://forum.arduino.cc/t/port-manipulation/280811>\
**Category:** Due\
**Created:** [January 8, 2015, 11:49am UTC](https://forum.arduino.cc/t/port-manipulation/280811 "2015-01-08T11:49:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pchatill](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@pchatill](https://forum.arduino.cc/u/pchatill)\
**Post date:** [January 8, 2015, 11:49am UTC](https://forum.arduino.cc/t/port-manipulation/280811/1 "2015-01-08T11:49:27Z")

</div>

HI.  
I'm trying to use port manipulation with arduino DUE.  
I had no problems with mega 2560, but the syntax changed...  
I found a very simple example on the forum to toggle pin 2, but it doesn't work.  
What is the correct syntax?  
Thank you for your help.  
(If you have others examples, it can help me...)

(I have already post on the specific due forum, but I'm not sure this forum is very reactif ...)  
Patrick

```cpp
void setup() {
  // put your setup code here, to run once:
  pinMode (2,OUTPUT);

}

void loop() {

// This will create a mask with a one in a 32 bit number to set this bit 27 of PORTB
REG_PIOB_SODR |= (0x01 << 25); // Turn on the LED
delay(1000);
REG_PIOB_CODR |= (0x01 << 25); // Turn off the LED using the CODR register
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 8, 2015, 12:00pm UTC](https://forum.arduino.cc/t/port-manipulation/280811/2 "2015-01-08T12:00:55Z")

</div>

You need a second delay after the last line.  
What is happening is that the last line executes,  
then the code re-enters the loop() section and immediately executes the first line.  
You will not see the flash.

krugman

---

<div class="post-metadata">

**Author:** ![pchatill](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@pchatill](https://forum.arduino.cc/u/pchatill)\
**Post date:** [January 8, 2015, 12:30pm UTC](https://forum.arduino.cc/t/port-manipulation/280811/3 "2015-01-08T12:30:25Z")

</div>

I'm very stupid and very sorry....

But if you have some documentation or examples for the port manipulation on due...  
Thanks

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 8, 2015, 7:02pm UTC](https://forum.arduino.cc/t/port-manipulation/280811/4 "2015-01-08T19:02:11Z")

</div>

> pchatill:  
> I'm very stupid and very sorry....

No you are not stupid, I have done the exact same thing on a number of occasions.  
And don't be sorry, mistakes are how you learn.

> pchatill:  
> But if you have some documentation or examples for the port manipulation on due...  
> Thanks

I do not use the Due, but there should be plenty of documentation here abouts  
[http://arduino.cc/en/Main/ArduinoBoardDue](http://arduino.cc/en/Main/ArduinoBoardDue)

I would have expected to see something along these lines (note that the pinMode function takes the number of the pin conected to the LED as its first parameter)

```arduino
int ledPin = 13;                 // LED connected to digital pin 13

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(1000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(1000);                  // waits for a second
}

```

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [January 9, 2015, 3:11am UTC](https://forum.arduino.cc/t/port-manipulation/280811/5 "2015-01-09T03:11:45Z")

</div>

Krug, are you trying to say that direct port manipulation shouldn't be used? I agree that you should use the framework functions as much as possible but there may be situations where you do require direct port writes. The fantastic thing about the Arduino framework is that it does allow that.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 5:39pm UTC](https://forum.arduino.cc/t/port-manipulation/280811/6 "2021-05-06T17:39:42Z")

</div>

Closed on May 6, 2021, 5:39pm UTC


