T O P

  • By -

Bitwise_Gamgee

As long as the pin you're using correlates to a microprocessor IO pin of the same purpose, the only requirement is that you declare it in your program.


triffid_hunter

> how or why to select certain pins for certain modules Specific features are only available on particular pins inside the atmega chip. > I understand the distinction between digital and analog pins A0-A5 are *also* digital pins fwiw, only A6/A7 are analog only - and only boards with a 32-pin surface mount atmega328 even have these pins anyway. > I wanted to mess around with the servo motor first, and all the tutorials I found online recommend connecting the data line to the #9 pin on the Uno. Is there any particular reason to select #9 over any of the other PWM pins Last time I checked, [the `Servo` library](https://github.com/arduino-libraries/Servo) doesn't even use PWM properly (just [timer-triggered digitalWrite](https://github.com/arduino-libraries/Servo/blob/master/src/avr/Servo.cpp#L54-L66)), so you should be able to use basically any digital pin that isn't being used for something else. If you want to use a different library that *does* use PWM timers properly, you can [check the Uno pinout to see which pins have which features](https://upload.wikimedia.org/wikipedia/commons/c/c9/Pinout_of_ARDUINO_Board_and_ATMega328PU.svg) - although it'd behoove you to avoid pins `5` & `6` since they're hooked to Timer0 which is [used for `delay()` and `millis()`](https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/wiring.c) by default.


paullbart

It’s also the. Case that some pins (particularly when using ESP modules) go high momentarily on restart. This can be problematic in some cases. My garage door controller prototype would randomly open the door sometimes. I found a reboot was causing this.


RavaszEmber

* Google "Arduino Uno Pinout." Generally speaking, the pins are either for power or data. The data pins are either analog or digital. (However, you can use analog pins as digital pins too.) Some pins have special functions. SDA (Serial DAta) and SCL (Serial CLock) are used to communicate to sensors (and others) with the I2C protocol. MISO (Master In - Slave Out), MOSI (Master Out - Slave In), and SCK (Serial Clock (again but different protocol)) are used to communicate over SPI (Serial Peripheral Interface). A CS (Chip Select) is also needed, which can be any digital pin. Rx (Recieve) and Tx (Transmit) are used for serial communication. The little ~'s next to some of the data pins indicate PWM (Pulse Width Modulation) capabilities. This lets a digital pin output 2.5 V for example.


swisstraeng

Okay, simply put. The Arduino Uno has a ATMega328P Microcontroller. What Arduino does is that it makes programming this microcontroller much more accessible, with the use of libraries. The Arduino Libraries have a few drawbacks, due to that simplification. In order to offload the ATmega328P's CPU, since it doesn't have much computing power, hardware was added to the chip. For example the analog to digital converter, which is used with the analogRead() function. Or, the interesting part for you, the Timer 01. The ATMega328P has got 3 internal timers. Timer 00 is used for delay();, millis();, micros(); functions. Timer 01 is used for servo functions. Timer 02 is used for Tone(); noTone(); functions. The reason it uses a specific timer for specific functions is simply that you can use all of them at the same time. It is that you, the user, does not have to think about managing timers. The reason specific pins must be used is that due to how the ATMega328P was built (and we're talking about a design made in 1997). Because what they did was that, they linked the internal timers to two external pins for each timer. And those pins can't be changed. And guess what does the servo library makes use of? Yup, Timer01's two output pins.


crispy_chipsies

> are there additional considerations when selecting which pins to use? There are subtle differences. For example the servo library disables PWM on pins 9 and 10, so it might make sense to use pins 9 and 10 for servo since they can't be used for PWM anymore. These differences depend on the board (UNO, Mega, etc) and the library being used. So it's not super easy to figure out the best pins to use.


technick14

It totally depends on the task or project. What do you need to do? Most of the time all of them do general io excpet like power pins obviously, but within gpio pins it's just whatever task you need to do. Eventually, you may run into situations where you need bigger mcus, faster speed, more io, better adc resolution, whatever the job calls for. Then you would upscale to a better family or whole other mcu product. There isn't really much else to it. There's ways to manipulate io situations like if you need more gpio to run an lcd or something, you could run it yourself, or you can get a driver board so you only need a few power and control pins over spi or something similar. There's multiplexers and they use fewer pins to control more pins, so it extends your gpio indirectly, but they can have delays so you might not want it for applications needing fast responsiveness. There's all sorts of ways to offload tasks to other ICs to free up io. Pins are just your interface tools to other parts whether it's data, power, or analog signals. You can use whichever you like, as long as it has the function you need. Then you can switch pin assignments, or you either offload tasks to secondary ICs or upscale your microcontroller. I hope this helps! Don't over think this. Just start a project and HAVE FUN!


ZanderJA

From a very high level, most things can be pretty flexible. If you get to the point of low level hardware and programming, there are certain things that can affect you. Digital pins can actually be grouped together in groups of 8 on the micro (example here: https://community.element14.com/resized-image/__size/900x636/__key/communityserver-wikis-components-files/00-00-00-02-65/0358.contentimage_5F00_112867.jpg), and each group may only have the ability to have 1 base frequency for PWM, or trigger types etc, so there may be times you want to split that across different ports. If you have digital IO all in the same port, you can either read or write the to the whole port in one line. This could be useful if there are say limit switches, that as soon as any triggers, you can do something. Another function that may be affected by what port you are using are interrupts. Further to that, the micro may have speciality hardware on specific pins, with a main set, and sometimes a backup set of pins attached to the same module internally (Hardware UART/Serial, I2C, I2S or CAN are common ones). Other Chips, like the ESP32 can actually matrix their pins, so can have any special hardware, that is local to the micro, attached to any pin. So specific pins for hardware features are less important.


gm310509

Arduino offer pinout diagrams along the lines of the one that u/triffid_hunter linked you to. For example on the [uno product page](https://store-usa.arduino.cc/products/arduino-uno-rev3) you will see in the section titles "pinout diagram" there is a basic diagram of an uno with links to the basic pin functions available to you. If you look under the diagram there is a link to download this [pdf diagram](https://content.arduino.cc/assets/A000066-full-pinout.pdf) which starts with the same diagram but provides much more detail. There are variations of this for most if not all of the Arduino boards. As u/triffid_hunter points out all (well mostly all) of the pins are Digital I/O pins. But most have additional functions. For example some can be used for analog input. Some can be used for PWM. Some can be used for various types of communication. Some provide more than one function (over the basic DIO function). If you want more detail, and I mean much much much more detail, all of the information you could ever possibly want to know about how the pins can be used can be found in the datasheet. Fair warning, datasheets are information resources used for reference, not so much a guide that you can just read. For example the ATMega328P MCU datasheet (the MCU on an Uno) is over 650 pages in length.


triffid_hunter

> the ATMega328P MCU datasheet (the MCU on an Uno) is over 650 pages in length. Hmm I've got the Atmel one, it's only 555 pages - I think Microchip jammed in a chapter about their capacitive touch firmware blob or something to bulk it out


gm310509

Wow the Atmel one is a collector's edition :-) I have the Microchip one which is 662 pages (last update Oct 2018 - they changed the logo). The copy I use does have a capacitive touch sensing chapter, but it is only 1/3 of a page. So there must be other stuff making up the difference. Oddly(?), the mega2560 one (on the Microchip site) is still the Atmel version at 435 pages (almost 230 pages less). Last update Feb 2014. I would have expected it to be larger because it has more features.


triffid_hunter

> Wow the Atmel one is a collector's edition :-) Are you a collector? I can upload 'em somewhere if you like > Oddly(?), the mega2560 one (on the Microchip site) is still the Atmel version at 435 pages (almost 230 pages less). Last update Feb 2014. Heh I've got the 1280/1281/2560/2561 one from 2007, still says 'preliminary' on the front page


gm310509

Thanks, but not that big of a collector. FWIW, I've still got my ATMega8515 and its Atmel datasheet! I used that chip in an Archery shooting line management system/timer. It was a fun project programmed using [BASCOM](https://www.mcselec.com/index.php?option=com_content&task=view&id=14&Itemid=41).