Help selecting pins to connect devices

I’m having a hard time understanding which pin to use for which connected device/sensor.

I’m using an Arduino UNO.

I seem to have:
5 analog pins
13 digital pins.
Of which a few are marked PWM.
2 digital pins seem to be used for rx and tx.
1 is wired to the onboard led.

So I want to fade an led. So my though process is that I need to use a analog pin. Because I’ll be sending more than on/off (HIGH/LOW. I’ll be sending values. Between 0 and 254. And I’ll be using AnalogWrite.

If I want to use a buzzer - same thing. I need to send a frequency. Not just in and off.

My PIR sensor is on and off. So I’d use a digital pin for that. And digitalRead.

So - I’ll put it out there: I’m wrong. But how should you decide on which pin to use for devices? Is there a though process? When should I use digital, analog or digital PWM?

To fade an LED you would use PWM, so any of the pins marked with a tilde (~) or 3,5,6,9,10,11 on an Uno.

For a buzzer you don't need PWM, you can use the tone() function or the Tone library on any pin.

You got it for the PIR.

Is there a thought process? When should I use digital, analog or digital PWM?

Start with the communications to digital devices. On an UNO, Serial comms to the PC uses pins 0 and 1. So you pretty much never want to put any devices on those pins.

I2C (used for a lot of sensors) uses SDA and SCL. On an UNO, that's actually shared with A4 and A5 so using I2C prevents you from using those pins as analog inputs. Fortunately you can have a lot of devices share the I2C bus.

SPI (faster and usually better than I2C) uses MOSI, MISO, CLK. Once again, those are shared with otherwise-useful pins on the UNO. On Arduinos such as the Due or Mega, it's better to access these pins on the ISP header in the middle of the board, which is in the same place on the UNO for compatibility. SPI also requires one more 'select' pin per device. Pin 10 has a special relationship to SPI (which is never actually used in 99.9% of Arduino projects) so it is always the first choice as 'select'. Any digital pin can be used as select.

Then look at your PWM pins to be used with analogWrite(). If you are controlling motors or dimming lights, then you need these pins.

On an UNO, two pins are special interrupt (input) pins. They are your first choice for high-speed encoders or wakeup-from-sleep buttons. 99% of projects don't need to know this.

Then look at analog inputs, for A0 upwards.

Then look at any pins with special analog restrictions. Only for more-advanced Arduinos. On a Nano, it has 2 analog-only pins which cannot be used as digital inputs or outputs.

Then assign remaining digital pins to digital devices: relays (for output) and switches (for input) usually.

Sometimes you will have special requirements like using a hardware timer for something more complex than PWM. Then you need to know more about which pins are linked to which timers. When you get to that stage, you will know.

tone() disables pwm on 3 and 11 (it uses the same timer) (tone() works on any pin)

Thanks MorganS. That's really helpful.
Don't get the MOSI and MISO and CLK pins. I can't see those on the board. You mentioned in the middle, but can't see what these are on my UNO. The ones I see that you never mentioned, or that I'm not sure about are:


I have a baby project for me to learn. It's using a buzzer, a PIR sensor and 2 x LEDs that I want to fade in and out.

So using your explanation, I'm going with:

Buzzer on A0 - Because ... Not sure. But it seems like a good choice.
PIR on D4 - Because it just uses On and Off, and doesn't use up a PWM pin or one of the 'reserved' pins.
LED1 on 9 - because it uses PWM as I fade it. If it was static on off, I'd use a non PWM digital pin. Maybe 2.
LED2 on 10 - because, as above.

Does that seem OK?


That's the plan.

There's an issue somewhere... While my LEDS fade in and out, I can hear a feint buzzing on the buzzer. :frowning:

For the faint buzzing, that should not happen. Check your wiring, specifically the ground.

The labelling on the UNO dies not explicitly show MOSI etc. Most tutorials for SPI online will tell you to use 11, 12 and 13. On some other Arduinos (maybe Micro or Nano) they are labelled separately.