How to Identify pins

Is there a way to see which buttons/switches/selectors are connected to which pin on the Mega2560. I have an existing box (EFIS for a flight simulator) which has to be reprogrammed. I mean not physical but a script/program which shows the pin number if I push a button for instance. I'm a rookie :wink:

No! the Microcontroller has no real world reference.

@caheyne, your topic was moved to a more suitable location on the forum.

Who says that all buttons are connected directly to the Mega. To know that, you will have to open the box and than you immediately know anyway.

I think you can do it with a simple sketch.
This one assumes pins connect to Gnd when buttons/switches are pressed.

byte x;
void setup(){
Serial.begin(9600);
  for (x = 2; x<70; x=x+1+{
  pinMode (x, INPUT_PULLUP);
  }
}
void loop(){
for (x = 2; x <70; x=x+1){
  if (digitalRead(x) == LOW){
  Serial.print ("D");
  Serial.print (x);
  Serial.println (" is low");
  }
}

@CrossRoads, what happens if the 'box' uses external pull-down resistors.

I've done that, they are all connected via a ribbon cable and connection board, impossible to see which button goes to which pin

Then look for a HIGH instead.
Or just report whatever is seen, with a short delay between each,
or read one at a time and use one of them as toggle to indicate to read the next pin.

Okay, sounds like a plan. Do you have a brief instruction how?

Did you see the sketch above?

Yep, I'm gonna try that one, thanks

In case any of the switches are in a matrix, this sketch sets each pin LOW in turn to see if pressing a button connects one pin to another. Buttons that Ground a pin will report a flood of:

Dn is changed when 2 is set low. 
Dn is changed when 3 is set low. 
Dn is changed when 4 is set low.
Dn is changed when 5 is set low.
...

If a button is part of a matrix that connects Pin X and Pin Y it will report:

DX is changed when Y is set low. 
DY is changed when X is set low.

If there is a diode in series, one of the two will be missing.

const byte PinCount = 70;
int IdleValues[PinCount];

void setup()
{
  Serial.begin(9600);

  // Set all of the pins to INPUT_PULLUP
  for (int inputPin = 2; inputPin < PinCount; inputPin++)
  {
    pinMode (inputPin, INPUT_PULLUP);
  }

  // Now read the idle state of each of the pins
  for (int inputPin = 2; inputPin < PinCount; inputPin++)
  {
    IdleValues[inputPin] = digitalRead(inputPin);
  }
}

void loop()
{
  // Make each pin in turn an OUTPUT pin and drive it LOW
  for (int driverPin = 2; driverPin < PinCount; driverPin++)
  {
    pinMode(driverPin, OUTPUT);
    digitalWrite(driverPin, LOW);

    // Read each pin in turn to see if it has changed from the idle state
    for (int inputPin = 2; inputPin < PinCount; inputPin++)
    {
      if (inputPin == driverPin)
        continue;  // Skip the driver pin

      // Report any changes from the Idle state
      if (digitalRead(inputPin) != IdleValues[inputPin])
      {
        Serial.print ("D");
        Serial.print (inputPin);
        Serial.print (" is changed when ");
        Serial.print (driverPin);
        Serial.println (" is set LOW.");
        Serial.flush();
      }
    } // End for inputPin

    // Set the driver pin back to INPUT_PULLUP
    pinMode(driverPin, INPUT_PULLUP);
    
  } // End for driverPin
}

I'm learning, reading and trying to understand what I have to do exactly :wink:

Load up my sketch,
set all the switches & toggles to their inactive state,
open the serial monitor,
and see what you get.
If you're not seeing a flood of "Dx is low" messages, then start operating the EFIS one button or switch at a time and see what you get.
If everything reports low at startup, then change to this and try again.

byte x;
void setup(){
Serial.begin(9600);
  for (x = 2; x<70; x=x+1+{
  pinMode (x, INPUT_PULLUP);
  }
}
void loop(){
for (x = 2; x <70; x=x+1){
  if (digitalRead(x) == HIGH){
  Serial.print ("D");
  Serial.print (x);
  Serial.println (" is high");
  }
}

I'm getting there, but I'm gonna try 2morrow, the wine is tasting to good now :upside_down_face:

I tried but get an error on the sketch!
Any idea?

[quote="CrossRoads, post:13, topic:862437"]

byte x;
void setup(){
Serial.begin(9600);
  for (x = 2; x<70; x=x+1+{
  pinMode (x, INPUT_PULLUP);
  }
}
void loop(){
for (x = 2; x <70; x=x+1){
  if (digitalRead(x) == HIGH){
  Serial.print ("D");
  Serial.print (x);
  Serial.println (" is high");
  }
}

[/quote/home/builder/opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware /home/builder/opt/arduino-builder/hardware -hardware /home/builder/.arduino15/packages -tools /home/builder/opt/arduino-builder/tools -tools /home/builder/.arduino15/packages -built-in-libraries /home/builder/opt/libraries/latest -logger humantags -fqbn arduino:avr:mega:cpu=atmega2560 -build-cache /tmp -build-path /tmp/504243961/build -verbose -prefs runtime.tools.arduinoOTA.path=/home/builder/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs runtime.tools.avr-gcc.path=/home/builder/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs runtime.tools.avrdude.path=/home/builder/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs runtime.tools.arm-linux-gcc.path=/home/builder/.arduino15/packages/arduino/tools/arm-linux-gcc/4.9.3 -prefs runtime.tools.openocd.path=/home/builder/.arduino15/packages/Intel/tools/openocd/0.9.0+0.1 -prefs runtime.tools.dfu-util.path=/home/builder/.arduino15/packages/arduino/tools/dfu-util/0.10.0-arduino1 -prefs runtime.tools.x86-linux-gcc.path=/home/builder/.arduino15/packages/arduino/tools/x86-linux-gcc/7.2.0 -prefs runtime.tools.sketchUploader.path=/home/builder/.arduino15/packages/Intel/tools/sketchUploader/1.6.2+1.0 -prefs runtime.tools.mkspiffs.path=/home/builder/.arduino15/packages/esp8266/tools/mkspiffs/2.5.0-3-20ed2b9 -prefs runtime.tools.bossac.path=/home/builder/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2 -prefs runtime.tools.CMSIS.path=/home/builder/.arduino15/packages/arduino/tools/CMSIS/4.5.0 -prefs runtime.tools.linuxuploader.path=/home/builder/.arduino15/packages/arduino/tools/linuxuploader/1.5.1 -prefs runtime.tools.arm-none-eabi-gcc.path=/home/builder/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4 -prefs runtime.tools.rp2040tools.path=/home/builder/.arduino15/packages/arduino/tools/rp2040tools/1.0.2 -prefs runtime.tools.arduino101load.path=/home/builder/.arduino15/packages/Intel/tools/arduino101load/2.0.1 -prefs runtime.tools.i586-poky-linux-uclibc.path=/home/builder/.arduino15/packages/Intel/tools/i586-poky-linux-uclibc/1.6.2+1.0 -prefs runtime.tools.esptool.path=/home/builder/.arduino15/packages/esp8266/tools/esptool/2.5.0-3-20ed2b9 -prefs runtime.tools.xtensa-lx106-elf-gcc.path=/home/builder/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-3-20ed2b9 -prefs runtime.tools.nrf5x-cl-tools.path=/home/builder/.arduino15/packages/arduino/tools/nrf5x-cl-tools/9.3.1 -prefs runtime.tools.core2-32-poky-linux.path=/home/builder/.arduino15/packages/Intel/tools/core2-32-poky-linux/1.6.2+1.0 -prefs runtime.tools.ctags.path=/home/builder/.arduino15/packages/builtin/tools/ctags/5.8-arduino11 -prefs runtime.tools.serial-discovery.path=/home/builder/.arduino15/packages/builtin/tools/serial-discovery/1.2.1 -prefs runtime.tools.CMSIS-Atmel.path=/home/builder/.arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0 -prefs runtime.tools.arc-elf32.path=/home/builder/.arduino15/packages/Intel/tools/arc-elf32/1.6.9+1.0.1 -prefs runtime.tools.flashpack.path=/home/builder/.arduino15/packages/Intel/tools/flashpack/2.0.0 -libraries /tmp/504243961/custom -libraries /tmp/504243961/pinned /tmp/504243961/sketch_may17a

Compiling sketch...

/tmp/504243961/sketch_may17a/sketch_may17a.ino: In function 'void setup()':

/tmp/504243961/sketch_may17a/sketch_may17a.ino:4:35: error: expected primary-expression before '{' token

for (x = 2; x < 70; x = x + 1 + {

^

/tmp/504243961/sketch_may17a/sketch_may17a.ino:4:35: error: expected ')' before '{' token

/tmp/504243961/sketch_may17a/sketch_may17a.ino: In function 'void loop()':

/tmp/504243961/sketch_may17a/sketch_may17a.ino:15:3: error: expected '}' at end of input

}

^

exit status 1

Couple of typos in it. This compiles:

byte x;

void setup()
{
  Serial.begin(9600);
  for (x = 2; x < 70; x = x + 1 )
  {
    pinMode (x, INPUT_PULLUP);
  }
}

void loop()
{
  for (x = 2; x < 70; x = x + 1)
  {
    if (digitalRead(x) == HIGH)
    {
      Serial.print ("D");
      Serial.print (x);
      Serial.println (" is high");
    }
  }
}

does this makes sense?:

/home/builder/opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware /home/builder/opt/arduino-builder/hardware -hardware /home/builder/.arduino15/packages -tools /home/builder/opt/arduino-builder/tools -tools /home/builder/.arduino15/packages -built-in-libraries /home/builder/opt/libraries/latest -logger humantags -fqbn arduino:avr:mega:cpu=atmega2560 -build-cache /tmp -build-path /tmp/225117299/build -verbose -prefs runtime.tools.arduinoOTA.path=/home/builder/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs runtime.tools.avr-gcc.path=/home/builder/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs runtime.tools.avrdude.path=/home/builder/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs runtime.tools.nrf5x-cl-tools.path=/home/builder/.arduino15/packages/arduino/tools/nrf5x-cl-tools/9.3.1 -prefs runtime.tools.rp2040tools.path=/home/builder/.arduino15/packages/arduino/tools/rp2040tools/1.0.2 -prefs runtime.tools.arduino101load.path=/home/builder/.arduino15/packages/Intel/tools/arduino101load/2.0.1 -prefs runtime.tools.i586-poky-linux-uclibc.path=/home/builder/.arduino15/packages/Intel/tools/i586-poky-linux-uclibc/1.6.2+1.0 -prefs runtime.tools.sketchUploader.path=/home/builder/.arduino15/packages/Intel/tools/sketchUploader/1.6.2+1.0 -prefs runtime.tools.bossac.path=/home/builder/.arduino15/packages/industruino/tools/bossac/1.7.0-industruino -prefs runtime.tools.dfu-util.path=/home/builder/.arduino15/packages/arduino/tools/dfu-util/0.10.0-arduino1 -prefs runtime.tools.x86-linux-gcc.path=/home/builder/.arduino15/packages/arduino/tools/x86-linux-gcc/7.2.0 -prefs runtime.tools.serial-discovery.path=/home/builder/.arduino15/packages/builtin/tools/serial-discovery/1.2.1 -prefs runtime.tools.xtensa-lx106-elf-gcc.path=/home/builder/.arduino15/packages/esp8266/tools/xtensa-lx106-elf-gcc/2.5.0-3-20ed2b9 -prefs runtime.tools.flashpack.path=/home/builder/.arduino15/packages/Intel/tools/flashpack/2.0.0 -prefs runtime.tools.ctags.path=/home/builder/.arduino15/packages/builtin/tools/ctags/5.8-arduino11 -prefs runtime.tools.openocd.path=/home/builder/.arduino15/packages/arduino/tools/openocd/0.11.0-arduino1 -prefs runtime.tools.arc-elf32.path=/home/builder/.arduino15/packages/Intel/tools/arc-elf32/1.6.9+1.0.1 -prefs runtime.tools.arm-none-eabi-gcc.path=/home/builder/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4 -prefs runtime.tools.CMSIS-Atmel.path=/home/builder/.arduino15/packages/arduino/tools/CMSIS-Atmel/1.2.0 -prefs runtime.tools.mkspiffs.path=/home/builder/.arduino15/packages/esp8266/tools/mkspiffs/2.5.0-3-20ed2b9 -prefs runtime.tools.core2-32-poky-linux.path=/home/builder/.arduino15/packages/Intel/tools/core2-32-poky-linux/1.6.2+1.0 -prefs runtime.tools.CMSIS.path=/home/builder/.arduino15/packages/arduino/tools/CMSIS/4.5.0 -prefs runtime.tools.arm-linux-gcc.path=/home/builder/.arduino15/packages/arduino/tools/arm-linux-gcc/4.9.3 -prefs runtime.tools.linuxuploader.path=/home/builder/.arduino15/packages/arduino/tools/linuxuploader/1.5.1 -prefs runtime.tools.esptool.path=/home/builder/.arduino15/packages/esp8266/tools/esptool/2.5.0-3-20ed2b9 -libraries /tmp/225117299/custom -libraries /tmp/225117299/pinned /tmp/225117299/sketch_may17a

Sketch uses 2752 bytes (1%) of program storage space. Maximum is 253952 bytes.

Global variables use 199 bytes (2%) of dynamic memory, leaving 7993 bytes for local variables. Maximum is 8192 bytes.

Upload started

Programming with: Serial

Flashing with command:/Users/caesar/.arduino-create/arduino/avrdude/6.3.0-arduino17/bin/avrdude -C/Users/caesar/.arduino-create/arduino/avrdude/6.3.0-arduino17/etc/avrdude.conf -v -patmega2560 -cwiring -P/dev/cu.usbserial-A50285BI -b115200 -D -Uflash:w:/var/folders/bv/hpj10x2d2bl6pms5jzkwv5hm0000gn/T/arduino-create-agent021010123/sketch_may17a.hex:i

avrdude: Version 6.3-20190619

Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/

Copyright (c) 2007-2014 Joerg Wunsch

System wide configuration file is "/Users/caesar/.arduino-create/arduino/avrdude/6.3.0-arduino17/etc/avrdude.conf"

User configuration file is "/Users/caesar/.avrduderc"

User configuration file does not exist or is not a regular file, skipping

Using Port : /dev/cu.usbserial-A50285BI

Using Programmer : wiring

Overriding Baud Rate : 115200

avrdude: stk500v2_ReceiveMessage(): timeout

avrdude: stk500v2_ReceiveMessage(): timeout

avrdude: stk500v2_ReceiveMessage(): timeout

avrdude: stk500v2_ReceiveMessage(): timeout

avrdude: stk500v2_ReceiveMessage(): timeout

avrdude: stk500v2_ReceiveMessage(): timeout

avrdude: stk500v2_getsync(): timeout communicating with programmer

avrdude done. Thank you.

That says it compiled ok, @wildbill had found a couple of syntax typos I made.

As for why it won't upload, that's a different problem. Maybe the 2560 doesn't have a bootloader installed? Maybe a bad cable? Maybe a wrong driver? Maybe th wrong COM port selected?

Okay, thanks I will look into that.
The good news is that I just got in contact with the manufacturer of the EFIS and hopefully he will send me the list of PIN connections.
Thanks so far! Very useful in my learning process! :sunglasses:

If the '2560 does not have a bootloader, you may have to load one, then you can download sketches via serial port.
This one uses 2 COM ports - select one for ICSP header programming, and the other port can be used for USB/Serial comm's if needed.
$7.77, free shipping in the US.