Hard coded pin definitions?

I came across this code in a sketch and wonder, are these pins hard coded or did I miss their definitions?

   pinMode(SCK,OUTPUT);
   pinMode(MOSI,OUTPUT);
   pinMode(SS,OUTPUT);
   pinMode(MISO,INPUT);

Some Arduinos have these pin names written on the board. Arduino/Genuino Micro, for example. Some Arduinos such as the Due use different pin numbers for these functions.

Doing it this way makes your code more portable. But if you only plan to use one board and it only has the pin numbers marked, then use the numbers.

Those pins are hardwired to the processor and as such, you have little or no control over them. Each board will have dedicated pins attached and, as has been mentioned, will often be labeled. Get the pinout diagram for the board you are using to see which pins are being referenced. When invoking the avr-gcc it is necessary to specify which processor you are compiling for. This is done for you within the IDE when you specify the board in the Tools menu. With the processor ID, the compiler will pull in a board specific library that contains all the pin mappings for that processor/board combination.

Using those names is an abstraction that will enhance portability to some extent, but it never hurts to know what's being hidden.

First thnx to all who replied. I ask because I am using an Adafruit 2050 LCD Touchscreen with micro SD card slot and would like to log some data. The “Datalogger” example in the IDE uses “pinMode(SS,OUTPUT); - Arduino pin 10” to define the chip select for the SD card but the 2050 already uses that pin for the TFT_CS for the screen controller.

// The display uses hardware SPI, plus #9 & #10
#define TFT_RST -1 // dont use a reset pin, tie to arduino RST if you like
#define TFT_DC 9
#define TFT_CS 10

Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

The part that confuses me is that the datalogger sketch calls out pin 4 for the SD card CS and not pin 10 so why define pin 10 (SS) at all for the SD card CS when it is already defined for the TFT_CS and will this cause a conflict?

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
[b]const int ccs = 4;[/b]

File dataFile;

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(SS, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(ccs)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1) ;
  }

I posted both sketches earlier under “Datalogger with 3.5 lcd touchscreen” but got 0 replies. I’ve attached both sketches again to this reply so have a look at them both. Just remember that the VIZIO sketch is in development.

Datalogger.ino (1.75 KB)

VIZIO_Power-up_V2B.ino (4.81 KB)

SS is Slave Select if Atmega is SPI slave. you can choose any pin to select the slaves. it is common to use SS as select pin for first slave, because SS must be OUTPUT otherwise the chip will 'fall' to slave mode. On Mega SS is 53 on Uno 10. You can use any pin as slave select of the SD. Set the pin in SD.begin and wire it to SD SS/CS pin

1. By default, physical Pin-16, 17, 18, and 19 of ATmega328P MCU are input lines (without internal pull-up resistor) and are associated with signals PINB2, PINB3, PINB4 , and PINB5 respectively.
io-1.png

2. In arduino UNO Board, the above pins/signals of Step-1 are routed to digital pins 10, 11, 12, and 13 respectively.
io-2.png

3. When we enable the SPI Interface of the ATmega328P MCU by including these lines (#include<SPI.h>, SPI.begin():wink: in a sketch, the pins/signals of Step-1 take over their alternate functions as per following definitions:
io-3.png

Pin-16 : SS/ (Slave Select) as output line
Pin-17: MOSI (Master Out Slave In) as output line
Pin-18: MISO (Master In Slave Out) as input line
Pin-19: SCK (Serial Clock) as output line

io-1.png

io-2.png

io-3.png

The hardware SS pin must be defined as an output to put the hardware SPI module into master mode. If you leave it as a default input then the SPI module acts as a slave. (It’s VERY rare to have an Arduino as an SPI slave.)

Even if you aren’t using that pin or you’re using it for some other function, it must be an output.