Atmega328p-pu (arduino bootloader) processor freezes/resets randomly

Hi fellow engineers,

I have updated the schematic of my project. The board for this was produced and all components are soldered to the board.

Now I am trying to make a "n20 gear motor" move using the motor driver, I have simplified my code, attached as image(screenshot).

Motor being used: n20 gear motor (produced in china, stalling current should be less than 250mA, normal current is approx 40-50mA.

When I run this code the processor sometimes resets and sometimes freezes completely, this happens frequently but randomly, almost within every minute.
The code itself is pretty simple, I think there is an electrical fault.

Does any of you have an idea what it can be? Thanks in advance

We prefer that you show a sketch between code-tags, use the </> button.
Can you give links to the motor driver, the motor, the 5V power supply, and the SSR ?
A photo of the pcb board would also help.

My guess is that you have GND traces on the PCB board connected to everything that needs GND without thinking about the different current paths for the microcontroller and the motor and the relay.

I will write down a few things to check:

  • The ground current of the motors can disturb the microcontroller. Do you have a ground plane in the pcb board ?
  • The X-tal and the two 22pF should be close to the ATmega328P.
  • I see only one GND connected of the ATmega328P. You must connect all GND pins.
  • Only two decoupling capacitors ? Is one at least near the ATmega328P ?
  • What is the analogWrite() doing in setup() ?
  • Even with a very small motor, add a larger capacitor to its power. For example 470µF to 0V and 5V.
  • It might be possible to drive the SSR directly with a pin of the ATmega328P. Do you know what current is required ?

There is no led ? How can you tell that the sketch is running ?

You need to be able to tell what is going on with the sketch. That means you have to add a usb-serial connection to be able to show messages. You can not start a fresh project without it.
That would be PD0 and PD1: I see you have them already on the programmer header.
In setup(), show a message: "The sketch has started". Then you know for sure if the ATmega has reset.

Once you can show messages with Serial.println(), then you can make a sketch that does a lot of tests.
Try a sketch without relay or motor. Just a blinking led.
Then try to switch the relay every second (or more) and let it run a few hours.
Then try the motor.
And so on.

Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you post a picture of your project and a jpg export of your PCB please?
Have you got bypass capacitors and suitably wide tracks to carry motor current?
What is the AC current/voltage that you are applying at J2, "heat"

If you remove the loads, motors and "heat" does the fault still occur?
What is your application?

Thanks.. Tom... :slight_smile: