Arduino pins

Interfacing hardware, sensors, output drivers, chips.
Post Reply
nielo
Posts: 5
Joined: Thu Apr 08, 2010 11:50 pm

Arduino pins

Post by nielo » Fri Apr 09, 2010 12:04 am

I'm stumped, where's the map to for the physical pins to the software representations? For example, in the temperature library (LibTemperature as I recall) there are the statements:

"pinMode(16, OUTPUT);"
"digitalWrite(16, LOW);"

I assume this is setting a pin on the ATmega328 as an Output and then driving it Low but I can't figure out exactly which pin is being set.

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Arduino pins

Post by paul » Fri Apr 09, 2010 7:50 am

This is some geeky Arduino stuff. It turns out that the analog pins are addressed as 14-19 (on a std Atmga328 based Arduino) when addressed as digital pins. This is IMO an unfortunate part of the Arduino language (although inherited from Wiring). It's forever confusing my students.

Here's the skinny on the temp sensor pinout. - The I2C pins are analog 4 & 5 (aka digital 18 & 19) on the Arduino. So if one uses the 16 & 17 pins for power - one can just "plug the sensor in" - to the end of the analog port.

"pinMode(16, OUTPUT);" Sets digital 16 pin (aka Analog 2) as ground pin
"digitalWrite(16, LOW);"

"pinMode(17, OUTPUT);" Sets digital 17 pin (aka Analog 3) as +5V pin
"digitalWrite(17, HIGH);"

Hope that helps,

Paul

nielo
Posts: 5
Joined: Thu Apr 08, 2010 11:50 pm

Re: Arduino pins

Post by nielo » Fri Apr 09, 2010 5:09 pm

Thanks for the quick reply and the info. As I recall other pins have different names in software than they do on the physical device. Hopefully this will be explained in wiring too.

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Arduino pins

Post by paul » Fri Apr 09, 2010 5:35 pm

The boards tend to have labels with the pin numbers refered to in software.

Paul

bperrybap
Posts: 42
Joined: Wed Jun 03, 2009 3:17 am
Location: Dallas, TX

Re: Arduino pins

Post by bperrybap » Fri Apr 16, 2010 10:47 pm

Then there is also analogWrite(pin, val) where pin is a "digital" pin#.
vs analogRead(pin) where pin# is a "analog" pin#.

So analogWrite() and analogRead() are referring to two different physical AVR pins
but yet use the same pin# "token".

Complicating things is that analogWrite() only works on certain digital pin numbers,
which vary depending on which AVR part you are using. (so much for portability)
Given that analogWrite() has holes in which functions are supported on the digital pins,
it seems like analogRead() should have simply used the same numbering
space as the digital pins rather than have two numbering spaces.

Seems pretty silly in my view.

--- bill

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Arduino pins

Post by paul » Tue Apr 27, 2010 3:56 pm

Don't even get me going on the Arduino language. I teach about 40 students a year, the vast majority new to Arduino and most new to coding in general. There have been buckets of complaints about the Arduino function names on the developers forum and David Mellis, the one person in charge of the whole Arduino code effort, does his best to ignore all of them.

AnalogWrite in particular has come in for lots of criticism. The name as has nothing at all to do with the analog pins (unlike what you would logically assume). It writes PWM values to the BBB pins that are labelled with PWM. It should be called PWMwrite or something similar, so put in some complaints in the Arduino forum, for whatever good you think it will do.

I just restored this helpful line

"The analogWrite function has nothing whatsoever to do with the analog pins or the analogRead function." in the analogWrite docs page on the Arduino site, but it was edited out earlier so it will probably be deleted again soon.

Paul

nielo
Posts: 5
Joined: Thu Apr 08, 2010 11:50 pm

Re: Arduino pins

Post by nielo » Thu Jun 24, 2010 5:35 pm

OK, here we go again, I just bought a Mega.

The reason I bought it is that I have an application that requires more I/O than the Duemilanove has available.

So, I get the Mega and I immediately notice that it has two sets of pins numbered 0 through 13 (the sets for PWM and Analog In) and 2 sets of pins numbered 14 and 15. So again I ask the obvious question of how do I address the physical pins in software if I have more than one pin with the same pin number?

When I started this thread by asking what is essentially the same question regarding the Duemilanove you guys responded quickly with the answer specific to the Duemilanove and I certainly appreciated it. But what I'd really like to know is where is the documentation that covers this subject in general? I hate to keep writing to a forum asking the same question again and again every time I try a new board. So far the only thing I've found is a note on the bottom of the Arduino Mega page that says "Please note that the I2C in not located on the same pins on the Mega (20 and 21) as on the Duemilanove / Diecimila (analog inputs 4 and 5)."

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Arduino pins

Post by paul » Thu Jun 24, 2010 6:49 pm

Nielo,

I'm very sympathetic because I don't agree with the way the Arduino guys have numbered the pins. The analog pins should have the same numbering system as everything else. They keep claiming that the system is for beginners and then doing stupid things like the analog pins having their own numbers.

Anyway here's the story - the analog pins only work, with the analog pin numbers, for the analogRead() function. For everything else, they have other numbers used to refer to same pins. Confused? Hey I didn't design it.

Dig through the docs on analogRead() and this doc

http://arduino.cc/en/Tutorial/AnalogInputPins



Paul

nielo
Posts: 5
Joined: Thu Apr 08, 2010 11:50 pm

Re: Arduino pins

Post by nielo » Fri Jun 25, 2010 1:39 am

I'll take your advice and look there. Thank you very much.

Ok, I looked at the Analog Input Pins page and it's great for the Duemilanove but the Mega, not so much.

"A/D converter
The Atmega controllers used for the Arduino contain an onboard 6 channel analog-to-digital (A/D) converter. The converter has 10 bit resolution, returning integers from 0 to 1023. While the main function of the analog pins for most Arduino users is to read analog sensors, the analog pins also have all the functionality of general purpose input/output (GPIO) pins (the same as digital pins 0 - 13).

Consequently, if a user needs more general purpose input output pins, and all the analog pins are not in use, the analog pins may be used for GPIO.
"

So, as I learned for the Duemilanove in software, when used as GPIO pins, the Analog In pins would map physically as follows:

GPIO 14 = Analog In 0
GPIO 15 = Analog In 1
GPIO 16 = Analog In 2
GPIO 17 = Analog In 3
GPIO 18 = Analog In 4
GPIO 19 = Analog In 5

But the Mega actually has 16 Analog In pins. The first 6 are listed above but there's no clue as to what the other 10 are named (in software) when used as GPIO.

Second, on the COMMUNICATION header I have pins marked on the board as 14-21. Same problem, what are those pins named (in software) when used as GPIO (or any other use for that matter)?

Isn't there some sort of table somewhere that lists what each physical pin is named in software under what conditions? Maybe something like the table above except with an entry for each pin.

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Arduino pins

Post by paul » Fri Jun 25, 2010 1:17 pm

Post a request on the "Bugs and Software" topic in the forum, and eventually someone will write some more docs. As I said before, the arduino team has pretty much burned their bridges with me.

Paul

nielo
Posts: 5
Joined: Thu Apr 08, 2010 11:50 pm

Re: Arduino pins

Post by nielo » Fri Jun 25, 2010 3:24 pm

Not your fault but I can't believe there is no existing documentation that lists, for each board, which physical pin is named what in software. It's so fundamental; how did anybody ever learn to use these boards?

bperrybap
Posts: 42
Joined: Wed Jun 03, 2009 3:17 am
Location: Dallas, TX

Re: Arduino pins

Post by bperrybap » Fri Jun 25, 2010 10:03 pm

For the ultimate answer consult the actual source code:
http://code.google.com/p/arduino/source ... _arduino.c
All the arduino pin functions use table lookups to map the naked constants to a port and a bit.

--- bill

floresta
Posts: 93
Joined: Mon Jul 28, 2008 6:52 pm
Location: Western New York, USA
Contact:

Re: Arduino pins

Post by floresta » Sat Jun 26, 2010 9:17 am

It's so fundamental; how did anybody ever learn to use these boards?
The problem seems to be that the perpetrators of the Arduino software have been at it so long that they have forgotten that there are those who are just starting out.

Don

paul
Site Admin
Posts: 735
Joined: Mon May 12, 2008 4:19 pm

Re: Arduino pins

Post by paul » Sat Jun 26, 2010 1:09 pm

Yeah and if they would just open up the damn docs, people would fix them for them. But I guess maybe they're scared of the same idiots (or robots) that keep posting ads for golf clubs on this site.

<rant>

If we wish to look at exactly when and how computers will become a menace to humans, we need look no further than the ingenious malware circling the globe. Another prominent example is the way the parasites in the US banking systems set up their automated systems and fee schedules.

</rant>

Post Reply