Why does my Arduino sometimes glitch up?

This is just something that I'm wondering about. I was doing a project very recently for a robotics team where I had this CNC shield kit. I also had this QR/Barcode scanner that would read barcodes on packages, a I2C 16 by 2 lcd screen with three buttons to select a number on the lcd screen, a servo motor, and an electromagnet which served as a gripper. How it would work is you scan a package via barcode and barcode scanner. The scanner then got the number that the barcode represented and it would insert it into the cell that went along with that barcode. Then you go to the LCD screen and select which number you want. The machine would then go and retrieve the package that was in that numbered cell and barcode value. Afterwards it would drop it off representing the package being delivered. Here is a video where it scans a package with the barcode value of one and then retrieves 1 with a value of 7. (the video was too big so I broke it into 2 halves)
IMG_0873_Trim (2).zip (7.6 MB)
IMG_0873_Trim(3).zip (7.2 MB)
I controlled the electromagnet with a relay and 9 volt battery.
Here is the code(I made it so it just demonstrated specifically storing package 1 and retrieving package 7 so that's why the step values are fixed)

#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
//#include<Servo.h>
SoftwareSerial mySerial(13,12);
 // A1 is up button, A2 Select, and A3 Down
LiquidCrystal_I2C lcd(0x27,16,2);  // run ic2_scanner sketch and get the IC2 address, which is 0x27 in my case
int count = 1;
int pre = count;
int stepX = 2;
int stepY = 3;
int stepZ = 4;
int DirX = 5;
int DirY = 6;
int DirZ = 7;
const int t = 1000;
const int Y = 2000;
const int Z = 2000;
Servo A;
void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(11,OUTPUT);//pin 11 controlled the relay and turned the Electromagnet on and off
pinMode(stepX,OUTPUT);
pinMode(stepY,OUTPUT);
pinMode(stepZ,OUTPUT);
pinMode(DirX,OUTPUT);
pinMode(DirY,OUTPUT);
pinMode(DirZ,OUTPUT);
  lcd.init();
    lcd.backlight();
lcd.setCursor(0, 0);
  A.attach(10);
A.write(0);
}

void loop() {
  if(mySerial.available()){
    char C = mySerial.read();
    Serial.print(C);
    delay(5);
digitalWrite(DirY,LOW);
for(int x =0; x<1200; x++){
  digitalWrite(stepY,HIGH);
  delayMicroseconds(Y);
  digitalWrite(stepY,LOW);
  delayMicroseconds(Y);
}
digitalWrite(DirX,HIGH);
for(int x = 0; x<1150; x++){
  digitalWrite(stepX,HIGH);
  delayMicroseconds(t);
  digitalWrite(stepX,LOW);
  delayMicroseconds(t);
}
A.write(90);
digitalWrite(DirY,HIGH);
for(int x =0; x<400; x++){
  digitalWrite(stepY,HIGH);
  delayMicroseconds(Y);
  digitalWrite(stepY,LOW);
  delayMicroseconds(Y);
}
digitalWrite(11,HIGH);
    delay(3000);
    digitalWrite(DirY,LOW);
    for(int x = 0; x<1200; x++){
      digitalWrite(stepY,HIGH);
      delayMicroseconds(Y);
      digitalWrite(stepY,LOW);
      delayMicroseconds(Y);
    }
    A.write(0);
    digitalWrite(DirX,LOW);
for(int x = 0; x<1150; x++){
  digitalWrite(stepX,HIGH);
  delayMicroseconds(t);
  digitalWrite(stepX,LOW);
  delayMicroseconds(t);
}
digitalWrite(DirZ,LOW);
    for(int x = 0; x <4000; x++){
      digitalWrite(stepZ,HIGH);
      delayMicroseconds(Z);
      digitalWrite(stepZ,LOW);
      delayMicroseconds(Z);  
    }
    digitalWrite(11,LOW);
    delay(2000);
    digitalWrite(DirX,HIGH);
for(int x = 0; x<250; x++){
  digitalWrite(stepX,HIGH);
  delayMicroseconds(t);
  digitalWrite(stepX,LOW);
  delayMicroseconds(t);
}
digitalWrite(DirX,LOW);
for(int x = 0; x<250; x++){
  digitalWrite(stepX,HIGH);
  delayMicroseconds(t);
  digitalWrite(stepX,LOW);
  delayMicroseconds(t);
}
    digitalWrite(DirZ,HIGH);
    for(int x = 0; x <4000; x++){
      digitalWrite(stepZ,HIGH);
      delayMicroseconds(Z);
      digitalWrite(stepZ,LOW);
      delayMicroseconds(Z);  
    }
  }
 if(digitalRead(A2) == LOW){
    if(count == 12){
      ;
    }
    else{
      pre = count;
    count++;
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print(pre);
    lcd.setCursor(0,1);
    lcd.print("> ");
    lcd.print(count);
    delay(500);
    }
  }
  if(digitalRead(A3) == LOW){
    if(count == 1){
      ;
    }
    else{
      pre = count;
    count--;
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print(pre);
    lcd.setCursor(0,0);
    lcd.print("> ");
    lcd.print(count);
    delay(500);
    }
  }
  if(digitalRead(A1) == LOW){
    digitalWrite(DirY,LOW);
for(int x =0; x<4300; x++){
  digitalWrite(stepY,HIGH);
  delayMicroseconds(Y);
  digitalWrite(stepY,LOW);
  delayMicroseconds(Y);
}
digitalWrite(DirZ,LOW);
    for(int x = 0; x <4000; x++){
      digitalWrite(stepZ,HIGH);
      delayMicroseconds(Z);
      digitalWrite(stepZ,LOW);
      delayMicroseconds(Z);  
    }
    digitalWrite(11,HIGH);
    digitalWrite(DirZ,HIGH);
    for(int x = 0; x <4000; x++){
      digitalWrite(stepZ,HIGH);
      delayMicroseconds(Z);
      digitalWrite(stepZ,LOW);
      delayMicroseconds(Z);  
    }
    digitalWrite(DirX,HIGH);
for(int x = 0; x<1150; x++){
  digitalWrite(stepX,HIGH);
  delayMicroseconds(t);
  digitalWrite(stepX,LOW);
  delayMicroseconds(t);
}
digitalWrite(DirY,LOW);
for(int x =0; x<1500; x++){
  digitalWrite(stepY,HIGH);
  delayMicroseconds(Y);
  digitalWrite(stepY,LOW);
  delayMicroseconds(Y);
}
digitalWrite(11,LOW);
    delay(500);
    while(1);
  }
  
}

So in the video it worked fine. However about a couple of days after that when I powered it up sometimes the arduino would skip some step. Like it wouldn't go right sometimes on the X-axis and then when it went left I didn't have any limit switches and it would ram itself against the end. Or it would stop short during the mySerial.available() if statement and wouldn't do anything when I made A1==LOW after. If it's truly because the arduino is imperfect then would a Raspberry Pi be better or some other microcontroller? Sorry for the long post :slight_smile:

It's hard to say... It could be a software error/bug or maybe some noise or a "glitch" on the power supply. Motors can cause glitches on the power supply.

If you do something simple, like for example counting minutes and sending the count to the serial monitor... Something where you are unlikely to have software bug... It should "run forever" as long as it's getting power. (Except some variables may eventually roll-over.) The minutes won't be perfect because the Arduino's oscillator isn't perfect.

...I built a car alarm once with a different processor and it ran 24/7, even when not "armed" and ran perfectly for about 20 years before "something died". The only time it was stopped or reset was when I had to change the car battery every-few years. I did get a couple of false-alarms but that was human error.

Do you know of a better suited processor(I'm thinking something more lower level and analog so the chances of a "software bug" are low) for these type of projects involving stepper motors but can still interface with sensors. Or is it better to go the opposite direction and get something more advanced like a Raspberry Pi so it's still relatively easy to control things and do so accurately? Because while I love the Arduino I also want to expand my range in microprocessors.

Also in this code when I tried going up or down the LCD would display random characters instead of numbers and when I isolated the LCD it worked fine. And the Barcode would scan on some runs but not on others just for some more examples.

I will take a SWAG and say the power supply is sick. Your 9V battery would not be up to the task. Post a schematic showing how you actually wired it including all power and ground connections. Also post links to technical information on the hardware parts, azon links are just sales info.

Do you have a DMM?
Check your power supply during the glitches.
Do you have bypass capacitors located in your project?

Can you please post image(s) of your project so we can see your component layout?

Can you please post a schematic of your project?

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

Alright, here's a crude Wiring Schematic I made on Tinkercad. And just in case you need clarification I used a much smaller 6 pin relay that can be turned on just by the arduino pin. Also this battery is what I used for the CNC shield to power the CNC shield which has an input voltage range from 12V-36V. Here's some of the only data I could find on the shield CNC-Shield and then if you need it here's some for the DRV8825.

I know there are some capacitors included in the shield for the DRV8825 drivers but other than that I did not use any capacitors in my circuit.

Hi,
Your schematic page is not there.

PLEASE copy it as an EXPORTED jpg, and post it in a new post.
Your power supply;


Do you have a fuse in the positive lead?

Some images of your project would help too.

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

Hello 8675300
Design a multichannel voltmeter using an Arduino to collect datas over time. Use a simple terminal program to collect and store the datas provided.
I suspect that you will have a glitch when all the actuators are activated at the same time.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

EDIT:
Or disconnect the control circuit from the load circuit first.

Sorry for the delay. Here's the image and a picture in real life

And no I did not include a fuse in the positive lead and while I could see that maybe causing an issue the whole point of the driver is to limit the power supply going in and the battery being 12 volts is at the lowest spectrum of the 12-36V range

And by the way in the Tinkercad circuit the wire connected to the bottom right of the relay that's just floating on the other end was meant to symbolize the Electromagnet's power supply but I couldn't find one or a text box so...
Also on a side note I find a lot of people on this forum to be impatient or rude so I really admire your attitude and patience TomGeorge :slight_smile:

Hi,
Sorry, we need a schematic not a picture of components.

Can you please post an image of a hand drawn circuit, showing power supplies, component names and pin labels?

The reason for a fuse is to protect your project from a short circuit, that battery is capable of 100A or more into a short circuit.
If you do get a short, 100A will blow/burn your project into oblivion.
A fuse will blow before that amount of current will flow.

Do you know the current consumption of your project?

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

Ohh, well at the moment the project is not with me. I'll have it on Monday but I can't measure anything with my multimeter today. Other than that yeah the fuse would be a pretty good idea :upside_down_face:, completely didn't consider current there for a second. As you can probably tell I'm not that experienced with electronics :frowning: But do you still want a hand drawn schematic without any values other than power source?

you are powering the relay directly from Arduino's pin, that's sin#1

you haven't connected any kind of freewheeling diode across the relay's coil terminals, sin#2

may be THIS can help

So are you saying that the power dissipation from stored energy in the relay without any protection could've damaged the arduino?

We need a complete circuit diagram with all your components, named and pins used labeled.

Sorry but you are in troubleshooting mode now and a proper schematic is a good start to trying to find your problem.

Thanks... Tom... :smiley: :+1: :coffee: :australia:
PS, I'm off to bed, g'dnight... :sleeping: :sleeping: :sleeping: :sleeping:

Here's a circuit diagram I drew. If you want I'll be able to give the voltages and currents components are drawing by tomorrow.

Hi,
I think you have too much powered by the Arduino 5V.
What is the current draw of the barcode scanner?
You are using the internal 5V regulator to supply the scanner.

It sounds like the regulator is shutting down due to overload, with the shield on the Arduino you cannot feel if the regulator is getting hot.

Monitor the 5V pin on the Arduino with respect to gnd, and see what it measures when the problem occurs.

Why have you got the servo powered on 9V, they are only 5V devices.
What is your relay? Can you post link to data/specs?

You should not be controlling the relay directly from an Arduino output pin, it cannot supply the current needed?

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.