Input Pins and Output pins randomly gets high or erratic

Id like to ask some help, the input pins randomly gets high when other sensor is active, as well as the output randomly gets high.

I already have a pull down 4.7k resistor on input and output but still the function is erractic.

Please see the attached code/program on Arduino nano.

Overall Idea Function

This program reads 7 touch sensors (A0–A6) and controls 7 outputs (D2–D8).

Only ONE sensor is allowed to be active at a time.
When exactly one sensor is touched, it turns ON its corresponding output for 1 second.

Pin Setup

  • Inputs (sensors):
    • A0 β†’ touchSensor1
    • A1 β†’ touchSensor2
    • …
    • A6 β†’ touchSensor7
  • Outputs:
    • D2 β†’ for sensor 1
    • D3 β†’ for sensor 2
    • …
    • D8 β†’ for sensor 7

------Here is the Code----

const int D2 = 2;
const int D3 = 3;
const int D4 = 4;
const int D5 = 5;
const int D6 = 6;
const int D7 = 7;
const int D8 = 8;

const int A0 = 14;
const int A1 = 15;
const int A2 = 16;
const int A3 = 17;
const int A4 = 18;
const int A5 = 19;
const int A6 = 20;

int touchSensor1 = 0;
int touchSensor2 = 0;
int touchSensor3 = 0;
int touchSensor4 = 0;
int touchSensor5 = 0;
int touchSensor6 = 0;
int touchSensor7 = 0;

int triggerDuration = 1000;

int outputDefault = HIGH;

int outputTriggered = LOW;

int inputDefault = LOW;

int inputTriggered = HIGH;

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

  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D6, OUTPUT);
  pinMode(D7, OUTPUT);
  pinMode(D8, OUTPUT);

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  pinMode(A6, INPUT);
}

void loop() {
  touchSensor1 = digitalRead(A0);
  touchSensor2 = digitalRead(A1);
  touchSensor3 = digitalRead(A2);
  touchSensor4 = digitalRead(A3);
  touchSensor5 = digitalRead(A4);
  touchSensor6 = digitalRead(A5);
  touchSensor7 = digitalRead(A6);

  // if analog only
  /*
  int rawA6 = analogRead(A6);
  touchSensor7 = (rawA6 > 500) ? HIGH : LOW;
  */

  digitalWrite(D2, outputDefault);
  digitalWrite(D3, outputDefault);
  digitalWrite(D4, outputDefault);
  digitalWrite(D5, outputDefault);
  digitalWrite(D6, outputDefault);
  digitalWrite(D7, outputDefault);
  digitalWrite(D8, outputDefault);

  if (touchSensor1 == inputTriggered && touchSensor2 == inputDefault && touchSensor3 == inputDefault && touchSensor4 == inputDefault &&
      touchSensor5 == inputDefault && touchSensor6 == inputDefault && touchSensor7 == inputDefault) {

    digitalWrite(D2, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor1 is ");
    Serial.print(inputTriggered);
    Serial.print(" and D2 is ");
    Serial.println(outputTriggered);

  } else if (touchSensor2 == inputTriggered && touchSensor1 == inputDefault && touchSensor3 == inputDefault && touchSensor4 == inputDefault &&
             touchSensor5 == inputDefault && touchSensor6 == inputDefault && touchSensor7 == inputDefault) {

    digitalWrite(D3, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor2 is ");
    Serial.print(inputTriggered);
    Serial.print(" and D3 is ");
    Serial.println(outputTriggered);

  } else if (touchSensor3 == inputTriggered && touchSensor1 == inputDefault && touchSensor2 == inputDefault && touchSensor4 == inputDefault &&
             touchSensor5 == inputDefault && touchSensor6 == inputDefault && touchSensor7 == inputDefault) {

    digitalWrite(D4, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor3 is ");
    Serial.print(inputTriggered);
    Serial.print(" and D4 is ");
    Serial.println(outputTriggered);

  } else if (touchSensor4 == inputTriggered && touchSensor1 == inputDefault && touchSensor2 == inputDefault && touchSensor3 == inputDefault &&
             touchSensor5 == inputDefault && touchSensor6 == inputDefault && touchSensor7 == inputDefault) {

    digitalWrite(D5, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor4 is ");
    Serial.print(inputTriggered);
    Serial.print(" and D5 is ");
    Serial.println(outputTriggered);

  } else if (touchSensor5 == inputTriggered && touchSensor1 == inputDefault && touchSensor2 == inputDefault && touchSensor3 == inputDefault &&
             touchSensor4 == inputDefault && touchSensor6 == inputDefault && touchSensor7 == inputDefault) {

    digitalWrite(D6, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor5 is ");
    Serial.print(inputTriggered);
    Serial.print(" and D6 is ");
    Serial.println(outputTriggered);

  } else if (touchSensor6 == inputTriggered && touchSensor1 == inputDefault && touchSensor2 == inputDefault && touchSensor3 == inputDefault &&
             touchSensor4 == inputDefault && touchSensor5 == inputDefault && touchSensor7 == inputDefault) {

    digitalWrite(D7, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor6 is ");
    Serial.print(inputTriggered);
    Serial.print(" and D7 is ");
    Serial.println(outputTriggered);

  } else if (touchSensor7 == inputTriggered && touchSensor1 == inputDefault && touchSensor2 == inputDefault && touchSensor3 == inputDefault &&
             touchSensor4 == inputDefault && touchSensor5 == inputDefault && touchSensor6 == inputDefault) {

    digitalWrite(D8, outputTriggered);
    delay(triggerDuration);
    Serial.print("touchSensor7 is ");
    Serial.print(inputTriggered);

Serial.print(inputTriggered);
    Serial.print(" and D8 is ");
    Serial.println(outputTriggered);
  }

}

Welcome to the forum

Why do you have a pulldown resistor on the outputs ?

Exactly which touch sensors are you using ?
Do they output a digital signal or an analogue one ?

Please post a schematic of your project showing how each of the components of it are connected and powered. A 'photo of a hand drawn circuit diagram is good enough

To post images etc. you need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

Users at trust level 1 can...

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

Ok, I see another topic with that question about identifiers. So the first advice is: DON'T DO IT! And always specify your board and diagram if posting separate threads.

First, use meaningful identifiers. If a constant represents a pin number use kinda prefix: you can choose any you like, e.g. "P_" if generic, "B_" is a button "L_" for LEDs, etc. And for analog pins you already have the identifiers A0, A1... just use them instead of redefine.
Second, if you have more than a couple of identical items, use arrays.
Your code will be more compact and readable end easier to manage/debug. Example, instead of:

const int D2 = 2;
const int D3 = 3;
const int D4 = 4;
const int D5 = 5;
const int D6 = 6;
const int D7 = 7;
const int D8 = 8;

const int A0 = 14;
const int A1 = 15;
const int A2 = 16;
const int A3 = 17;
const int A4 = 18;
const int A5 = 19;
const int A6 = 20;

int touchSensor1 = 0;
int touchSensor2 = 0;
int touchSensor3 = 0;
int touchSensor4 = 0;
int touchSensor5 = 0;
int touchSensor6 = 0;
int touchSensor7 = 0;

check this out:

byte P_Output[] = {2,3,4,5,6,7,8};
byte P_Sensor[] = {A0,A1,A2,A3,A4,A5,A6};
int touchSensor[] = {0,0,0,0,0,0,0,0};

See? You now have three arrays made up of 8 elements. So you can address any single item using its reference number from 0 to 7 e.g. like "analogRead(P_Sensor[1])".
And your code can be packed using "for()" statements to cycle each one, e.g.:

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

  for(int i=0; i<=7; ++i)
    pinMode(P_Output[i], OUTPUT);
}

Note that you don't need to define the analog pins as INPUT: they're already set. If you use pinMode for INPUT, you convert them to digital.

Now it's your turn to try modifying the code and see if it works (the things I've shown you so far are not complete, so I'll continue to help you if you ask me...).

Note how the inputs are read in the original sketch

  touchSensor1 = digitalRead(A0);
  touchSensor2 = digitalRead(A1);
  touchSensor3 = digitalRead(A2);
  touchSensor4 = digitalRead(A3);
  touchSensor5 = digitalRead(A4);
  touchSensor6 = digitalRead(A5);
  touchSensor7 = digitalRead(A6);

Yeth. And things like this

  if (touchSensor1 == inputTriggered && touchSensor2 == inputDefault && touchSensor3 == inputDefault && touchSensor4 == inputDefault &&
      touchSensor5 == inputDefault && touchSensor6 == inputDefault && touchSensor7 == inputDefault) {

will end up looking much nicer, as well as being easier to modify or fix.

a7

Yes, knowing nothing aout the exact sensors and wirings, I can't tell if the user wants analog or digital readings. If the sensors are connected using their digital outputs, any digital pin can be used. I'm just waiting for more hardware information to be more specific.

nor can I

I await the reply from @jaime0624 with interest

Hi, @jaime0624
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Are you switching inputs to gnd or 5V?

Tom.... :smiley: :+1: :coffee: :australia:

Hello,

I am using a TTP223 Capacitive sensor.

I want to have a digital signal.

Have you soldered the header pins onto the TTP223 boards?

Are all the GNDs connected to the nano GND?

You will get random readings if not soldered.

Then you're in luck, because the TTP223 output is digital. Depending on which pin you connect to, the output may be open-drain, but that doesn't imply analog, just that the output requires a pullup to operate with your logic/Arduino.

You can download the datasheet here:

AllDatasheet datasheet version for TTP223 sensor

Okay, so it has a digital output, so you can use any I/O pin (the analog ones aren't necessary).
But a direct link to the board and/or a detailed image of both sides might be helpful, because you need to check your board to see if the digital output is connected to the OPDO pin (Open Drain Output, which requires a pull-up or pull-down resistor) or the Q pin (CMOS output).

Or, just enable pullup on the Arduino - no major harm in a second pullup, if the CMOS output is wired.

Good day guys, thanks for your inputs.

I tried multiple troubleshooting steps, and grounding the whole metal enclosure seems to eliminate the issue. After doing this, the erratic behavior disappeared. It’s still under observation, but I noticed that the problem does not occur when I test the setup on a plastic table.

However, the final enclosure is made of aluminum, so I’m wondering if it could be causing some sort of noise amplification, transient interference, grounding issue, or capacitance effect. I’m not entirely sure.

What are your thoughts on this?

See post #10

hi, can you help me understand how this works?

I'm very interested of finding out why.

Hi, @jaime0624
Can you post some images of your project?
So we can see your component layout.

Tom.... :smiley: :+1: :coffee: :australia:


Here is the temporary grounding that solves the issue.

I've asked twice if the header pins are soldered but you have not answered my question.