How to control a servo

hello i have a NUCLEO-H7A3ZI-Q and i’m trying to learn how to control a servo -SG90. my program is on keil studio cloud, and it doesn’t look working :frowning: Here is my programe :slight_smile:

#include “mbed.h”

PwmOut servo(PB_3);

int main() {

servo.period_ms(20);

while (true) {
 
    servo.pulsewidth_ms(1); 
    ThisThread::sleep_for(2s);

    servo.pulsewidth_ms(1.5); 
    ThisThread::sleep_for(2s);

   servo.pulsewidth_ms(2); 
    ThisThread::sleep_for(2s);
}

}

Thanks for your help :wink:

Hello,

the parametr of pulsewidth_ms (int ms)should be the integer so you cannot pass value 1.5. So for 1.5 ms you have to change it to pulsewidth_us (int us).

Try to change it like below

        servo.pulsewidth_us(1000);
        ThisThread::sleep_for(2s);
        servo.pulsewidth_us(1500);
        ThisThread::sleep_for(2s);
        servo.pulsewidth_us(2000);
        ThisThread::sleep_for(2s);

BR, Jan