Atmega328P crash when PWM is activated to drive a NPN-MOSFET-HEATER system

Thread Starter

Riccds

Joined Aug 4, 2022
5
Hi all, I have a problem that's getting me nuts. I have milled my first PCB, it's a PID system to control a heater for a 3d printer nozzle(has also NTC thermistor, fan+switch, LCD, rotary encoder). The system was working fine when on Arduino. Then I milled it and I think that something in the layout is generating a disturbance that crashes the 328. The LCD starts displaying odd characters and the MCU crashes or resets. This always happens soon after I activate the PWM control.
The PWM drives an NPN S8050 transistor, driving itself a IRFZ44N MOSFET, opening flow for a 40W 12V heater element. The problem became much more stronger when I started applying linear ramps to the PWM signal level in an attempt to smooth out possible spikes. And always crashes during a ramp up or down.
Below is the schematic, there are also two 0,1 uF caps between the 328 VCC-GND and AVCC-GND near the MCU that are not depicted. This discussion is very similar to this one https://forum.allaboutcircuits.com/...elf-if-i-start-pwm.160079/page-2#post-1397598 in which @BobTPH suggests the current is very high(50A) in his system. However I don't understand that calculation. In my case I have considered a maximum of around 3,3 A drawn by the heater. Do you have any suggestion?
circuit_explained.png
View attachment 282150
Back.png
3dFront.png
3dBack.png
 
Last edited:

MrChips

Joined Oct 2, 2009
30,707
There could be any number of reasons for intermittent behavior, chief of which are bad board layout, improper power supply filters, high current spikes.

Here are the things I would be looking at:
1) Reroute your traces to the XTAL oscillator. Keep traces between the MCU and XTAL as short as possible. Connect C1 and C2 to the bottom layer ground plane.

2) Reroute the trace to /RESET. Keep the trace short. Check that /RESET has the recommended pullup and capacitor.

3) Check all Vcc and GND pins on the MCU. Make sure that the 100nF capacitors are tied directly to Vcc and GND pins.
Install POWER and GND plane.

4) Make sure that there is at least one 10-47μF capacitor between Vcc and GND on the output of U2.
C4, C5, C6 are useless on the input side of the 7805. You want them on the Vcc and GND pins of your ICs. Your long traces make them even more useless. Your entire PCB layout of U2 does not help.

5) Repeat: install Vcc plane and GND plane.

PCB layout is as much an art as it is a science. Just connecting point-A to point-B is not good enough.
Think electron or current flow. How far and how many bends do I have to navigate to get from point-A to point-B.
Keep it simple and make it pretty.
 

Papabravo

Joined Feb 24, 2006
21,159
I have several suggestions that have nothing to do with board layout:
  1. Check each pin with multiple functions to see that it is correctly configured an initialized. Pins which can trigger interrupts are especially vulnerable
  2. Check to make sure that each interrupt has a valid interrupt vector and that there is a recovery mechanism for unexpected interrupts.
  3. Check the watchdog mechanisms to make sure that they are not triggering the reset.
  4. Check for jumps or calls to locations with no valid code at that location.
By themselves, each of these things has a low probability of being the problem. Elimination them will help you narrow the remaining set of potential problems.

Also can you explain the meaning of "I have milled my first PCB"? To an amateur machinist it sounds like you made it on a Bridgeport milling machine which is a bit incongruous to say the least.
 

Thread Starter

Riccds

Joined Aug 4, 2022
5
@MrChips Thank you for your suggestions. Point 3 is already done as you say.For L7805 caps C4,C5,C6, I followed the reccomendation from the datasheet but I forgot some old good theory so there was no thought from my part.
On all the points you mentioned I am making the changements, I will try first to jumper and solder on the same board before making a new layout.
Just one question: I have a front and back GND plane, but I didn't make a Vcc plane. You mean a 5V plane I guess. This means I should create sparse areas of both GND and Vcc, both on the front and on the back correct? I will research this topic better anyway. Thanks

@Papabravo Thank you too for your suggestions.
I have an external interrupt for the rotary encoder push-button signal and a timer interrupt that triggers the refresh of the LCD temperature reading each second. I wanted it to update regularly, so as the code is getting a bit complicated and timing difficult to manage that was the best way I could find and it works. However I don't know exactly if this can interact destructively with a do-loop in which I analogwrite the PWM ramp.
On the other points I have to study a bit and will check everything as I am not too educated on those subjects.
For the milling thing, lol, you are correct, engraved is probably better said altought I found people saying "I milled the PCB" so I just followed. Forgive me also I am not mother toungue english. I engraved the FR4 PCB on a CNC 6040 table machine. I would say it's a halfway between an engraver/router and a light milling machine. The motor is a 2.2 kW, 24k rpms. I have engraved/milled/cut also steel on that machine, very slowly but It made it. Result of the PCB was pretty neat.
engraved.jpg
 
Last edited:

MrChips

Joined Oct 2, 2009
30,707
@MrChips also, would you use a tank capacitor at the PSU connection to the board, between 12V and GND?
Yes. There should be about 100-470μF electrolytic capacitor where 12VDC enters the board.
Plus there should be about 1-10μF at the input pin of the 7805 regulator.
 

Papabravo

Joined Feb 24, 2006
21,159
@Papabravo Thank you too for your suggestions.
I have an external interrupt for the rotary encoder push-button signal and a timer interrupt that triggers the refresh of the LCD temperature reading each second. I wanted it to update regularly, so as the code is getting a bit complicated and timing difficult to manage that was the best way I could find and it works. However I don't know exactly if this can interact destructively with a do-loop in which I analogwrite the PWM ramp.
On the other points I have to study a bit and will check everything as I am not too educated on those subjects.
...
The thing you must be careful with when there are active interrupts is to protect critical sections. Reading and writing a single byte to memory or I/O registers is always safe. Reading and writing multiple bytes is NEVER safe. You must protect multiple byte reads and writes by disabling interrupts before the operation and enabling them when the operation is complete. This is especially true for multi-byte timers and A/D conversions.
 
Last edited:

Thread Starter

Riccds

Joined Aug 4, 2022
5
Yes. There should be about 100-470μF electrolytic capacitor where 12VDC enters the board.
Plus there should be about 1-10μF at the input pin of the 7805 regulator.
So, I have proceeded making the modifications you suggested. I started stepwise with what I had on hand and rechecked if it crashed at each step. First thing I added a 100 uF at Vcc-GND of L7805. This didn't work. I further added a 100 uF at the PSU connection to the board. This one changed the whole thing even removing the first one on L7805. The system appears being very stable now. I have run it now for a few hours without crash and the LCD does not show signs of flickering. I'll keep stress testing it for a while and then I'll place it in operation.

Either way I will have to redesign the circuit as it's not very healthy to have pending capacitors under the board and have to add a few other things. I will follow your suggestions but after reviving some study material on filtering just to have a better idea of what I'm doing.
Your help was very appreciated, this thing has been a pain.Thanks
 
Top