Skip to main content
Share Your Experience: Take the 2024 Developer Survey
7 votes
Accepted

Why do we use bitwise operators to assign PORTx, DDRx and Pinx?

My question is why do we need to use bitwise operations to make assignements? Because you're not assigning, you're modifying. If you want to turn on just pin 3 (for example) you need to only control ...
Majenko's user avatar
  • 105k
6 votes
Accepted

Arduino Uno R3 to Wemos D1 R2 project migration - Pinout problems

The tricky thing with the WeMos boards is the pin notation is different from other Arduino boards. Sometimes you will see the digital pins on Arduino boards referenced with the Dn notation. For ...
per1234's user avatar
  • 4,178
5 votes
Accepted

Port manipulation in Arduino using byte value

What is the problem in this code? No problem, it does change the bits of port D and B from 0B10101000 and 0B00101010 to 0B01010000 and 0B00010101 respectively. However, microseconds later loop() ...
James Waldby - jwpat7's user avatar
5 votes

Problems with I2C connection on ESP8266 - 12F, which pins should I use?

Unlike most Arduinos, the ESP8266 doesn't have a hardware TWI, so I²C is bit-banged in software. This means that you can use any GPIO pins. By default, Wire.begin() initializes pins 4 (SDA) and 5 (SCL)...
tttapa's user avatar
  • 1,320
5 votes
Accepted

What is the equivilent of PORTx for Teensy (4.0)?

All Teensies support digitalWriteFast out of the box. digitalWriteFast compiles to just setting the bit in the right port register. You can not do that faster by direct register manipulation. However,...
luni64's user avatar
  • 261
4 votes

Equivalent for PORTB in Arduino Mega 2560

The other answer has provided correct information and useful documentation on the question as asked. This answer uses a slightly more portable method of producing a brief pulse on a given digital pin....
James Waldby - jwpat7's user avatar
4 votes
Accepted

Equivalent for PORTB in Arduino Mega 2560

This code sends a pulse on pin PB0, which on the Uno is equivalent to digital 8. On the Mega, digital 8 is PH5, thus you would write PORTH |= _BV(PH5); PORTH &= ~_BV(PH5); The equivalence ...
Edgar Bonet's user avatar
  • 43.2k
4 votes
Accepted

Where is PORTB defined?

PORTB is defined by the "toolchain". It is not defined by Arduino. For instance in my setup the Arduino Uno uses "toolchain" avr-gcc\4.9.2-amtel3.5.3-arduino2\avr. If you grep that folder (location ...
jantje's user avatar
  • 1,382
4 votes

Pin toggle speed

If you compile the code and examine the results you can see how many instructions it will take, and thus how many clock cycles. It's slightly more complex because of what else happens in main() to ...
Majenko's user avatar
  • 105k
4 votes
Accepted

Reserve /dev/ttyUSBx

Yes. You need to create new unique names for them using udev rules. By using the unique serial number, along with the VID and PID of the board you can use udev to create a symbolic link to a device ...
Majenko's user avatar
  • 105k
4 votes

How fast can I read Arduino port D?

Your timing seems reasonable. At a rough guess, without actually compiling it, your code will turn into something along the lines of: IN R24,PORTD ;First read - temp = PORTD - 1 cycle ST X+, ...
Tom Carpenter's user avatar
4 votes
Accepted

Can analog pins be defined as either their analog name (A0) or their digital pin number?

For example, instead of defining A0 for a pinout with the Uno, it could be 14? Yes. In fact A0 is just a placeholder for the number 14. For all these things you should look in the pins_arduino.h file ...
Majenko's user avatar
  • 105k
4 votes
Accepted

USB-C to USB-C cable Arduino is not responding even not powered by this cable

That Nano is not USB-C compliant. Yes, it has the connector, but it doesn't do all the required handshaking to put the USB-C port on the laptop into host (and provide power) mode. This is needed ...
Majenko's user avatar
  • 105k
3 votes
Accepted

While condition with & bitwise of PINx read

SPI transmitt is started by writing one Byte into SPDR. And this code won't return from SPI_MasterTransmit function before it's complete. Transmitt completition is anounced by setting SPIF by hardware ...
KIIV's user avatar
  • 4,752
3 votes
Accepted

Arduino Nano not showing up in /dev

dmesg should be the tool to help you. Run: $ dmesg -w Then hit return a few times (to get some blank lines). Next plug in your Nano. Some text should appear on the screen, which will give you a ...
Majenko's user avatar
  • 105k
3 votes

Arduino Nano (clone) not recognized

It sounds like a blown protection diode MBR0520 in between USB and +5V of the nano.
Jerby's user avatar
  • 31
3 votes
Accepted

How to determine which pins_arduino.h file is used?

In the boards.txt file for the core in question you have a line for each board: xxxx.build.variant=yyyy where yyyy is the name of the folder in variants for the board. For instance, the Uno, has: ...
Majenko's user avatar
  • 105k
2 votes

SAM3X8E (Arduino Due) Pin IO registers

I actually followed the above examples and did many tests that I am comfident the results will aid anyone looking into addressing the registers directly. void setup() { pinMode(25, OUTPUT); ...
Peter Mbiria's user avatar
2 votes

SAM3X8E (Arduino Due) Pin IO registers

This is a code example that flashes a led on pin 33. Code borrowed from above - many thanks for the very helpful explanations :) This is the beginning of a project to talk to a TFT touchscreen display ...
James Moxham's user avatar
2 votes
Accepted

ADC port manipulation

Your modified code doesn't disable the pullup on the pin. PORTC &= ~_BV(ADCChannel);
Ignacio Vazquez-Abrams's user avatar
2 votes
Accepted

I2C on non I2C pins, possible?

You can change the address of the GY-521 by pulling the AD0 pin to Vcc. After that the address will be 0x69 instead of 0x68.
Gerben's user avatar
  • 11.3k
2 votes
Accepted

Performance tuning PORTB manipulation - mask

4 cycles: IN Rd,PORTB CBR Rd,7 ORI Rd,x OUT PORTB,Rd Translation into proper GCC inline assembly syntax is left as an exercise for the reader.
Ignacio Vazquez-Abrams's user avatar
2 votes

while(1) loop blocks program to enter interrupt

The while loop should have no effect on interrupts. Where does run() get called? We'll need to see the code that sets up and calls the code you've shown. But as a sanity check, you could try replacing ...
JRobert's user avatar
  • 15.3k
2 votes
Accepted

Why does "!=LOW" != "HIGH"?

Because ((PIND & (1 << 2)) != LOW) means exactly (PIND & 4) != 0 and that is true if PIND is 4, 5, 6, 7, 12, 13, ... (any number having 4 in it binary), so it yelds 4 != 0 Which ...
gilhad's user avatar
  • 1,420
2 votes
Accepted

How do I configure a pin to be a bidirectional and open collector?

If the other end of the link has the same signal voltage as your Arduino, you can simulate an open collector with a single pin: digitalWrite(pin, LOW); pinMode(pin, OUTPUT); sets the pin to output an ...
Edgar Bonet's user avatar
  • 43.2k
2 votes

Converting code for ATTINY85 to ATMEGA2560. Pin change interupts

Each processor has different registers, things that control stuff like interrupts, input and output. I would not expect code written for one processor, especially if it directly accesses the registers,...
Nick Gammon's user avatar
  • 38.3k
2 votes

Can I disable the TX pin by setting its mode to INPUT while still receiving data on the RX pin of a UART?

Well, I ended up answering my own question in a few minutes of forum research. You can disable the TX pin of the hardware UART with: UCSR0B &= ~bit (TXEN0);
willem.hill's user avatar
2 votes
Accepted

How come some Arduino Nano pins (D3, D4, A3, A4, A6, A7) cannot be set high?

You don't need an array, and your "base" pin is wrong - you start at pin 1, not pin 0. Also pins A6 and A7 can only be analog inputs, not digital outputs. It's simpler to write: void setup() { ...
Majenko's user avatar
  • 105k
2 votes

how to convert the following code into one using direct port manipulation

It is better to use interrupts instead of pooling pins. but this is related to your arduino controller. what is you arduino core controller? if you can select I prefer "Nano 33 IoT " which ...
Saeed Mirshams's user avatar
2 votes

Port manipulation

To read a pin you simply read the port the pin is part of then mask out which pin within that port you want to read. For example to read pin D11 you need to read bit 3 of port B. if (PINB & (1 <...
Majenko's user avatar
  • 105k

Only top scored, non community-wiki answers of a minimum length are eligible