🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    [Solved]Driving a single servo

    Arduino
    3
    7
    11.1k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      JimW
      last edited by m5-docs

      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);
      }

      }

      1 Reply Last reply Reply Quote 0
      • S
        salty_good
        last edited by

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

        Thank you!

        1 Reply Last reply Reply Quote 0
        • S
          salty_good
          last edited by

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

          1 Reply Last reply Reply Quote 0
          • J
            JimW
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • ajb2k3A
              ajb2k3
              last edited by

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

              UIFlow, so easy an adult can learn it!
              If I don't know it, be patient!
              I've ether not learned it or am too drunk to remember it!
              Author of the WIP UIFlow Handbook!
              M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

              1 Reply Last reply Reply Quote 0
              • J
                JimW
                last edited by

                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);

                }

                S 1 Reply Last reply Reply Quote 0
                • S
                  salty_good @JimW
                  last edited by salty_good

                  @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
                  }
                  
                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post