Arduino Motor Control

Hi

I am trying to use Arduino PWM to control motor speed. The aim is that when someone turns on the switch, this sends a high input to one of the Arduino pins (12). Then when pin 12 goes high, I want pin 11 to also go high in order to turn on the transistor which controls the motor. I want to keep pin 11 on full voltage for around 5 secs to give a “push” to the motor, then I want to use PWM to moderate the speed. My code is shown below:

int switchPin = 12;// this is the arduino pin which is attached to the switch
int TransistorPin =11;

void setup()
{
  pinMode(TransistorPin, OUTPUT);//this is the pin which turns on the transistor
  pinMode(switchPin, INPUT);
  
}

void loop()
{
  while(true){
    if (digitalRead(switchPin)==LOW){//if the switch is off, keep the motor off
      digitalWrite(TransistorPin, LOW);
      
    }
    if(digitalRead(switchPin)==HIGH){//if switch is high, turn on motor
      digitalWrite(TransistorPin, HIGH);
      delay(5);//keep motor on at full voltage for 5 seconds
      while (digitalRead (switchPin)==HIGH){
        digitalWrite(TransistorPin, HIGH);//then begin pwm
        delayMicroseconds(3000); //this ratio gave roughly the right speed
        digitalWrite(TransistorPin, LOW);
        delayMicroseconds(1000);
      }
    
    
      digitalWrite(TransistorPin, LOW);//turning off again
    }
      
    }
}

However, this is not working, and the chassis on which the motor is mounted needs a small shove to start moving. Any ideas where I might be going wrong? Thanks!

However, this is not working

What is it doing? Does the switch not work?

You posted code, but your next statement makes me think your code works:

the motor is mounted needs a small shove to start moving

Sounds to me like you don’t have enough power going to the motor.

What kind of motor? What specs (volts, amps)? How many volts are you giving it? What is powering the motor? Do you have a schematic of the wiring?

In addition to the above questions re: how the motor is powered and wired, have a looked at this line.

delay(5);//keep motor on at full voltage for 5 seconds

The delay() function’s argument is msecs so you aren’t getting 5 secs of full power, you’re getting just 5 msecs of full power.

Also why do you have this while in there:

void loop()
{
  while(true){
    if (digitalRead(switchPin)==LOW){//if the switch is off, keep the motor off
      digitalWrite(TransistorPin, LOW);
     
    }
    if .... more code follows

I don’t think it hurts but it appears all you’re doing is putting another infinite loop inside of the infinite main loop().

BTW if you followed one of the SIK tutorials on using a transistor to control the motor be aware there’s (IMO) a mistake in the base transistor value. IIRC they used a 10k value and it needs to be more like 200-300 ohms inorder to allow the full current flow.

Good catch Mee, when I read his post, the last line, I didn’t bother to read the code.

But I suspect that’s not the whole problem. Either his motor is weak, or geared wrong, for the vehicle (because 50% duty cycle should make it move) or there’s something less than good in the wiring or drive of the transistor or supply voltage or ???