Seeduino Xiao, Problems with receiving Analog Readings / Internal Pullup

Hello,

I've constructed two custom PCBs for two distinct projects. Both are designed for applications in a 12V car/truck environment. I'm experiencing similar issues with both, even though they have different designs to power a 12V motor.

Issue with PCB #1:

This Application uses a Mosfet and some Diodes and Resistors to engage the Motor.

I'm using D10 (MOSI) to detect a GND signal. I've enabled the internal pull-up resistor for this. When the device is exposed (no casing - and it's a solid aluminum casing), it functions correctly. However, when I place the casing on, anomalies occur.

When I activate the GND signal, there are times it detects and triggers repeatedly. At other times, it doesn't respond at all. I've added a buzzer to the PCB for feedback.

Code:

#include <Arduino.h>

const int triggerPin = A10;    // Pin connected to the trigger button
const int sensorPin1 = A5;     // Pin connected to one side of the sensor
const int sensorPin2 = A6;     // Pin connected to the other side of the sensor
const int motorPin = A7;       // Pin connected to the Mosfet for motor control
const int buzzerPin = A0;      // Pin connected to the positive side of the Buzzer
const int buzzerGndPin = A3;   // Pin connected to the negative side of the Buzzer

unsigned long motorStartTime = 0;
bool motorRunning = false;
bool triggerUsed = false;
unsigned long buzzerBeepTime = 0;

// Für die Entprellung
unsigned long lastTriggerTime = 0;
const int debounceTime = 50;  // Zeit in Millisekunden für die Entprellung

void setup() {
  pinMode(triggerPin, INPUT_PULLUP);
  pinMode(sensorPin1, INPUT_PULLUP);
  pinMode(sensorPin2, INPUT_PULLUP);
  pinMode(motorPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  pinMode(buzzerGndPin, OUTPUT);
  
  digitalWrite(buzzerGndPin, LOW);  // Set GND pin of Buzzer to LOW
  digitalWrite(motorPin, LOW);
}

void loop() {
  int triggerState = digitalRead(triggerPin);
  int sensorState1 = digitalRead(sensorPin1);
  int sensorState2 = digitalRead(sensorPin2);

  if (!triggerUsed && millis() - buzzerBeepTime >= 4000) {
    // Beep buzzer every 4 seconds if trigger has not been used yet
    beepBuzzer(500);
    buzzerBeepTime = millis();
  }

  if (triggerState == LOW && (millis() - lastTriggerTime) > debounceTime) {
    lastTriggerTime = millis();
    
    if (sensorState1 == HIGH && sensorState2 == HIGH && !motorRunning) {
      triggerUsed = true;
      motorRunning = true;
      motorStartTime = millis();
      digitalWrite(motorPin, HIGH); // Turn on the motor
      beepBuzzer(500); // Beep the buzzer when motor starts
    }
  }

  if (motorRunning && millis() - motorStartTime >= 5000) {
    digitalWrite(motorPin, LOW);  // Turn off the motor after 5 seconds
    motorRunning = false;
  }
}

void beepBuzzer(unsigned long duration) {
  digitalWrite(buzzerPin, HIGH);
  unsigned long beepStartTime = millis();
  while (millis() - beepStartTime < duration) {
    // Diese Schleife sorgt für die Dauer des Piepsens, aber ohne `delay`
  }
  digitalWrite(buzzerPin, LOW);
}

Issue with PCB #2:

This design employs an L293D to drive the motor in both directions.

For this PCB, I use a trigger signal to turn the motor clockwise. Additionally, I've incorporated a reed sensor to detect a magnet (positioned in the holder) which, when close, turns the motor counterclockwise.

However, I'm encountering a familiar issue.

Upon powering the unit, I receive sporadic trigger signals. For instance, when the motor is supposed to run continuously until a specific condition (like "Sensorbolzeneingefahren") is met, it unexpectedly halts and remains inactive until I re-trigger it. This happens even when the logical action would be to await the sensor trigger. Furthermore, the reed sensor is highly sensitive; merely approaching the cables with my fingers results in a trigger.

The reed sensor connects two pins on the Seeduino. Is there a method to detect this connection without applying current to them (akin to a pull-up)?

The trigger mechanism remains identical to that in PCB #1: a GND source connected to a pull-up pin.

Code:

const int reedInput = A7;
const int reedGround = D9;
const int buzzerPinPlus = A5;
const int buzzerPinMinus = A1;
const int triggerPin = A2;
const int input2L293D = A4;
const int input1L293D = A6;
const int sensorBolzenEingefahren = A3;
const int sensorBolzenAusgefahren = A10;

void setup() {
  pinMode(reedInput, INPUT);
  pinMode(reedGround, OUTPUT);
  digitalWrite(reedGround, HIGH);  // Setzt D9 auf HIGH

  pinMode(buzzerPinPlus, OUTPUT);
  pinMode(buzzerPinMinus, OUTPUT);
  pinMode(triggerPin, INPUT_PULLUP);
  pinMode(input2L293D, OUTPUT);
  pinMode(input1L293D, OUTPUT);
  pinMode(sensorBolzenEingefahren, INPUT_PULLUP);
  pinMode(sensorBolzenAusgefahren, INPUT_PULLUP);

  digitalWrite(buzzerPinMinus, LOW); // Ground for the buzzer
}

void loop() {
  // Bolzen ist eingefahren, warte auf Reed-Sensor, um auszufahren
  if (digitalRead(sensorBolzenEingefahren) == LOW) {
    if (digitalRead(reedInput) == HIGH) {
      buzzreed();
      while (digitalRead(sensorBolzenAusgefahren) == LOW) {
        extendBolzen();
      }
      stopMotor();
    }
  }
  // Bolzen ist nicht eingefahren (ganz oder teilweise ausgefahren), warte auf manuellen Trigger, um einzufahren
  else if (digitalRead(sensorBolzenEingefahren) == HIGH) {
    if (digitalRead(triggerPin) == LOW) {
      buzz();
      while (digitalRead(sensorBolzenEingefahren) == HIGH) {
        retractBolzen();
      }
      stopMotor();
    }
  }
}

void buzz() {
  digitalWrite(buzzerPinPlus, HIGH);
  delay(500); // Buzzer is on for 500 milliseconds
  digitalWrite(buzzerPinPlus, LOW);
}

void buzzreed() {
  digitalWrite(buzzerPinPlus, HIGH);
  delay(200); // Buzzer is on for 200 milliseconds
  digitalWrite(buzzerPinPlus, LOW);
}

void extendBolzen() {
  digitalWrite(input1L293D, HIGH);
  digitalWrite(input2L293D, LOW);
}

void retractBolzen() {
  digitalWrite(input1L293D, LOW);
  digitalWrite(input2L293D, HIGH);
}

void stopMotor() {
  digitalWrite(input1L293D, LOW);
  digitalWrite(input2L293D, LOW);
}

After extensive testing, I suspect the inconsistencies may stem from issues with the internal pull-up. Since all of them are used to detect trigger signals or sensor inputs. Are there alternative methods to utilize it? Or do i have a "stupid little mistake" going on?

I do not see any attempt to serial.Print() anything in your program, so how did you develop the program to this point without ever displaying any values?

What are "A10" and "A7"? Where is "D10" in the code?

Which MCU do you have? There are about half a dozen different Xiao boards.

The 7805 voltage regulator is missing the required input capacitor, and will need some additional protection against high voltage spikes in the extremely harsh automotive environment. See Transient Voltage Suppression in Automotive Applications

1 Like

Due to the Housings im Not able to connect a usb while the device is connect to the 12v source. That's why there's no Serial Connection

Checking of triggering is made with a Multimeter and clamps.

I must be missing something, here. Are you saying this all worked properly and now it is in your housing and it doesn't work?

A10 (D10) = Connected to a Switched GND Source, its the Trigger to initiate the Code.
A7 = Conected to the Mosfet to act as a Relais when it Pulls Up the Pin.

Im Using this Board:

Seeeduino XIAO, Arduino Microcontroller, SAMD21 Cortex M0+

  • Artikel-Nr.: SE-102010328
    |* EAN: 0886269001039

Due to Limitations in Space i cant go bigger than this.

Also there is a 10uF Capacitor in front of the 7805CV, it actually works pretty well, didn´t have any defects till now, other than the Strange behaviour when it is in the Metall Casing.

You have been lucky, but it is just a matter of time.

Yes, exactly, the weird thing is, we Builded this Board multiple times, but only some have the strange Behaviour, there are some Units out there working without any Problems since May 23. We didn´t change anything other than the 100Ohm and 10K Ohm Resistors, but we switched Back to the Original ones after we saw that something didn´t act right, weird thing is, they still dont work right. That´s why i came here, this is actually my First Arduino Project and i just cant figure out the Problem, it´s really weird.

Can you tell me how you would improve the current setup? I would be thankfull for any help.

I would follow the advice in the link I posted, and add TVS diodes, extra filtering, etc.

This is characteristic of voltage spikes in the power supply, resulting from inadequate power supply decoupling:

This is characteristic of a floating input, due to bad connection or lack of pullup resistor:

Furthermore, the reed sensor is highly sensitive; merely approaching the cables with my fingers results in a trigger.

1 Like

Thank you, that sounds plausible.

I will look into the TVS diodes and filtering.

Can you give me a more specific hint what you mean by pullup resistor? for testing i´ve put a 10k resistor between the reedsensor and the Reed Input Pin. But it didn´t do much to reduce the sensitivity.

As i said, im greatfull for all your help, im noway a Pro in this segment, neither am i an electrician :smiley:

The 10K "pullup" resistor should be connected between the input pin and Vcc to define the pin state as HIGH when there is no input. Or use the internal pullup resistors with pinMode(pin_number, INPUT_PULLUP);.

The reed switch should be connected between the input pin and GND. digitalRead() will return LOW when the switch is closed.

1 Like

You mean i should go in the Code from:

pinMode(reedInput, INPUT);

to :

pinMode(reedInput, INPUT_PULLUP);

Is that right?

I almost always mean what I write, so yes.

Sorry, if i ask "stupid questions" sometimes to verify english isn´t my first Language.

For testing, im using a Car Battery without anything conected to it but my PCB´s, do you think there would be any Voltage Spikes in this Setup that can make this kind of reaction?
Not that i think your Idea isn´t valid, just trying to find the exact culprit.

Yes, from the motor shown on your schematic. Also, voltage drops due to inadequate wiring can cause brownout resets.

I can recall the time I had to turn off the lights above my work bench to eliminate the noise signals affecting the project.

Yeah, that´s how i feel with this setup, at this moment. The problem is, it needs to work without getting affected by such strange "forces".
Thats why i´m calling for help :sweat_smile:

The Current Diode im using with the LM7805 is this one: bzx-85c5v1

and the following Capacitor

Do i switch out the BZX 85C5V1 VIS Zener-Diode for the TVS Diode? or do i put it between the 12V Incoming to the Board like this?