Arduino Mega - 54 or 64 digital pins

I was wandering how many digital pins I can use as an output?

this picture from the official Arduino site showed a total of 69 digital pins:

also from the official site:
" The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4 UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP header, and a reset button."

can I be sure I indeed have 69 Digital pins and I can use all of them?

if I do:

void setup(){
for (int i = 0; i < 69; i++)
{
pinMode(i, OUTPUT);
}

will I be able use all the above pins as a digital OUTPUT ?

Arduino Mega 2560 uses ATmega2560 in a 100-pin package, so you can use the pins as shown.

Please see page:2 of the datasheet at the link for your post.

You cannot easily use pins 0 & 1. They are used for communications with the PC/laptop, for uploading your sketch and when using the serial monitor.

If you need 69 output pins, you can have that many, or even more, on any Arduino, for example Nano or Pro Micro.

so apart of pin 0 and 1 that reserve for communication with computer, can I use all the remain 67 Pins as digital outputs? if I declare them to be an output as the follow:

void setup(){
for (int i = 2; i < 69; i++)
{
pinMode(i, OUTPUT);
} 

Yes you can.

1 Like

Wait! :open_mouth:

This doesn't goes 69

Fix

for (int i = 2; i <= 69; i++)
//                ^^
1 Like

Depends on what you're going to do with them.
The MCU has pin/port current limitations.
Tell us more about your project.
Leo..

I'm gonna use all 60 pins to open a switch for driving small LEDs. so each pins of the 60 pins of total will draw no more then: 5V / 2200R = 2.27mA

the 2200R is the resistor between the arduino GPIO pin to the 2N3904 Base who is the switch transistor for the LED

The Mega MCU has four VCC/GND pins, which allow for 100mA each, so 400mA absolute max chip current. So you're ok with 2.27mA per pin.

Using 60 transistors could be a nightmare. Is this for your 12volt/3watt LEDs?
2.27mA seems too low for a 2N3904 to fully saturate with 250mA collector current.
Leo..

The 60 transistor is for driving 60 individual 10mm LEDs. not for 12V/3Watt LEDs.
I have already drive 40 of them with 40 2N3904 using 2k2 resistor from the Arduino GPIO pins to the transistor base. it seems to work just fine/

@R_Gol, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for advise on (nor for problems with) your project.

Replace the 2n3904 with a suitable mosfet and the current consumption of your microcontroller drops to virtually nothing.

Any reason why you need those 10mm leds and e.g. an array of WS2812's doesn't get the job done? What you're doing sounds rather..well, odd. What's the application?

I will look for a suitable MOSFET for next projects. I'm afraid I have only those 2N3904 so I will have to use them for that project.

I need to use individual LED that will be places far array from each other, therefore I can't use a led strip as each data is series connect to the next one..

Iv'e checked my circuit using the 2N3904 transistor and it seems to work just fine with the value I specify.. Why not using it if it is working for what I need in that particular project?

WS2812's don't have to be part of a strip. They come as single element modules as well.

Why use a compromised approach if a better alternative is cheaply available? Suitable mosfets for this purpose are cheap. Even bss123 or 2n7000 would do nicely and they're a dime a dozen - quite literally.

how the data is sends to each single LED/element ?
edit: must be a connection between each LED somehow isn't it?

Is just because all the project was last minute and I could not buy any other transistors so I was forced to use what I had at home .

One single data pin will address hundreds of leds if need be. You address them individually over a serial data connection.

Ok, well...up to you. I hear excuses, excuses, etc.

I'm new to this and therefore still can't get it. Those addressable leds have four
terminals: GND, VCC+, Data in, Data Out (?)

in any case if my LEDs should be far away from each other I will need to connect a wire between each LED data out to the next LED data in. isn't it ?
so apart of using only 1 pin in the arduino instead of 60 (which is quite a big difference). I will still need to connect 60 wires between each LED data in to data out? and also each LED GND and VCC+ ?
Is that the way those LED are used ?

Yes, but you can route the Vcc and GND wires in several ways. The data wire needs to run from one led to the next.
Look up some ws2812 tutorials, it'll make things clear.
How far apart will your leds be?

You could also use shift registers such as a TPIC6B595 to drive as many LEDs as you want using two digital pins.

it will be a few groups of around 5 to 10 LED . each group will be around 15Meter away form the Arduino. the distance between LED in each group should not be to far (around 1 meter I think) but the distances between groups could be between 5 to 10 meter..