How to toggle two pin at the same time

Hi ,
I want to assign two pin as output and toggle at the same time. Is it possible in arduino. I see only one pin at a time in Reference manual by using digitalWrite(myPins, HIGH);

Kindly advise.


OK, so there is a way of doing this using "direct port manipulation" (which of course presumes both bits are on the same 8-bit port), but I want to ask you two things

  • When you say "toggle", do you mean both pins toggle in the same direction or might you mean one goes HIGH as the other goes LOW?
  • The ever-important "XY problem" - just why do you want to do this and how critical it is that they change at precisely the same instant rather than within a microsecond of each other?

Hi paul,
Thanks for quick reply.
Answer for your question.

  1. I just give Toggle as an example.all possible directions are possible.
    For example GPIO3 & GPIO4 assigned as output pin.

The transistion should happen at the same time.( I am able to do this in micro chip by assigning port )

  1. Its requires for my project.My concern it should not take one cycle.

for example Let say GPIO3 takes 1 cycle then GPIO4 should not take one more cycle.

I want both GPIO transistion should happen at the same cycle.

( I cannot say in micro sec. eventhough my clock is 16MHz. I need to see instruction cycle to make HIGH to LOW or LOW to HIGH)

Kindly advise.

Thanks and Regards

Eventually found it here.

Assuming GPIO3 and 4 are PORTD bits 3 & 4:
Have this in setup() :
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);

Have one of these in loop when they are to change:
PIND = PIND | 0b00011000; // toggle IO by writing to input port.
PORTD = PORTD & 0b11100111; // clear bits 3 & 4 - or clear only 1 if needed
PORTD = PORTD | 0b00011000; // set bits 3 & 4 - or set only 1 if needed

Thank you Paul & CrossRoads.

Yes i did the above way in At-mega.But now my concern is in RFduino kind of board.I see only Few GPIO's( Only 8 pins).
I am planning to order few boards.Not sure how this work because i don't see PORT pin Masking available in Micro chip & Atmel.

Kindly advise.

Note: Thanks for pointing out the webpage & how to assign in Arduino( I am newbie in Arduino IDE). But i did the above way in Atmel Studio IDE.

Thank you guys for the wonderful support.

Thanks and Regards

OH, it's an RFDuino... why didn't you say so?

There's certainly a way to do a similar thing - but the register names, organization of the bits, etc will be different. You might get better results asking on their forum - I don't think many people here are using the RFDuino (I certainly don't see much discussion of it), and this would be an easy question for someone who's familiar with the guts of RFDuino. It will certainly be described in the manufacturer's reference for the chip (ie, what the registers are called and how they work), though many people don't much enjoy reading through that kind of thing. It's not exactly light reading.

What processor is on an RFDuino?

Thanks DrAzzy & CrossRoads,

DrAzzy :
Yes i am looking for manufacturer reference and how to use it in Arduino IDE.
I have the setup for Arduino + RFduino(Library) IDE in my computer & started coding. but now i see only digitalWrite(GPIO, HIGH) for single GPIO HIGH or LOW. Not port Masking (2GPIO's at the same time).
I can see the chip have only 8 GPIO's. 2 GPIO's already i am using it for serial.

I looked into variant.h file in the \variants\RFduino folder to have insight of the port .But No Luck.can anyone point me to the right place?.
Yes i posted the query in RFduino forum also. I see the question is common for both Arduino & RFduino forum. My apologies if i duplicate the question in both the forum( till now no luck).

I am planning to use Nordic nRF51822 SOC.
Product Number from RFduino : RFD22301 RFduino SMT Module.They use Nordic nRF51822 chip.

I really appreciate your support.

Thanks and Regards

Hi Friends,
I got right direction from RFduino Forum. Yes I think it will work if we give

NRF_GPIO->OUTSET = NRF_GPIO->OUTSET & 0b11101011;// set bits 2 & 4
NRF_GPIO->OUTCLR = NRF_GPIO->OUTCLR | 0b00010100;// clear bits 2 & 4

Able to compile properly. Need to test.

I don't have RFduino board right now. probably I will get it on Monday

Thanks for all your support.

Thanks and Regards