1

i have a custom atmega328p board which have connected to pin 9 the gate of a MOSFET. If i run a simple sketch like:

void setup() {

}
void loop() {
  analogWrite(9,70);
}

The motor connected to the mosfet works just fine, from 70pwm to 255pwm.

On the other sketch i'm receiving serial data from an android app via bluetooth. Ive 2 Servo motors and a DC motor connected to the mosfet. The 2 servos works like expected, the DC motor doesn't spin unless i use a 255pwm.

void loop() {
  if(Serial.available() > 0){     
      state = Serial.read();  
  }
  if (state == 'S') {
      servoONE.write(90);
      servoTWO.write(90);
      analogWrite(MOSFET,MOSFET_LOW);
  }
  else if (state == 'F') {
      analogWrite(9,70);
      servoONE.write(90);
      servoTWO.write(90);
  }
  else if (state == 'L') {
      servoONE.write(MAX_SERVO);
      servoTWO.write(MIN_SERVO);
  }
  else if (state == 'R') {
      servoONE.write(MIN_SERVO);
      servoTWO.write(MAX_SERVO);
  }
  else if (state == 'Q') {
      analogWrite(MOSFET,MOSFET_HIGH);
      servoONE.write(MAX_SERVO);
      servoTWO.write(MIN_SERVO);
  }
  else if (state == 'E') {
      analogWrite(MOSFET,MOSFET_HIGH);
      servoONE.write(MIN_SERVO);
      servoTWO.write(MAX_SERVO);
  }
}

Do you know why the same motor works on the first sketch with any pwm above 70 and on the second sketch it only works at 255?

1 Answer 1

3

You have a timer conflict.

Pin 9 is OC1A which is the Output Compare Channel A pin of Timer 1. However the Servo.h library also uses timer 1.

So you can't use servos and still use pins 9 (OC1A) and 10 (OC1B) as PWM.

However, you can still use pins 3, 5, 6 and 11 as PWM since they are on other timers.

3
  • Ok, thanks for the quick answer, i see the pin 11 is the MOSI pin and im using an ISP programmer to program my board. Will i be able to program my board if i use pin 11 as PWM ? Commented Jun 13, 2017 at 9:57
  • Of course. So long as what you have connected to it doesn't interfere with the ICSP signal - and the gate of a MOSFET shouldn't.
    – Majenko
    Commented Jun 13, 2017 at 10:06
  • Tested right now using pin 3 instead of pin 9, it works!!! You saved me a lot of troubles :D Commented Jun 13, 2017 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.