Atmega328P gets into hung while enabling/disabling Relay with AC Motors

Thread Starter

mukund_bm

Joined Aug 24, 2014
21
Dear Friends,

I am facing specific problem with the Atmega328P, I am trying to run 2HP Water Pump with Atmega328P + 40 AM single channel Relay. The controller starts the relay, but when it stops the relay the processor gets into hung stage. I am powering Atmega329P and relay with 2 separate inputs.

Kindly suggest

Code:

int MotorCtrlPin1 = 9; // Borewell Motor
int MotorCtrlPin1S = 11; // Borewell Motor Starter


// Function to reset the device
void(* resetFunc) (void) = 0; //declare reset function @ address 0

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);
delay(50);
Serial.println("");
Serial.println("Iniatilizing.....");

pinMode(LED_BUILTIN, OUTPUT);

pinMode(MotorCtrlPin1, OUTPUT);
pinMode(MotorCtrlPin1S, OUTPUT);

}
int testcycle =1;

void loop() {
Serial.println(testcycle);
digitalWrite(LED_BUILTIN, HIGH);
delay(10000);
if (testcycle < 2)
{
// Stop Button
digitalWrite(MotorCtrlPin1, HIGH);
delay(500);
digitalWrite(MotorCtrlPin1, LOW);
delay(500);

// Start Button
digitalWrite(MotorCtrlPin1S, HIGH);
delay(2000);
digitalWrite(MotorCtrlPin1S, LOW);
delay(500);

// Stop Button
digitalWrite(MotorCtrlPin1, HIGH);
delay(3000);
digitalWrite(MotorCtrlPin1, LOW);
delay(500);
resetFunc();
}
testcycle=5;
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}

Below is my schematic and code:

1632543422508.png
 

dendad

Joined Feb 20, 2016
4,451
Do you have the reset pin pulled up to 5V?
If not, add a 10K from reset to +5V.
And, add another 10K from the Q9 base to GND.
A 100nF cap from +5 to GND right at the CPU is a help too.
 

DickCappels

Joined Aug 21, 2008
10,153
Noise from the relay or the motor is probably getting into your controller.

Besides the 10k pullup resistor, you might want to add a .001 uf ceramic capacitor from the reset pin to ground. Be aware that this might affect your in-circuit programmer, but this was Atmel's advice.

You also need to add a .01 or .001 uf multilayer ceramic capacitor from each Vcc pin to ground to take care of high frequencies.

It may be a spike as the voltage across the relay coil changes quickly. An RC snubber across the diode that is across the coil might help.

You might also have to but a common mode choke in series with the two inputs to the optoisolator and add bypass capacitors to the +12 V supply right at the relay.

If you AC motor happens to have brushes (I think they exist) you may need to put RFI suppression capacitors between each motor terminal and earth at the motor to keep the nasty noise from working its way to the controller and confusing it.

The 2N3904 has an Ft of about 300 MHz so it may be oscillating as the relay turns on and/or off. That can be corrected with a small ceramic capacitor between the base and collector, or better yet, switch to something slower like a 2N2222.
 

Papabravo

Joined Feb 24, 2006
21,158
Is this a circuit done on a breadboard, or do you have a printed circuit board?
Don't discount the possibility that you have an error in your program. There are many reason for a program to hang and a prudent designer would eliminate them all.

EDIT: a couple of questions
  1. How does the resetFunc at address 0 do anything useful - except CRASH
  2. int testcycle = 1 is not inside setup() and is not inside loop(). When does it ever get executed?
 
Last edited:

Thread Starter

mukund_bm

Joined Aug 24, 2014
21
Is this a circuit done on a breadboard, or do you have a printed circuit board?
Don't discount the possibility that you have an error in your program. There are many reason for a program to hang and a prudent designer would eliminate them all.

EDIT: a couple of questions
  1. How does the resetFunc at address 0 do anything useful - except CRASH
    1. Using the below function to reset the microcontroller.
  2. int testcycle = 1 is not inside setup() and is not inside loop(). When does it ever get executed?
    1. testcycle object declared as global variable not inside the setup or loop, used in the loop only.
Thank you for the observation, please see my comments in-line and reset function below...

void(* resetFunc) (void) = 0; //declare reset function @ address 0
 

Papabravo

Joined Feb 24, 2006
21,158
Thank you for the observation, please see my comments in-line and reset function below...

void(* resetFunc) (void) = 0; //declare reset function @ address 0
Your answer does not shed any light at all on the situation. What is the processor actually doing when the function is called besides crashing?
 

BobaMosfet

Joined Jul 1, 2009
2,110
Thank you for the observation, please see my comments in-line and reset function below...

void(* resetFunc) (void) = 0; //declare reset function @ address 0
You don't start the MCU at location zero this way. You have absolutely zero understanding of the jump tables, memory space, stack-frames etc. Not how it's done.
 

DickCappels

Joined Aug 21, 2008
10,153
@BobaMosfet

Can you recommend some reference for Mukund_bm that might help them with this particular problem?

@mukund_bm
If I may suggest a test to help separate firmware from hardware problems: Remove the relay and replace it with an LED with a series resistor so you have an indicator. After that do things seem to be "right", or is the controller still crashing?
 

trebla

Joined Jun 29, 2019
542
Thank you for the observation, please see my comments in-line and reset function below...

void(* resetFunc) (void) = 0; //declare reset function @ address 0
Why you need make this soft reset? Your code never reaches this part:

C-like:
testcycle=5;
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
 

BobaMosfet

Joined Jul 1, 2009
2,110
@BobaMosfet

Can you recommend some reference for Mukund_bm that might help them with this particular problem?

@mukund_bm
If I may suggest a test to help separate firmware from hardware problems: Remove the relay and replace it with an LED with a series resistor so you have an indicator. After that do things seem to be "right", or is the controller still crashing?
@mukund_bm If all you want is for the chip to reboot, then set your reset to a routine to pulls the RESET pin in the appropriate direction. Chip will reset, pin will reset, it will start over- reliable, simple. Don't forget to set your lockbits appropriately

1632771644718.png

If you want to educate yourself on what it takes to do it in software- learn about the reset vector-

1632771830784.png
 
Last edited:

Papabravo

Joined Feb 24, 2006
21,158
@BobaMosfet

Can you recommend some reference for Mukund_bm that might help them with this particular problem?

@mukund_bm
If I may suggest a test to help separate firmware from hardware problems: Remove the relay and replace it with an LED with a series resistor so you have an indicator. After that do things seem to be "right", or is the controller still crashing?
The FIRST reference that should always be consulted is the datasheet. After that the Instruction Set Manual, along with the assembly language list of whatever program you are trying to debug.

One more thing: If you want to engage in bare metal programming you need to drastically improve both your skillset and your tools. Otherwise you will spend boatloads of time just spinning your wheels.
 
Last edited:

p55xp

Joined Jul 19, 2009
4
This is not a new problem, I faced same while switching an inductive load with a relay, or even a triac, sending command from a microcontroller. I have this experience with both PIC and 8051 micros. Every time I switched the program get scrambled. You need to clean the microcontroller running environment from allied EMI, RFI interference. Try using 0.1uF ceramic disc capacitor at Vcc pin of the microcontroller as near as possible across Gnd. This is how I solved my problem in both PIC and 51 core mcu. It may help in your situation too.
 

Papabravo

Joined Feb 24, 2006
21,158
That's funny. I've never had this problem because we always had PCB designers that knew what they were doing. There is no substitute for the right talent in product development.
 
Last edited:
Top