# DC motor speed control

I am trying to control the speed of motor using ADC value received using ATMega16, the system works well with 12V. But when I go for 24V the motor runs continuously, even when ADC value is 0.

I have checked with oscilloscope, the PWM output suddenly goes HIGH and remains HIGH even when ADC input is 0.

I believe that the code is correct since the system works perfect in case of 12V.

#include<avr\io.h>
#include<avr\interrupt.h>
#define F_CPU 1000000UL
#include <util/delay.h>

int del;
int x,x1,z,z1;
int rpm = 0;

int main (void)
{
DDRA = 0X00;
DDRD = 0xFF;
DDRC = 0xFF;
DDRB = 0xFF;

while (1)
{

{
PORTD = x;
PORTC = x1;
del = (del-512)/50;

if (del > 0)
{
PORTB = 0x08;
_delay_ms(del);
PORTB = 0x00;
_delay_ms(10-del);
}

else
{
PORTB = 0x00;
}
}


MOSFET Datasheet: http://www.mouser.com/ds/2/205/ixys_98625-342386.pdf

• wrt edited question : something the motor is doing is interfering with the CPU (crashing it or latching it up). I see far too little decoupling on the PSU for a start, and no protection on PortB.3 against spikes fed back (e.g. via the Miller capacitance) through the MOSFET gate. Also, the MOSFET is big and slow; what is your PWM frequency? – Brian Drummond Jan 6 '18 at 16:18
• PWM frequency: 1Khz. The MOSFET is big because later this circuit will be used to run a 60V motor, with a heavy load. How do you suggest I proceed with the interference part ? – David Flo Jan 6 '18 at 16:20
• @KanishakKatahra Why haven't you added a series resistor to the MOSFET gate (limit oscillation and current limit) and a resistor from gate to ground (MOSFETs at low gate voltages look like constant-current sinks instead of switches that are OFF.) – jonk Jan 6 '18 at 17:25
• What values should the resistors be ? You are suggesting that the MOSFET be in OFF state unless a resistor from gate to ground is added ? – David Flo Jan 6 '18 at 17:28
• @KanishakKatahra Since MCUs often have about $50-100\:\Omega$ series impedance anyway, and your MOSFET gate is in the nF range, try $220\:\Omega$ for the series resistance. Also tie the gate down to ground with perhaps $10\:\text{k}\Omega$? I don't know your switching requirements, but as far as guesses go those are as good as any. If that doesn't help, then it's a different problem or else you need an even more extreme driver change. – jonk Jan 6 '18 at 18:00