arduino mega: use analog pins as digital?

Hey is it possible to use the analog pins as digital pins, and if yes, what numbers are they addressable at?

And if not, got any suggestions for how to fake it?

thanks!
-steve

Just out of interest....

What project are you wanting to use more than 54 digital pins on?

Everyone..... Steve's building a spaceship from an arduino!!! :stuck_out_tongue:

But on a more serious note. I presume that like on the other arduino boards, you just number them following on from the last number of the digital pin. for example if the last digital output is numbered 54 then the analog pins would be 55 (analog 0) 56 (analog 1) 57, 58 and so on...

Please elaborate on what you are wanting to do with all of these pin s because I'm interested now!

Mowcius

i wont go into too much detail, its way past my bedtime on a work day...

I would recommend shifting the 10 analog pins up 3 and putting the analog(digital) pins at numbers 0,1,2 and then I would have thought that if you number them to the 'next' theoretical digital pin number (is it 45 onwards?) then it should work like on the other arduino boards.

I would recommend trying it with something simple such as an LED blink on analog pin 0 (set as "int ledPin = 45" or similar).

Mowcius

I am also thinking of getting an arduino mega just to make some coding much simpler and avoid the use of any extra boards so it would be great if you can use those analog pins as digital pins.

btw... interesting project, I would recommend that you post it up when you have finished it - looks great!

You can also do direct port manipulation on these pins as well they map to the F and the K ports. It is one of the few ports that you get a straight run at so if you need to interface any 8 bit wide devices you can do it with these ports.

Port Pin Function
PF 0 54 A0
PF 1 55 A1
PF 2 56 A2
PF 3 57 A3
PF 4 58 A4
PF 5 59 A5
PF 6 60 A6
PF 7 61 A7
PK 0 62 A8
PK 1 63 A9
PK 2 64 A10
PK 3 65 A11
PK 4 66 A12
PK 5 67 A13
PK 6 68 A14
PK 7 69 A15

2 Likes

Ok, so can I do something like hook V+ to one side of a pushbutton, and the other side to analog 11 on an arduino mega, and then run this type of code:

int pinValue;

void setup(){

pinMode(65, INPUT);
Serial.begin(9600);
}

void loop(){

if(digitalRead(65) == HIGH)
{
pinValue = 1;
} 
else
{
pinValue = 0;
}

Serial.println(pinValue);
}

Anyone? :slight_smile: Is it possible to use analog pins on the arduino mega as digital pullup pins? I'm using the awesome hardware abstraction library for buttons for the rest of my project, and it would be super nice if it were possible to use analog pins as pullup digital pins, but so far it doesn't appear that arduino mega can do that, or I don't know how to do it. I would Love some input... I do know which pin numbers to address them as, but is there some hardware limitation that prevents the analog pins from working as pullup digital?

The Mega analog pins (as digital pins 54-69) do have pull-up resistors. Did you actually try to use them as digitial inputs? You may want to say more about what you tried and the results of your tests

I did indeed try using them as pullup digital inputs, thank you for asking me to clarify, I will be happy to do so in pursuit of knowledge on how to do this correctly. Perhaps I need a wiring diagram to explain how I did what I did and what the results i was expecting, and then the results I got.

This is currently how my circuit is wired up:

and I'm only using the diagram as a loose translation of how it's wired. I am actually paying attention to which pin is connected and which pin is addressed in my code. I do realize that the diagram appears to be pointing at analog 0, which would be digital 54.

I was expecting to be able to address the analog pin as a digital input and use it like I am successfully addressing 20 other tact inputs on my project using the digital pins. (16+4 as seen in above posts)

I was hoping to use the Button HAL Arduino Playground - Button Library for this implementation, but if I can't no big deal, I'd love to understand how I can do this the regular way if the HAL can't do this for this hardware in this instance.

The results I got was that it doesn't appear to be sending current out to let the switch complete the circuit to ground. Am I totally missing the point or misusing the terminology? I'm one of the horde of untrained enthusiasts. When I use the digital pins with the Button library, I have them connected to the digital pin and to ground, and then I address them as PULLUP and they work like a charm.

Try one of the simple button tutorials that directly set the pinMode and pull-ups (changing the pins numbers as needed). There may be something in that library that is not working with the Mega.

Don't forget to add series resistors when driving LEDs with digital output pins, otherwise pin damage will result.

Lefty

Hey, so I know in theory I should be running resistors to my LEDs, but... in practice I'm just not seeing a short or medium term problem with skipping them. What kind of timeframe do you think I can expect to start to see some kind of adverse reaction to running my LEDs like this? I just haven't had problems doing it like this with the arduino. This certainly simplifies the prototyping process to not have to worry about it... and I appreciate that quite a bit. I can spend more time working on the parts of my project that actually are hard to figure out...

FYI, here's a couple of videos of my project at work:

http://www.ustream.tv/recorded/1643277
http://www.ustream.tv/recorded/1643331

Still early days... lots of room to complete the full feature set that I have envisioned, but it's usable today... :slight_smile:

do you think I can expect to start to see some kind of adverse reaction to running my LEDs like this?

Yes, very risky with a good chance of burning out the AVR chips output pin. The pin has a max current rating of 40ma and without a current limiting resistor to a LED you have no control of how much current will flow through the LED.

From the Arduino playground section:

Important Note!

While LEDs will not work when placed backwards, you don't have to worry about whether it will be damaged: as long as there is a series resistor of at least 100 ohms next to it, the LED will survive the experience!

However, using an LED without a series resistor is a sure-fire way to kill it! (You'll see a bright flash and it may turn dark)
Always use a resistor! A 1.0K ohm is a good place to start. We'll cover how to figure out the best resistor value later on.

Lefty

welp, that's what beatseqr version 2 is for, I guess. I'm glad all I need is 100 ohms, because the brightness on these LEDs is right at the edge of being too hard to see in bright ambient light. Fortunately, I do all of my best work in a cave-like environment, so no problems there.

What kind of timeframe do you think I can expect to start to see some kind of adverse reaction to running my LEDs like this?

You will already have damaged the output pins. They may continue to work for some time or could fail at any moment even though now you are not over stressing them. Once damage has occurred the outputs are more susceptible to further degradation even at rates below the maximum.

There is no such thing as "getting away with it" electrons are very unforgiving.

I am surprised you can only just see an LED in daylight with a 100R resistor. Are you multiplexing them as that reduces the brightness and you could reduce the resistor a bit further.

I used 75R in an 8X8 multiplexed matrix I just did with blue LEDs, that is still inside the 40mA limit for the pin but outside the 20mA continuous current limit for the LED. However, because the duty cycle (ratio of on time to off time) is only one eighth this is within the peak pulse current of the LED.

Ok, I think I feel stupid enough, so no further responses required. However, I feel like it's better than I have the courage to attempt such an ambitious project than to not even attempt it at all, so I'm going to try to put down my feeling of stupidity and push forward. I thought the core purpose of the arduino project is to bring electronics down to the level that artists with no electrical engineering education are able to begin learning about this discipline. And maybe the learning path is just hardwired to include the forums and non-traditional teachers who are of varying teaching techniques. I wish there was a more structured learning path so I could spare the forums from the horrifyingly stupid newbie stumbles that I'm invariably going to have. But the only structured learning path for this level of electrical engineering is at universities, and that's just not something I can do with an existing career, mortgage, wife, baby, lack of funds, etc.

And this may seem like the most basic principle to you with the resistor on the LED. I've burned out LEDs in the past, and the arduino just doesn't seem to do that when I plug 'em in directly. But you have such strong words, even though I don't seem to experience the same level of hardware failure that you are indicating is an eventual certainty... I just haven't experienced arduinos failing on this apparently incorrect technique, so in my own interpretation and words... I'm getting away with it, because I just haven't experienced electrons being so unforgiving. I don't mean to dispute your experience, and I hope you will afford me the same courtesy.

So again, thanks for the advice. I'll be sure to add in resistors when I build my collaborating partner's instance of this project.

ok, using Alexander Brevig's button and LED hardware abstraction layer libraries I was able to get all 16 analog pins on the arduino mega to function as digital input and output. I must not know what I'm doing otherwise, because I couldn't get my wiring and code to work without the library. Eh, whatever, it works, that's the bottom line. So:

YES, you can use Arduino Mega analog-in pins as digital input and output. They're numbered 54 (analog 0) through 69 (analog 16).