Your shopping cart is empty.

Seeeduino MEGA Pin Control

18 Jul, 2014

Seeeduino MEGA Pin Control

The Seeeduino Mega’s Extra Pins

Wait, What?

If you’ve been shopping around for an Arduino Mega-equivalent board for a smaller pricetag than the made-in-Italy original, you’ve probably run across Seeed Studio’s Seeeduino Mega.  The boards use the exact same ATmega2560 microcontroller, with a few hardware differences.  The most noticeable are the reduced size of the Seeeduino Mega and its extra pins.  Here, we’ll be focusing on those extra pins and how to access them in your Arduino program.

Arduino Mega 2560

Is That What the Extra Holes Are For?

You can see several (count: 16) unconnected pins on the Arduino’s ATmega2560.  Seeed Studio decided to hook them up to allow the user access to even more pins.  They wired them to the end of the board, near the Arduino Mega’s two rows of digital pins.  On the Mega, you can see them labeled with names such as PE2 and PJ7, corresponding to their port and bit information.  You need this information to actually use these pins.

Why Is This So Hard?

The problem with these new pins is that the Arduino Mega doesn’t have them, so there’s no reason to support them in software.  However, the Arduino software does allow for control of these pins, but you have to dig around in the source code to know how to set the register information directly.  For example, to set digital port 70 to output, you would use this line of code: DDRH |= 0x02;.

Wait, What?

Don’t worry, we dug around in the source code (and so did this guy) so that you don’t have to.  But here’s some basic information that you’d need to know to understand what’s going on:

  • The AVR family of Atmel microcontrollers stores pin information in its I/O registers.  Arduino uses the ATmega2560’s registers to store three pieces of information for each digital pin:
    • Type of pin (either input or output), stored in the Data Direction Register (DDR)
    • Output write value (for output pins), stored in the PORT register
    • Input read value (for input pins), stored in the PIN register
  • Each of these registers holds information for 8 pins.  In order to control all 86 physical pins at once, we need 11 of each of these three registers, named: A, B, C, D, E, F, G, H, J, K, and L*.
*Yes, I is missing, probably because it looks like L.  You might also say, “but 8 times 11 isn’t 86 at all!”, and to that I’d say, “pins 6 and 7 of the G registers are unused...but if I were you, I’d avoid messing with them in case I’m lying.”

Tell Me Again Why This Is Useful?

To understand what DDRH |= 0x02; does, we need to know what DDRH is, and how bit operations work.  DDRH is the DDR register controlling the pin that we care about.  0x02 expanded into binary is 0b00000010, and it’s clear from this that we’re dealing with the 1th (one-th, i.e. the second one in 0-indexing) bit.  So we’re setting pin H2.  You can easily tell where this is by looking at the Mega’s pin marking.  The |= operator sets the preceding operand to the result of a bitwise OR of the two operands.  Essentially, it copies all the 1s of 0x02 and pastes them into DDRH.  If DDRH were 0b11110000 before the operation, it would be 0b11110010 after it.  That’s how you assign bits in C++.

Basically:

To set a pin as OUTPUT, write a 0 to that pin’s bit in that pin’s DDR.  To then output a HIGH signal to that pin, set its corresponding pin in the PORT register to 1.  Or if it were an input pin, you would need to write a 1 to that pin’s bit in that pin’s DDR before reading its value by checking its bit in its PIN register (0 would be LOW, 1 HIGH).

Hold On, I Didn’t Read the Technical Garbage Because You Said This Is Easy

Yes, alright, fine, we’ll get to the easy stuff.  We’ve made some changes to the Arduino IDE’s source code to allow for normal control of these pins, just as if there weren’t anything special with them.  Instead of pins PH2, PH7, PJ2, PJ3, PJ4, PJ5, PJ6, PJ7, PD4, PD5, PD6, PG4, PG3, PE7, PE6, and PE2 and dealing with weird bitwise operations, you can use the standard pinMode(), digitalWrite(), and digitalRead() functions, but now with pin numbers 70 to 85 (you can also use DPH2, DPH7, DPJ2, etc.).

Check out our GitHub to get the patch files.  And please read the “Seeeduino Mega Extra Pins Hack” section of the readme to learn how to apply the changes to your system’s installation of the Arduino IDE.  Here’s the front-end of the change we made:

The Seeeduino Mega sitting happily with the other Mega boards in the Arduino IDE.

X

 

(Sources: http://urbanhonking.com/ideasfordozens/2009/05/18/an_tour_of_the_arduino_interna/, http://forum.seeedstudio.com/viewtopic.php?f=4&t=380, http://blog.ricardoarturocabral.com/2010/07/seeeduino-mega-atmega-1280-pin.html)
Leave a comment

Please note, comments must be approved before they are published

is added to your shopping cart.
Go to Cart
is added to your wishlist.
Go to Wishlist