Digital input to serial output

I have a project where i am reading numerous digital inputs (15) and directing them to a serial output (serial1), which is a RS232 Output (via MAX3232), monitoring the activity via tera Term. As i connect the digital inputs one at a time to pins 22-36 individual some of the pins give be a dual output. (see screen shot) cant figure out why some inputs behave as expected and other don't.
OUTPUT:
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
INPUTS:0,0,0,0,0,0,1,1,0,0,0,0,0,0,0

here is the code:

#define NUM_INPUTS 15  // Number of inputs
const int inputPins[NUM_INPUTS] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36};

void setup() {
    Serial.begin(115200);   // Serial Monitor via USB
    Serial1.begin(115200);  // RS232 Output to AVL (via MAX3232)
    
    for (int i = 0; i < NUM_INPUTS; i++) {
        pinMode(inputPins[i], INPUT);
    }
}

void loop() {
    String data = "INPUTS:";  // Start the message
    
    for (int i = 0; i < NUM_INPUTS; i++) {
        data += String(digitalRead(inputPins[i]));  // Read each input and append to message
        if (i < NUM_INPUTS - 1) data += ",";  // Add comma between values
    }
    
    Serial.println(data);   // Send to Serial Monitor (USB)
    Serial1.println(data);  // Send to AVL device via RS232
    
    delay(2000);  // Adjust as needed
}

Tie those two pins directly to ground and see if they continue to show "1". Start debugging your wiring.

Not sure what you mean by dual output. Some of the inputs are 0 and some are 1. What were you expecting?

so for example when i connect to pin 22 the reas out shows both 22 and 23 as 1

So there is nothing connected to pin23 yet?
If yes then it is very possible for that to happen.

yes. nothing connected to 23 even though it shows 1 (connected. the only pin connected is 22

Arduino I/O pins have no default state when nothing is connected, they can read 0 or 1 but most of the time read 0. Sometimes they can flip state just from static electricity in the air
So the fact that it reads 1 with nothing connected is possible. If you want all your pins to read 0 when a wire is not connected then you need to connect each pin to ground with a resistor, try 10K

Thanks.
I was just testing.
.y ultimate plan is to have an input into each pin. The input signal would be sent to the pins from a series of external switches. My intent was to monitor the state of those switches.
Do you think that having the input wires in the pins would provide a ground

570-954-7986

Depends on what is on the other side of the wire.
A wire alone will not provide a ground and would probably make thing worse, since it will act as an antenna and pick up all kinds of electromagnetic interferrence

Hi @btoothill ,

as @jim-p wrote the state of GPIO pins of a controller is usually "floating" if not tied to LOW or HIGH potential by internal or external resistors.

The easiest way to handle this is to use

  pinMode(buttonPin, INPUT_PULLUP);

to activate the integrated pullup resistor. However this will pull the pin state always to HIGH if it is not connected to GND. So your button has to connect the pin to GND and your software logic has to consider that

  • switch open = HIGH
  • switch closed = LOW

If you don't like that and the controller you use does not support INPUT_PULLDOWN you have to add pulldown resistors to the pins, e.g. 10 kOhm from each(!) pin to GND.

For further explanation see here https://deepbluembedded.com/arduino-pinmode-function-input-pullup-pulldown/

Good luck!
ec2021

1 Like

What are the switches connect to?

Servo's that control spray nozzles

570-954-7986

The output goes to an avl (automatic vehicle locatator)
That sends the data to a server where it is parced and mapped

570-954-7986

So there may be a high volatge on the switches?
Is the switch maybe disconnecting something like 5V from the servo?

Not really cool to advertise you name and address to the whole world!

No it comes from servo as 12 v then I use a buck converter bro get to 5v before it goes to board

570-954-7986

im going to try that, i can deal with normally high in my device configuration and read low as a change of state

Are you saying you have 15 buck converters, one for each switch?

Be careful that you don't blow up your arduino.
Whatever you are doing the Arduino MUST see either 0V or 5V to know if a switch is open or closed. There must also be a ground connection.

yes i will when i finish testing. they are powered by the 12v external signal of each input. don't like that setup but cant figure out how to get all those 12v inputs down to 5v and retain each individual signal. thought of a multiplexer at one time. your thoughts?

A simple voltage divider using 2 resistors.