I have expanded past the basic Uno and now have a good collection of different boards. For standard protocols like SPI and I2C I can usually find the correct pins on the board by label and I don't have to specify a pin in the code. That said, for chip select or just using other devices through direct wiring, it is unclear to me as to what pin number I should use in code to correspond to the pin label on the board. I have noticed many diagrams that show the board, complete with the label on the pin, a number beside it, then usually a GPIO number next to that and so on. Many times this results in multiple different numbers. Which of these should I use to know what I need to put in my code. Ideally I would use a predefined constant, and I assume that by installing my board in my IDE that it would precompile some headers that bring these constants. How can I discover these constants for various boards? And if I am building something that I plan on installing on multiple different types of boards, what is the typical best practice for coding this? I would assume that there would be some #ifdef statement for each board type and I would have a set of different definitions according to the defined board types. But where do those live in the system so I can discover which names I should use?
Can you give a specific example referring to a board; for example, ESP32 Board (Fig-1) or UNO Board (Fig-2).
Figure-1:
Figure-2:
Unfortunately the information on pin number interpretation is scattered in various files and places in the Arduino core, and is hard to dig out. Furthermore many of the newer processors allow you to reassign pin functions and numbers in the code.
My approach is to focus on one processor at a time and collect what reliable information I can, and keep a separate "Arduino sketch" folder for each processor type, along with useful tables and diagrams.
Always check multiple sources, including the original datasheet for the processor in question.
Why? For example, there are many Arduino Nano drawings and documents out there that describe pins A6 and A7 as being capable of use as DI/DO. They are not. This even appeared on official Arduino documentation for a time.
So check, check, check, and especially, prototype everything before you make circuit boards, for example.
Golan, that is a great example. Let's look at the ESP32 board you provided. I am using a NodeMCU-32C board. Let's look at the top right pin. In your diagram that is D23 label on the board, pin1 in the next column and GPIO23. That same pin on my board is labeled D23, numbered 36 and labeled GPIO23. So what pin number would I use in code? The options would appear to be 1, 23, or 36. That would imply a possibly different number in that board versus the NodeMCU board.
GPIO23 and D23 both strongly suggest to use "23" in your Arduino programs. Try it and see if it works.
But check, check, check as noted above.
ACK on checking if it will work for the purpose, and if there are some internal features that might not allow that pin to be used. That said, if the answer is to use pin 23 in the code, what is the purpose in the diagrams for calling it as 'pin1', or '36'? Those numbers seem to not have any value and just confuse it. Is the rule that I should consider the GPIO number as the golden standard?
Ask the artist who prepared the drawing. I have no idea and also consider those numbers to be a disservice to Arduino users.
I tend to label by functionality, rather than pin assign.
If you have ever done PCB layout, you soon realise than the layout can become quite messy when sticking to the pins chosen in a program.
Imagine if you will, that you have two GPIO pins connected in a certain way in your program, but when you come to layout the PCB, it soon becomes evident that running the trace, would lead you to having to use vias. However if the pins are not specific, then having a layout that is less complicated by switching the GPIO pins in software is a much better option.
This still allows you to name the pins in software by function, then do a table to change the pins to suit your PCB layout.
This is my NodeMCU Board (Fig-1):
Figure-1:
My approach:
1. I accept the Board as "Arduino NodeMCU" as it is in the boards list of Arduino IDE.
2. I consider the inscription D4 of the Board as a a default function for the operation of "Digital Input/Output Operation". I comfortably execute the following codes:
pinMode(D4, OUTPUT);
digitalWrite(D4, HIGH); //built-in LED near the RST Button is ON
3. I consider legend GPIO2 of the Board as the DPin-2 for the signal name "D4" to perform "Digital Input/Output Operation". I comfortably execute the following codes:
pinMode(2, OUTPUT);
digitalWrite(2, HIGH); ////built-in LED near the RST Button is ON
4. I consider "TXD1" as the alternate function of D4 to work as "Transmit Line" for Software UART1 Port. As I am not finding the corresponding "RXD1" line on the board, I do not use it as a UART1 Port; instead, I use SUART(RXD2, TXD2). The codes:
#include<SoftwareSerial.h>
SoftwareSerial SUART(D7, D8);
SUART.begin(9600);
5. All those mentioned above are known to me through reading some scattered documents and trial-and-error.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.