Custom PCB issues with ATmega328p & Hbridge

Thread Starter

Brettjohnson7191

Joined Apr 28, 2022
27
Hello All I am new to this forum so let me introduce myself.
I am currently a senior EE major at the UM Duluth. I am working on a custom PCB that is giving me some issues. Now this is just the second PCB that I have ever designed so If you see anything at all that you would change or do different PLEASE let me know. I would love to learn more about this area of the field.
PCB specs
Power supply 2 18650 batterys at 7.4V
IC- ATmega328P
L298N hbridge

The PCBS job is to monitor an Infared sensor and when it sets a pin high it has a list of instructions to execute some of which include operating 3 small 6V dc motors that need to be turned CW and CCW. Currently I am using the Hbridge as that is what I originally used to make the circuit work using an Arduino Uno and qunqi L298N motor drive board. I plan to change this later to a Fet based design to avoid the voltage drop and low input voltage issues.

Now my PCB works however I have issues with the Hbridge. These include a 3V drop at the motors which seems higher then it should be and when the motors are running sometimes it glitches out where the motors just rapidly change direction and freeze the IC in that command until I turn off power supply. I am wondering If this is a back EMF issue or a ground bounce issue. I did not have this large of a voltage drop in my circuit with the UNO and drive board and it worked perfect then so I know it is an issue with my PCB.
Attached are my schematic and IDE code.
Code:
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int IN1 = 9;
int IN2 = 8;
int IN3 = 7;
int IN4 = 6;
int trg = A1;
int swt = A2;
int count = 0;

void setup() {
  pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(trg, OUTPUT);
pinMode(swt, INPUT);
//HBRIDGE PINS
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Mouse Masher");
  Serial.println("Mouse Masher");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print("V2");
  Serial.println("Mouse Masher");
  int Sensor_Value= digitalRead(swt);
  Serial.println(Sensor_Value);
    if(Sensor_Value == HIGH)
    {
      //fireing
      digitalWrite(trg,HIGH);
      delay(750);
      digitalWrite(trg,LOW);
      //delay(10800000);
      delay(5000);
      analogWrite(trg,HIGH);
      delay(500);
      //pause for kill and wind up smasher
      digitalWrite(IN1,HIGH);
      delay(1750);
      //wind up trap arm
      digitalWrite(trg, LOW);
      delay(500);
      //engage latch
      digitalWrite(IN1,LOW);
      delay(500);
      //disengage windup
      digitalWrite(IN2, HIGH);
       delay(250);
       //add slack
       digitalWrite(IN2,LOW);
       delay(500);
       //shutoff slack adjustment
       digitalWrite(IN3,HIGH);
       delay(500);
       digitalWrite(IN3,LOW);
       //activate mouse remover
       digitalWrite(IN4,HIGH);
       delay(750);
       digitalWrite(IN4,LOW);
       delay(5000);
       count++;
    }
}
Moderator edit: Added code tags like this [code]... your code [/code]
 

Attachments

MrChips

Joined Oct 2, 2009
30,707
Welcome to AAC!
Hello All I am new to this forum so let me introduce myself.

Now my PCB works however I have issues with the Hbridge.

I did not have this large of a voltage drop in my circuit with the UNO and drive board and it worked perfect then so I know it is an issue with my PCB.
Don't be so quick to jump to conclusions. Not because a system worked before does it mean that it is correct.

How is the motor being powered?
For starters as a test I would suggest using a separate power supply for the motors. This puts the microcontroller board on its own power supply.
 

Thread Starter

Brettjohnson7191

Joined Apr 28, 2022
27
Welcome to AAC!

Don't be so quick to jump to conclusions. Not because a system worked before does it mean that it is correct.

How is the motor being powered?
For starters as a test I would suggest using a separate power supply for the motors. This puts the microcontroller board on its own power supply.
The motors are being powered by the full 7.4V through the H bridge the Microcontroller is being powered by the same 7.4V batter but its run through a voltage regulator
 

nsaspook

Joined Aug 27, 2009
13,079
Where are your (low ESR and low ESL) bypass and energy capacitors? I only see two 22pf capacitors connected to VCC/AVCC on the controller, that's not a normal value for that. Normally you would use values like 0.1uf for a chip bypass and maybe 10uf for device energy caps to reduce voltage rail drops. The L78** regulator needs capacitors on the input and output for stability and to improve transient response.

1651178368281.png

https://www.autodesk.com/products/eagle/blog/what-are-decoupling-capacitors/
 

Thread Starter

Brettjohnson7191

Joined Apr 28, 2022
27
Where are your (low ESR and low ESL) bypass and energy capacitors? I only see two 22pf capacitors connected to VCC/AVCC on the controller, that's not a normal value for that. Normally you would use values like 0.1uf for a chip bypass and maybe 10uf for device energy caps to reduce voltage rail drops. The L78** regulator needs capacitors on the input and output for stability and to improve transient response.

View attachment 266050

https://www.autodesk.com/products/eagle/blog/what-are-decoupling-capacitors/
Thank you! I will add the ESR and ESL caps I wasn't aware of them being needed but I see it now in the data sheet. That could be part of the issue because the microcontroller works perfectly till the H bridge is turned on and 500mA or so current is drawn
 

DickCappels

Joined Aug 21, 2008
10,153
Most likely noise from the bridge or the motors, particularly if the motors have brushes, is crashing the controller. You need bypass capacitors to keep the noise from getting from where it is generated to what is susceptible.

You should have good high frequency bypass capacitors located right at the bridge driver and the microcontroller.

For the both chips 10 uf or larger in parallel with a 0.1 uf or .01 uf ceramic capacitor.
The bridge might need a larger capacitor than the 10 uf mentioned above.

If the motors have brushes they are generating radio frequency signals and bypass capacitors can help keep those signals from getting into your controller. Below are some ideas.
1651224487953.png1651224497115.png1651224505531.png

Back in the days when Polaroid was a profitable company I designed the electronics for a film recorder that used a motorized Polariod film back. After every few images the microcontroller would crash and the motor in the film back would run continuously, dumping the entire pack of film on the floor. It was that dirty motor in the film back with wire brushes.
 

Thread Starter

Brettjohnson7191

Joined Apr 28, 2022
27
Most likely noise from the bridge or the motors, particularly if the motors have brushes, is crashing the controller. You need bypass capacitors to keep the noise from getting from where it is generated to what is susceptible.

You should have good high frequency bypass capacitors located right at the bridge driver and the microcontroller.

For the both chips 10 uf or larger in parallel with a 0.1 uf or .01 uf ceramic capacitor.
The bridge might need a larger capacitor than the 10 uf mentioned above.

If the motors have brushes they are generating radio frequency signals and bypass capacitors can help keep those signals from getting into your controller. Below are some ideas.
View attachment 266092View attachment 266093View attachment 266094

Back in the days when Polaroid was a profitable company I designed the electronics for a film recorder that used a motorized Polariod film back. After every few images the microcontroller would crash and the motor in the film back would run continuously, dumping the entire pack of film on the floor. It was that dirty motor in the film back with wire brushes.
Thank you! I assume for the microcontroller and Bridge I would just add the high frequency bypass capacitors at the Vcc pins? I will add all of these suggestions and will report back. I can't thank you guys enough they don't teach us this stuff in school so glad I have access to infinite knowledge on here.
 

MrChips

Joined Oct 2, 2009
30,707
It is generally accepted as good engineering practice to install a 0.1μF ceramic capacitor between Vcc and GND pins at every IC on your PCB.

They don’t teach you this in school unless you were in my lab class.
 

KeithWalker

Joined Jul 10, 2017
3,063
Your problem is caused by switching the direction of the motor without giving it time to stop first. Changing direction at speed generates a large current surge and back EMF which is feeding back through the supply, causing the controller to glitch. The best way to do it is to introduce a pause long enough for the motor to stop and then to ramp the speed up rather than switching on at full speed.
 

Ian0

Joined Aug 7, 2020
9,667
I would recommend a diode and a series resistor (which could usefully be a 25Ω PTC thermistor for overload protection) between the battery and the input of the 7805. Then put at least 470uF on the input the regulator and 10uF aluminium electrolytic on the output, and make sure that all the power earths are kept together, and all the signal earths are kept together and only joined at the 0V terminal of the regulator, or at the main decoupling capacitor.

P.s. My wife was a professor (of history) at UMD before moving to England
 

Thread Starter

Brettjohnson7191

Joined Apr 28, 2022
27
Problem Solved! I added the 470uF cap on the 7805 input and 10uF on the output and it solved the stability issue. I also incorporated everyone's suggestions into the schematic and plan to order one more board after further testing. Thank you all so much!
 

nsaspook

Joined Aug 27, 2009
13,079
Problem Solved! I added the 470uF cap on the 7805 input and 10uF on the output and it solved the stability issue. I also incorporated everyone's suggestions into the schematic and plan to order one more board after further testing. Thank you all so much!
Welcome to engineering.

These are the types of we mistakes we all make but should only make once(right!). This often happens when you copy something without actually understanding why each component was added to the electronic circuit.

Attention to detail will save you much sorrow.
 

Ian0

Joined Aug 7, 2020
9,667
Problem Solved! I added the 470uF cap on the 7805 input and 10uF on the output and it solved the stability issue. I also incorporated everyone's suggestions into the schematic and plan to order one more board after further testing. Thank you all so much!
A little bit of energy storage works wonders when there is something that puts short, heavy demands on the power supply.
Don't forget the diode - it stops the motor from stealing energy from the 470uF capacitor when it takes a lot of current, and that helps keep the input to the 7805 stable, and regulators don't have infinite PSRR.
It also stops you blowing up a board full of logic when (I say "when" not "if", because it will definitely happen sooner or later!) you connect the supply up backwards. (If you don't, then your customers will, when you get out into the real world!)
 

KeithWalker

Joined Jul 10, 2017
3,063
You are treating the symptoms rather than the cause. Your motor will not last very long if you keep reversing it while it is running. You will burn out the brushes and commutator.
 

nsaspook

Joined Aug 27, 2009
13,079
You are treating the symptoms rather than the cause. Your motor will not last very long if you keep reversing it while it is running. You will burn out the brushes and commutator.
He has multiple root causes here of problems. His circuit shouldn't glitch because of that.
 

KeithWalker

Joined Jul 10, 2017
3,063
He has multiple root causes here of problems. His circuit shouldn't glitch because of that.
Why not? He has one problem caused by reversing the motor while it is running. The reversed voltage adds to the back EMF and causes a large voltage spike followed by a momentary short across the motor as it reverses. That's enough to interfere with any microcontroller on the same battery supply. The solution is to stop the motor before reversing it. If the motor still causes interference when it starts up, a soft start will help to minimize that. It's very easy to modify the program to do all that.
 

nsaspook

Joined Aug 27, 2009
13,079
Why not? He has one problem caused by reversing the motor while it is running. The reversed voltage adds to the back EMF and causes a large voltage spike followed by a momentary short across the motor as it reverses. That's enough to interfere with any microcontroller on the same battery supply. The solution is to stop the motor before reversing it. If the motor still causes interference when it starts up, a soft start will help to minimize that. It's very easy to modify the program to do all that.
I build design and build 3-phase motor controllers from scratch.
https://forum.allaboutcircuits.com/threads/pic32mk-mc-qei-example.150351/post-1615116

There is often a requirement for reversing a motor before stopping as a safety measure for personal and product. If your controller glitches (the drivers and output stage might get very unhappy) during this then there is a hardware problem that needs to be fixed, period. Never depend on software to fix a hardware deficiency.
 
Last edited:

KeithWalker

Joined Jul 10, 2017
3,063
This is not a safety issue and the motor is not 3 phase. It is a small DC motor with very rudimentary brushes and commutator. It will not survive a lot of abuse. The problem is being caused by the software which should not reverse the motor while it is running because that causes a lot of unnecessary electrical noise on the supply and will eventually damage the motor.
 

Thread Starter

Brettjohnson7191

Joined Apr 28, 2022
27
This is not a safety issue, and the motor is not 3 phases. It is a small DC motor with very rudimentary brushes and commutator. It will not survive a lot of abuse. The problem is being caused by the software which should not reverse the motor while it is running because that causes a lot of unnecessary electrical noise on the supply and will eventually damage the motor.
I am not reversing the motors while they are running. I had one place in my code where I forgot a delay in the initial code, I posted but already corrected that along with my other issues. Otherwise, I have a decently large delay in-between turning the motor one way and then switching directions I changed most delays to 500ms. I am using a small geared down high torque dc motors to wind something up that is spring actuated. Motors are not spinning more than probably 10-20rpm.
 
Top