I have connected DRV8835 with 2 DC motors with 11 power supply. Arduino nano.

When I for loop like i from 0 to 1024 for analogWrite(i,Motor1) for the DC Motor1 it's accelerating from 0 to 200 and it's stopping. When I becomes 250 it's starting at higher speed. When it becomes 500 it's stopping again. And 550 we have our power again.

Now I tried to run analogWrite(1000, ***) but it's not working. When I change the value from 1000 to 1005 it's not working again. But at 1024 we have the maximum speed and it is working?

 #define LEFT_PWM 9
 #define RIGHT_PWM 10
 #define LEFT_DIR 5
 #define RIGHT_DIR 6

void setup() {
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

void loop() {
  digitalWrite(LEFT_DIR, HIGH);
  analogWrite(LEFT_PWM, 1000);

analogWrite has the following prototype

void analogWrite(uint8_t, int);

which means that its first parameter has [0, 256) range and follows the rules of modulo arithmetic, with 256 as the modulo. If you supply arguments outside of [0, 256) range, they will be interpreted % 256. 1000 is the same as 232 (1000 % 256 == 232), 500 is the same as 244, 550 is the same as 38 and so on.

This is not fully consistent with the symptoms you describe (if your description is accurate), which might imply that something else is a factor here. Are sure you are using your DRV8835 in PHASE/ENABLE mode (pin 11 pulled high)?

  • The pin MOD was the real problem. Soldered it to VCC with a wire and problem is solved. Before that the motors behavior was very random. – Lary May 8 '19 at 22:22

The "value" for analogWrite() should be an integer between 0 - 255.

The arduino.cc website is a great resource for such information: analogWrite(). Language Reference.

  • Unfortunately, arduino.cc is a very poor resource. A good resource would tell us the exact types of analogWrite arguments, i.e. the full C/C++ function declaration. arduino.cc doesn't. – AnT May 8 '19 at 20:23
  • @AnT - It's a great resource for beginners who are making the most basic mistakes :) – VE7JRO May 8 '19 at 20:26
  • 1
    .. facepalm.. I've been coding on nodemcu and TI controllers where we have more bits. and the analog rage is up to 4096 – Lary May 8 '19 at 22:01
  • @Lary analog range depends upon the resolution of your ADC. Ardunio support ADC with 10 bit resolution that means 2^10 bits of value = 4096 max value. Here in analogWrite is used to generate a PWM signal with certain duty cycle. The % of duty cycle depends upon value you write in the function. 255 means 100% duty cycle. – Vaibhav May 9 '19 at 5:43
  • @Vaibhav: 2^10 = 1024. And the range of values returned by the ADC is 0 – 1023. – Edgar Bonet May 9 '19 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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