Are you restricted in the use of certain pins?

I have been playing with microcontrollers for a few weeks. I have an Uno, a Mega 2560 and a Nano but I'm asking specifically about the Mega.

Being a newbie (still) I thought you can/could use any pin????

I tried to use pin 1 for an IR Receiver brick but it didn't seem to work? Then I tried pin 0, same thing.

Finally I tried pin 22 and all was sweet. Worked perfectly.

Then I wanted to move the sketch (and mounted breadboard) to the Nano which, of course, doesn't have a pin 22. So I used pin 21 (A7 on the Nano) and that didn't seem to work either. So I used pin 13 and all is okay again.

I read the specifications and I know that pins 0, 1 and 14 - 21 are special communications pins but is that ALL they can do? Can they be used as ordinary digital pins? To switch on and off LEDs, for instance?

I also noticed that pins 50 to 53 have special functions(SPI?)! So can't I use these to switch LEDs either?

I'm surprised to notice there are NO "pinMode" set in my whole Mega sketch (over 600 lines of code and comments)!!!!

My Mega is currently driving a 7 Segment (manually no library), an 8X8 LED module (with MAX7219) and the IR Receiver (manually but I do use IRremote.h to "start" the IR brick etc but no library calls). The 8X8 uses the LedControl.h completely. :)

And there are NO pinModes at all. Could this be why the IR Receiver didn't work on the communication pins?

I could/will just try it but I am hoping for a more informative answer from you smart people :D

Why it worked on pin 22 and pin 13 with no pinModes but won't on pins 0, 1, 21?

Okay, I declared pin 21 as INPUT and it worked :D

Then moved it to pin 0 and declared that as INPUT and THAT worked too.

Still curious why it worked without declaration on pins 21 and 13? :roll_eyes:

So, will any pin work if it is correctly declared (pinMode)?

So can I use all the communication pins and SPI pins to switch LEDs?

I want to use pins 0 to 19 - cathodes and 20 to 49 - anodes to control 600 LEDs....

I would prefer to use two ranges of pins (0-19 and 20-49) so I won't have to use arrays to define the pin numbers. I don't want to use MAX7219's or shift registors etc as I suspect they will be too slow????

Mostly you can use any pin BUT so do other things! For example pins 0 and 1 are used by Serial so if your using that you can't you can use them for an IR sensor. Analog input and PWM are only available on certain pins.

You need to watch out for clashes between different bits of software and different shields.

For example Servo (supplied with the IDE) will not work with the standard motor shield.

And yes of course you MUST set the pins you are using to be input or output.

Mark

For example Servo (supplied with the IDE) will not work with the standard motor shield.

I think that you will find that it works unless/until you want to use PWM to control the motor speed.

Pins 0 and 1 are used by Serial (or Serial1 on a Leonardo) so best not to use them for digital I/O unless all other digital I/O pins are in use already.

Pins A6 and A7 (found on Arduinos that use the SMD version of the ATmega328p) are only inputs to the A/D input multiplexer. They have no associated digital pin. A0-A5 can be used for analog input or digital I/O. On the Arduino UNO the A4 and A5 pins double as the I2C/TWI/Wire bus.

On reset the digital pins are all set to INPUT but as part of the bootloader process some are likely set to OUTPUT so it is always best to use pinMode() if you are using a pin as a digital input or output.

I take you point about shields etc but I doubt I will use them. :)

Well, I lie.... I WILL be using a (blank) shield but only as a means of fixing all the wires and so I can detach the Mega for reprogramming etc.... The Mega will be living in my ceiling :D

The point that ALL (most?) pins are in INPUT mode answers my question why pin 22 and 13 worked without setting pinMode :D

Thank you for your very informative responses :D