Using the M5Stack Servo module to control a 360° servo

I am trying to use the M5Stack Servo module to control a 360° servo. The problem is that as soon as the external power supply is connected the servo rotates in a clockwise direction at a steady speed. I have been able to get the servo to rotate anti-clockwise at a slower speed for a period (3 seconds in the example) by adapting the SERVO sketch as below. However the servo always returns to the constant clockwise speed for the 1 second, whatever angle is used in the first function call. My understandin is that 90° (or 1500 us) should be stop and above 90° clockwise and below 90° anticlockwise and the further from 90° the faster the rotation.

Could someone show me a simple skech to make a 360° servo spin clockwise and then anticlockwise using the M5Stack Servo Module?

/*
    Description: Use SERVO Module to control the rotation of 12-channel servo.
*/
#include <Arduino.h>
#include <M5Stack.h>
#include <Wire.h>


#define SERVO_ADDR 0x53
void setup() {
    M5.begin(true, false, true);
    M5.Power.begin();
    M5.Lcd.setTextFont(4);
    M5.Lcd.setCursor(70, 100);
    M5.Lcd.print("Servo Example");


    Wire.begin(21, 22, 100000);
    // put your setup code here, to run once:
   
}


// addr 0x01 mean control the number 1 servo by us
void Servo_write_us(uint8_t number, uint16_t us) {
    Wire.beginTransmission(SERVO_ADDR);
    Wire.write(0x00 | number);
    Wire.write(us & 0x00ff);
    Wire.write(us >> 8 & 0x00ff);
    Wire.endTransmission();
}


// addr 0x11 mean control the number 1 servo by angle
void Servo_write_angle(uint8_t number, uint8_t angle) {
    Wire.beginTransmission(SERVO_ADDR);
    Wire.write(0x10 | number);
    Wire.write(angle);
    Wire.endTransmission();
}


void loop() {
  //Servo_write_us(0, 1200);
    for(uint8_t i = 0; i < 12; i++){
        //Servo_write_us(i, 1000);
        Servo_write_angle(i, 100);
    }
    delay(1000);
    for(uint8_t i = 0; i < 12; i++){
        //Servo_write_us(i, 2000);
        Servo_write_angle(i, 180);
    }
    delay(3000);
}

I can get the servo to turn clockwise and counterclockwise using this sketch:

#include <Arduino.h>
#include <M5Stack.h>
#include <Wire.h>


#define SERVO_ADDR 0x53
void setup() {
  M5.begin(true, false, true);
  M5.Power.begin();
  M5.Lcd.setTextFont(4);
  M5.Lcd.setCursor(70, 100);
  M5.Lcd.print("Servo Buttons");


  Wire.begin(21, 22, 100000);
  // put your setup code here, to run once:


}


// addr 0x01 mean control the number 1 servo by us
void Servo_write_us(uint8_t number, uint16_t us) {
  Wire.beginTransmission(SERVO_ADDR);
  Wire.write(0x00 | number);
  Wire.write(us & 0x00ff);
  Wire.write(us >> 8 & 0x00ff);
  Wire.endTransmission();
}


// addr 0x11 mean control the number 1 servo by angle
void Servo_write_angle(uint8_t number, uint8_t angle) {
  Wire.beginTransmission(SERVO_ADDR);
  Wire.write(0x10 | number);
  Wire.write(angle);
  Wire.endTransmission();
}


void loop() {
  // update button state
  M5.update();


  // if you want to use Releasefor("was released for"), use .wasReleasefor(int time) below
  if (M5.BtnA.wasReleased() || M5.BtnA.pressedFor(1000, 200)) {
    M5.Lcd.clear(BLACK);
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.print("Anticlockwise");
    Servo_write_angle(0, 160);
  }
 else if (M5.BtnB.wasReleased() || M5.BtnB.pressedFor(1000, 200)) {
  M5.Lcd.clear(BLACK);
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.print("Stop");
    Servo_write_angle(0, 165);
  }
  else if (M5.BtnC.wasReleased() || M5.BtnC.pressedFor(1000, 200)) {
  M5.Lcd.clear(BLACK);
    M5.Lcd.setCursor(0, 0);
    M5.Lcd.print("Clockwise");
    Servo_write_angle(0, 170);
  }
}

Why 160° and 170° work is a mystery to me, I found them by trial and error. I cannot find a value to work for stop. The servo doesn't go smoothly in either direction, it stutters a lot at first with the 160° beforeturning properly. Something is not right, I'm hoping someone can help.

After some experimentation I have found that calling the funtion with

Servo_write_angle(0, 159);

Causes the servo to stop dead.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.