Arduino Programm only working when USB-port connected to Computer

Hi Arduino-Community!
This is my first time posting so please dont mind little noob-mistakes.

I built a pet-feeder controlled by an Arduino Uno, working with 2 stepper motors, 1 rfid module, a clock, a switch, and a seperate psu-module.
The goal of this project is to provide meals for a specific cat two times a day.

The whole thing in itself seems to work as intended, while the automatic feeder is still connected to my pc via the onboard usb-port of the arduino. But as soon as i unplug the usb-connection, the system stops working.
My first thought was, that the stepper motors are drawing to much current so that the 12V/2A plug of the psu could not supply enough and needed the additional power from the usb connection, but switching to a 12V/3A power supply didn't make any difference.

So the question is: why does my project do not work, when not connected to the computer via usb?
Are there any reasons the serial communication between the rfid module and the arduino breaks when not connected to a computer?

Below you can find a list of my used parts, a drawn wiring diagram and the whole code of this project. Any help is appreciated!

Parts:

  • Arduino Uno
  • 12V PSU with 3x12V Out, 3x5V Out, 3x3,3V Out
  • 2x 28byj-48-stepper (12V) + ULN2003 driver
  • 1x RTC ds3231
  • 1x WL-134 RFID Module + self built antenna

Wiring:

Code:

//includes for Scanner
#include <SoftwareSerial.h>
#include <Rfid134.h>
bool katzeda;
String katze = "276098001016530";
String gescannt;

class RfidNotify
{
public:
static void OnError(Rfid134_Error errorCode)
  {
    // see Rfid134_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }
static void OnPacketRead(const Rfid134Reading& reading)
  {
    char temp1[8];
    char temp2[8];
    char temp3[8];
    sprintf(temp1, "%03u", reading.country);
    sprintf(temp2, "%06lu", static_cast<uint32_t>(reading.id / 1000000));
    sprintf(temp3, "%06lu", static_cast<uint32_t>(reading.id % 1000000));
    String temp1s((char*)temp1);
    String temp2s((char*)temp2);
    String temp3s((char*)temp3);
    gescannt = temp1s+temp2s+temp3s;//string zusammenbauen
  }

};
SoftwareSerial secondarySerial(0, 11); // RX, TX
Rfid134<SoftwareSerial, RfidNotify> rfid(secondarySerial);


//includes for Motors
#include <AccelStepper.h>
AccelStepper Motor2(8, 2,4,3,5); // Motor2 = Schale
long positionM2 = 0;
int rotations;

AccelStepper Motor1(8, A0,A2,A1,A3); // Motor1 = Klappe
long positionM1 = 0;
const byte switchPin = 13;  //Switch in der Deckelklappe   

//includes for Timer
#include <RTClib.h>
#include <Wire.h>
DS3231 rtc;
char t[32];

bool canRotate;
const long intervall = 60000;

unsigned long timeStamp1;
unsigned long timeStamp2;
bool klappeOffen;

//includes for manual open
int Button = 12;
int Button_State=0;



void setup() {
  //Setup for Scanner
	Serial.begin(9600);		// Initialize serial communications with the PC
  secondarySerial.begin(9600);
  rfid.begin();
  katzeda = false;
  klappeOffen = false;
  gescannt = "";

  //Setup for Motor2 Schale
  Motor2.setCurrentPosition(positionM2);
  Motor2.setMaxSpeed(500.0);
	Motor2.setAcceleration(200.0);
	Motor2.setSpeed(500);
  Motor2.disableOutputs();

  //Setup for Motor1 Klappe
  Motor1.setCurrentPosition(positionM1);
  Motor1.setMaxSpeed(500.0);
	Motor1.setAcceleration(200.0);
	Motor1.setSpeed(-500);
  Motor1.disableOutputs();
  pinMode (switchPin, INPUT_PULLUP);

  //setup for Timer
  Wire.begin();
  rtc.begin();
  //rtc.adjust(DateTime(2019, 1, 21, 5, 0, 0));
  //rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
  canRotate = true;
  rotations = 0;

  //setup for resetwheel Button
  pinMode(Button, INPUT);
}

void loop() {
  //Überprüfe Ob Position Reset          -----------------------------------------------------------------------
  Button_State = digitalRead(Button);
  if (Button_State != 0){
    wheelReset();
  }
  //Überprüfe Bedingung IST UHRZEIT -----------------------------------------------------------------------------
  DateTime now = rtc.now();
  if(!canRotate){
    if((now.hour()==5)||(now.hour()==17)){
      canRotate = true;
    }
  }
  if(canRotate){
    if (((now.hour()==6)||(now.hour()==18))&&(now.minute()==30)&&((now.second() >= 0 ) && (now.second() < 30))){
      switch (rotations){
        Motor2.enableOutputs();
        case 0:
          Motor2.moveTo(819);
          Motor2.runToPosition();
          rotations = 1;
          break;
        case 1:
          Motor2.moveTo(1638);
          Motor2.runToPosition();
          rotations = 2;
          break;
        case 2:
          Motor2.moveTo(2457);
          Motor2.runToPosition();
          rotations = 3;
          break;
        case 3:
          Motor2.moveTo(3476);
          Motor2.runToPosition();
          rotations = 4;
          break;
        case 4:
          Motor2.moveTo(0);
          Motor2.runToPosition();
          rotations = 0;
          break;
        Motor2.disableOutputs();
      }
      canRotate = false;
    }
  }
  //Überprüfe Bedingung IST KATZE DA ----------------------------------------------------------------------------
  rfid.loop();
  //Serial.println("Spot1");
  //Serial.println(gescannt);
  //Serial.println(gescannt == katze);
  //delay(1000);
  if(gescannt == katze){
    gescannt = "";
    timeStamp1 = millis();
    if(!klappeOffen){
      klappeOffen = true;
      Motor1.enableOutputs();
      Motor2.enableOutputs();
      Motor1.setSpeed(500);
      Motor1.moveTo(1600);
      Motor1.runToPosition();
      Motor1.disableOutputs();
    }    
  }
  else{
    if(klappeOffen){
      timeStamp2 = millis();
      if(timeStamp2-timeStamp1>=10000){
        klappeOffen = false;
        Motor1.enableOutputs();
        Motor1.setSpeed(-300);
        while(digitalRead (switchPin) == HIGH){
          Motor1.runSpeed();
        }
        Motor1.setCurrentPosition(0);
        Motor1.disableOutputs();
        Motor2.disableOutputs();
      }
    }
  }
}

// Resettet Position Futterrad ----------------------------------------------------------------
void wheelReset(){
  Motor2.enableOutputs();
  Motor2.moveTo(0);
  Motor2.runToPosition();
  Motor2.disableOutputs();
  rotations = 0;
}

It sounds like a hardware problem, related to power.

Please post a schematic diagram. Your wiring diagram is too blurry and is missing too much information.

Please give a link to all major components so we can see their specifications. Particularly that "power supply module".

What is the rectangle in the wire from the 12V output of the power supply to the RFID module? I hope it is not a resistor.

@langer1308
Are you sure that the PSU is outputting 5V?
Did you try pushing he reset button after you disconnect the USB?
Is the green power LED lit?

@PaulRB

Will do that later. Right know i dont have the means to put it together.

It indeed is a resistor. Is that a problem? My last electronics class was almost a decade ago.

@jim-p
I didnt measure the output myself, i just thought that if everything is working while the arduino is connected to the computer, the voltage would be alright.

I relocated the whole thing to the kitchen to let my cat practice (that was were i first noticed the usb connection seems to be needed), so i unplugged the power completely and repowered it again. The power indicator is lit.

Well it's geting some voltage but it may not be 5V. You need to check if it's 5V.
Do you have a 5V phone charger that you can connect to the USB connector to power the UNO?

I tested this and sadly it did not make it work.

Tomorrow i should have the time to create a better schematic diagram and maybe it makes a constructional flaw visible, but with the little knowledge i have about circuits i tend to suspect a logical mistake in the code.
Maybe something about those "serial.begin" things (i do not know why i even needed 2 of those (i will look for links to the modules for the correct libraries, but it is mainly the first thing that pops up in google).
I just threw together the example snippets from the module's libraries and tried to delete everything i didn't need. The code did not work without the "Serial.begin" and the "secondarySerial.begin" so i kept them.

What is it's purpose?

It drops the Voltage from 12V to 8-9V, because the WL-134 RFID Module needs this.

I just want to make clear: The whole circuit works, when the arduino is connected to the computer, but does not work without the usb connection - even when the usb is plugged into the wall with a phone charger plug providing the same power as when the arduino is connected to the pc.

Drawing a schematic diagram takes more time than expected, i have to reschedule it to after the christmas days.

Here are links to the used parts, tho:

That looks suspicious. You are using a connection of the HW serial for SoftwareSerial.

A resistor is not a voltage regulator. If you try to use a resistor, the voltage drop will vary with the current drawn by the RFID reader, which won't be constant, it will be changing over time and whether there are any, and how many, tags are in range, for example. I would be surprised if it worked ok with an unstable voltage supply like that. You need to use a 9V regulator like 7809.

Your drawing shows "A2, A3, A4, A5" for one motor driver... and you did not number the pins for the second motor driver.

  1. Disconnect all connections.
  2. Draw a schematic as you connect the wires and label all connections.

Charming. I built an auto feeder for one of our cats. I used a food auger driven by a small motor, the serving size was proportional to the time it ran.

The cat never did find the potentiometer I used to adjust the running time / serving size.

But... he did figure out something. He would alert and run to the kitchen in time to get to the device just before it did anything I could see or hear or sense. As far as I know there was no sound from anything that would have been emanating from it.

The auto feeder was very useful. It took him some time to realize he just wasn't going to get fed by humans, then he became somewhat easier to have around.

Cats.

a7

Sorry for the late answer, last months were really busy.

@ PaulRB thank you for the info about the regulator. I will try to integrate one in the future. Sadly i guess this won't fix the problem, as the system works with the resistance while being connected to the pc.

@MicroBahner Could you elaborate about that HW / SW Serial thing? As the code shows i have two software serial instances defined and as i understand the logic of the code i dont need the first one?
The SoftwareSerial for the RFID scanner comes from the example code of the library, so why does this thing look like a HardwareSerial?

@ xfpd The second motor is defined as "AccelStepper Motor2(8, 2,4,3,5);" and connected to the Pins 2 to 5.

@alto777 It is fascinating how well you can train your cat on getting food. Right now we have a workaround to achieve what the selfmade feeder should do. I built each cat a box with a chip-controlled entrance flap and a time-controlled feeder inside.
Our cats know when it's close to feeding time and wait infront of the box. And if we have not prepared the food portions atleast 20 minutes ahead of time they get really annoying, afraid we would forget them.
Lovely little pests.

1 Like

So, this thing is still not working, but to give anyone the chance to solve this crime, i hereby keep it open for another 6 months.
Any help on my quest to automate feeding 2 cats 2 times a day while seperating spiked food from non spiked is appreciated.

Content:

  • Status Quo
  • Wiring Info
  • Code
  • Parts

Status quo:
I dumbed down my circuit and now have only the Arduino itself, a PSU module, the scanner module and a stepper motor (for control, i dont have LEDs laying around) connected.
THe system now should scan an RFID Chip and shortly run the motor to show it scanned the right number.
The whole system works while the USB Port of the Arduino is connected to my Computer.
As soon as it has to run without Computer connection it just does nothing.

Wiring:
The Arduino gets its Power from the Vin Pin and is Ground through GND right next to it.
Pin 0 is connected to the Scanner for reading the signals.
Pins 2-5 are connected to the Stepper driver unit.
Both Scanner and Motor get their Power from the PSU module.

Code:

#include <SoftwareSerial.h>
#include <Rfid134.h>
String gescannt;
class RfidNotify
{
public:
static void OnError(Rfid134_Error errorCode)
  {

  }
static void OnPacketRead(const Rfid134Reading& reading)
  {
    char temp1[8];
    char temp2[8];
    char temp3[8];
    sprintf(temp1, "%03u", reading.country);
    sprintf(temp2, "%06lu", static_cast<uint32_t>(reading.id / 1000000));
    sprintf(temp3, "%06lu", static_cast<uint32_t>(reading.id % 1000000));
    String temp1s((char*)temp1);
    String temp2s((char*)temp2);
    String temp3s((char*)temp3);
    gescannt = temp1s+temp2s+temp3s;//string zusammenbauen
  }
};

//includes for RFID
String katze = "276099200779425";

SoftwareSerial secondarySerial(0, 11); // RX, TX
Rfid134<SoftwareSerial, RfidNotify> rfid(secondarySerial);

//includes for Motors
#include <AccelStepper.h>
AccelStepper Motor2(8, 2,4,3,5); // Motor2 = Schale
int positionM2 = 0;



void setup() {
  secondarySerial.begin(9600);
  rfid.begin();
  gescannt = "";

  Motor2.setCurrentPosition(positionM2);
  Motor2.setMaxSpeed(500.0);
	Motor2.setAcceleration(200.0);
	Motor2.setSpeed(500);
  Motor2.disableOutputs();


}

void loop() {
  rfid.loop();
  if(gescannt == katze){
    gescannt = "";
    Motor2.enableOutputs();
    Motor2.moveTo(positionM2+400);
    Motor2.runToPosition();
    Motor2.disableOutputs();
    Motor2.setCurrentPosition(positionM2);      
  }
}

Arduino
PSU Module: AMS1117 Multi DC-DC 12V-3,3V/5V/12V Step-Down Spannungsregeler Power , 1,50 €
RFID WL-134: 134.2K WL-134 AGV RFID Module Antenne FDX-B ISO11784/85 Protocol Card Tag Reader | eBay
Stepper + driver: https://www.androegg.de/shop/uln2003-schrittmotor-28byj-48-motor-stepper-12v-driver-board-set/

Your designation in post 13 indicates Digital Pins 2, 3, 4, 5, not Analog Input Pins A2, A3, A4, A5.

Hey, first of all thank you for still wasting thoughts on this topic.

Both Pins 2-5 and A2-A5 should be connected to one motor driver each in the final project. As i tried to narrow down the potential fix to my problem, i disconnected all and everything but 1 motor driver (now connected to Pin 2-5) and the RFID Scanner (connected to Pin 0).

So for now forget everything that was before. Now we have this should-be-simple circuit together with the code posted in Post#14.
Even this simpler version works fantastically well while the arduino is connected to my PC via USB but stops doing anything when not connected.

There also is a blinking indicator on the scanner that shows when it is scanning while connected to PC, but does not blink anytime i trie to scan something without the PC connection..

This indicates external power to the Arduino is insufficient.

The whole story looks so weird... If you power your arduino with old style reliable 5V2A "samsung" usb charger, it doesn't work?
I mean not with some "6 in 1 fast smart charger".
If not, your PC has to have some paranormal power....

@xfpd
O M G !
After your comment about insufficient Power i thought maybe the Vin Pin doesnt allow enough mA troughput (wild assumption, i know), but i read that it has to get 7-12V to put this through the internal regulator. So i connected the Vin to a 12V output from the PSU instead of 5V like before. Sadly it changed nothing. And then i thought "Would be a shame if that Vin was defective". It wasn't.
BUT:
The second Arduino had an other 5V Charger on its USB Cable. An other one than the one i tried to power my first Arduino with.

TURNED OUT: That f***ing 5V-Charger was dead....
This whole case could have been solved with @jim-p 's Post#5...
I'm crying right now, but im just grateful for you making it click to me <3

1 Like

Should have used that "samsung" charger instead of the apple product i used...