ATMega 328p freezes but PWM pin keeps pulsating?

Hello everyone,

I'm having some trouble figuring out why does it happen. I'm controlling a DC motor with a Mosfet using an ATMega328p standalone (with crystal, capacitors, etc) and a potentiometer to regulate the speed.

I'm using a flywheel diode to prevent the spikes from the motor but for some reason the microcontroller is freezing from time to time.

The question is: Why is it that the PWM output keeps pulsating (I have checked it with an oscilloscope) if the microcontroller is frozen (it won't read the analog input potentiometer anymore nor modify the pulse width). It keeps the PWM output pulsating!

Is this normal?

The sketch is simple:

byte motor=3;
byte testpin=9;
byte pot=A0;

void setup() {

pinMode(motor,OUTPUT);
pinMode(testpin,OUTPUT);
pinMode(pot,INPUT);

}

void loop() {

analogWrite(motor,(analogRead(pot)/(1023.0/255.0)));

}

Thank you!

How do you know that the 328P freezes?

Usually code freezes but the hardware PWM will keep on running. Your code does not seem to be the reason.

How are you driving the motor?

I'm driving it using a CSD18502KCS Mosfet, as I usually do. The pin 3 is connected to its gate through a 1k resistor. It's working fine and there's nothing special about it. I connected a flyback diode across the motor. When I check with the oscilloscope I see a voltage increase of around 1V - 1.5V (spike) each time the PWM pulse goes off, which I'm not sure if shouldn't be happening since I'm using a flyback diode.

When I say that the 328p freezes it's because I assume that if writing an analog output is the only instruction it must do and suddenly it doesn't it must be because it's somehow frozen. Am I wrong?

Thanks for the help!

Hardware is not my strong point. So I can't help further.

I will ask a moderator to move this thread to a more suitable section (it's not an installation and troubleshooting question :wink: ). I suspect that those in the know will ask you to provide a schematic diagram; please provide one.

PS:
Sorry, I missed the description in the opening post how you knew that it was hanging.

I surmise that the TCn, once configured for PWM, runs without further attention from the processor. The processor can change this at any time but, since it's in la-la-land...

pplg:
ATMega328p standalone (with crystal, capacitors, etc) and a potentiometer to regulate the speed.

Circuit board? Breadboard? Got proper supply decoupling.
Post a diagram and a picture of the project.

analogWrite(motor,(analogRead(pot)/(1023.0/255.0))); // 1023? floats?

analogWrite (motor, analogRead(pot) >> 2); // bitshift is cleaner

Try this.
It only writes to the PWM pin once, after a pot change.
And you can see if the MCU is still alive on the serial monitor.
Leo…

const byte potPin = A0; // pot connected to A0, 5volt and ground
const byte motorPin = 3;
int potValue, previousPotValue;
byte motorValue, previousMotorValue;

void setup() {
  Serial.begin(9600);
}

void loop() {
  potValue = analogRead(potPin); // read pot
  if (potValue < (previousPotValue - 2) || potValue > (previousPotValue + 2)) { // add some deadband
    previousPotValue = potValue; // update value
    motorValue = previousPotValue >> 2; // convert 10-bit to 8-bit (0-1023 to 0-255)
    if (motorValue != previousMotorValue) { // if value has changed
      analogWrite(motorPin, motorValue);
      Serial.print("Motor value: ");
      Serial.println(motorValue);
      previousMotorValue = motorValue; // update
    }
  }
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
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 please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can I suggest you add the "blink without delay" code to your project, this will flash the on board LED and act as a heartbeat.
The heartbeat will indicate if your code has locked up.

Tom.... :slight_smile: