I have a code that reads the value of a sensor and because it needs to be very fast it does not use digitalRead but a strange form of reading. In the top of the code it shows:

int irpin = 2;

I have no idea why this PIND referes to my arduino pin 2, but it only work if my sensor is in pin 2. If I want to change to pin 9 and only change the irpin varialbe it does not work. Why?

To read the sensor I use this code:

while (IRSENSOR & (1 << irpin )) {

Have no idea why this work. Does anyone know this kind of programming with arduino?

  • 3
    That's not a strange form of reading, that's the normal form of reading. The Arduino libraries are the strange form of reading. Dec 3, 2014 at 19:19
  • Agree, but I am a beginner, sorry. Do you know how to create a variable/const of PIND without using define?
    – Samul
    Dec 3, 2014 at 20:22

2 Answers 2


PIND refers to the arduino's IO register for input in port D

Without arduino, one has to refer to pins by specific bits in 3 different sets of registers for each port (PIN*, DDR*, and PORT*; B,C, and D).

The arduino code makes it simpler to pick out a single pin by hiding all of this, but it does make reading a pin take much longer.

In the code, the specific pin is being selected by masking off all but the "irpin" bit in PIND, so changing irpin will pick a different bit and thus a different pin.

Pin 9 is actually bit 1 of port B, not D, so see if this works:

int irpin = 1;

alternatly, using a reference instead of define,

volatile uint8_t& IRSENSOR = PINB;
int irpin = 1;
  • Thank you so much, it worked perfectly! One final question: I would like to change #define pino_sensor_infravermelho_PIN PIND -->> const byte pino_sensor_infravermelho_PIN = PIND; BUT it does not work, IDE does not allow me to compile. How to solve this?
    – Samul
    Dec 3, 2014 at 20:16
  • How did you know it's 1? It worked but I would like to know how to discover it!
    – Samul
    Dec 3, 2014 at 20:27
  • I just looked at this cool image I found on google to get the pin numbers, pighixxx.com/test/portfolio-items/uno/?portfolioID=314 If you are really interested in how all these registers work it is all described in the atmega328 datasheet.
    – BrettAM
    Dec 3, 2014 at 20:32
  • You can get rid of the #define with a reference to a volatile unsigned 8 bit value. See my edit for the code
    – BrettAM
    Dec 3, 2014 at 20:45
  • 1
    @Samul: I don't see any difference between the two, other than one shows the Arduino and the other shows the IC. Dec 3, 2014 at 23:59

Reason explained by @BrettM. To be more perfect use macros everywhere, e.g.:



Arduino's digitalWrite(),digitalRead(),pinMode() functions are useless for more serious coding because they are 1) slow 2) you cannot address pins not connected to headers (e.g. at Arduino2560).

  • Thank you so much! do you know how to declare those 2 variables without using define?
    – Samul
    Dec 3, 2014 at 20:20
  • It's rather complex task because depends what is intended usage. To be precise you need consider using in inline assembler, proper resolving variables in debugger when watching, IO vs. MEM access (_SFR_MEM vs. _SFR_IO) etc. PIND, PD2 itself is also #define. I would not experiment with it without good reason to make more reusable and readable code (=writing common libs not hardcoded to particular device), e.g. define port+pin as WORD (hi=port, lo=pin) and implementing own macros.
    – TMa
    Dec 3, 2014 at 20:30
  • BTW: You do not need using define at all, if define is problem: while (PIND & _BV(PD2))
    – TMa
    Dec 3, 2014 at 20:36
  • You are awsome, I will use it inside the while without declaring any variable. Could you please help me understand this: -->> while (porta_pino_sensor_infravermelho & (1 << pino_sensor_infravermelho)) { <<--. I know & is bitwise and and << is bitshift but it does not make any sense to me! Have you ever seen this way of reading a pin HIGH/LOW?
    – Samul
    Dec 3, 2014 at 20:40
  • 1
    @Samul: I recommend retaining the macro definitions. They don't cause any slowdown during runtime and they make the code easier for others (and eventually yourself) to read and understand. Dec 3, 2014 at 23:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.