[Solved]Driving a single servo



  • using the M5 with firmware 1.2.3en and arduino 1.8.9

    I know you can't use servo.h, so I came up with a work around, or at least I thought I did.

    I cannot have M5.begin(); in the code or the servo doesn't work, without that the servo works but I cannot write to display.
    Is there a solution? here's the example code;

    #include <M5Stack.h>

    #define servoPin 5
    int i = 0;
    int servoPulse(int angle) {
    int pulseWidth = map(angle, 0, 180, 544, 2400);
    return pulseWidth;
    }

    void setup() {

    m5.begin();
    Serial.begin(9600);
    pinMode(servoPin, OUTPUT);
    

    }

    void loop() {

    for (int i=0; i<=180; i++) { // drive servo from 0 to 180 at max response
    int pulseWidth = servoPulse(i);
    //digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulseWidth);
    //digitalWrite(servoPin, LOW);
    delayMicroseconds(20000 - pulseWidth);
    Serial.print(i);
    }

    for (int i=180; i>=0; i--) { // return to 0 at half speed
    int pulseWidth = servoPulse(i);
    //digitalWrite(servoPin, HIGH);
    delayMicroseconds(pulseWidth);
    //digitalWrite(servoPin, LOW);
    // delayMicroseconds(20000 - pulseWidth);
    delay(50);
    Serial.print(i);
    }

    }


  • M5Stack

    Hi, Please try to comment out this line -> Serial.begin(9600);

    Thank you!


  • M5Stack

    (Already set to serial speed 115200 in M5.begin () function)



  • I was hoping that it would be that simple, however, that wasn't the solution.

    I cannot use M5.Lcd.print and the servo at the same time.



  • What device are you using?
    I use G21 which is also SDA on port A



  • I'm using an M5stack Core with the accelerometer built in, and arduino 1.8.9

    This problem occurs when using M5.begin(); which I apparently need to print to lcd inside the loop.

    So for instance, inside the loop,

    M5.Lcd.setCursor(0, 200);
    M5.lcd.setTextColor(WHITE,BLACK);
    M5.Lcd.print (i);

    The Servo will not work, but instead chatter.
    // M5.begin(); servo works but the display does not (this statement is inside the setup, for example,

    void setup() {

    M5.begin();

    //myservo.attach(servoPin);

    }


  • M5Stack

    @jimw Please try this code.
    Serial speed 115200

    and the firmware (1.2.3-en) for micropython
    It is not for Arduino.

    #include <M5Stack.h>
    
    int servoPin = 5;
    
    void servoSet(int Angle, int Time) {
      int pulseWidth;
      int TimeCount;
    
      TimeCount = Time / 20;
    
      for (int i = 0; i <= TimeCount; i++) {
        pulseWidth = map(Angle, 0, 180, 544, 2400);
        digitalWrite(servoPin, HIGH);
        delayMicroseconds(pulseWidth);
        digitalWrite(servoPin, LOW);
        delayMicroseconds(20000 - pulseWidth);
      }
    }
    
    void setup() {
      M5.begin();
      M5.Lcd.printf("M5Stack Servo test:\n");
      pinMode(servoPin, OUTPUT);
    }
    
    void loop() {
      M5.Lcd.printf("Angle 45deg\n");
      Serial.println("Angle 45deg");
      servoSet(45, 1000); //45deg
      M5.Lcd.printf("Angle 135deg\n");
      Serial.println("Angle 135deg");
      servoSet(135, 1000); //135deg
    }

Log in to reply
 

Looks like your connection to M5Stack Community was lost, please wait while we try to reconnect.