Electron PWM Not Working on D2

I am currently trying to PWM a vibration motor on the electron from D2. The motor is connected directly to D2 and 3v3. analogWrite(D2, 255); has no effect but digitalWrite(D2, LOW) does turn the motor on.

That sounds correct as one end of your motor as connected to 3V3 and the other end is connected to the GPIO.

Did you check that the maximum current drawn by the vibration motor does not exceed 20mA?

You should also be able to see the motor moving with digitalWrite(D2,127) which has a PWM of 50% duty cycle

I may be totally wrong (I have been before), but In the case that you set the pin low the motor ran. That should be power from the 3.3 vdc to a voltage that is near ground potential. In the case where you set D2 to 255 it should be near 3.3 vdc, you would have basically no potential difference across the motor. Setting the PWM value to a low number would be like setting D2 to Low. Setting the PWM to 127 like @kennethlimcp said should be about half the voltage across the motor.


@carbuthn you’re right on the money. There was no potential difference across the pins with analogWrite(D2,255). The other issue I was running into was I had digitalWrite(D2,HIGH) in the setup(), which I think was causing issues with analogWrite().