How to use Servo motor in i2c

may i know the correct solution on my programing.
if i use master to send the data to slave, and slave have the servo motor to run the the degree position,
because the problem is, the motor run to the value has been decided(like 20 or 50), but after the servo motor move,
the small vibration come from inside servo,
thank you :slight_smile:

master:-

#include <Wire.h>

#define THIS_ADDRESS 1
#define OTHER_ADDRESS 2


void setup() {

  Wire.begin(THIS_ADDRESS);
  
}

void loop() {
    
    Wire.beginTransmission(OTHER_ADDRESS);
    Wire.send(1);
    Wire.endTransmission();
  }

slave:-

#include <Wire.h>

//Include Servo Library
#include <Servo.h>

//Define Pins
int servoPin = 9;

//Create Servo Object
Servo kazServo;

#define LED 13
#define BUTTON 10

#define THIS_ADDRESS 2
#define OTHER_ADDRESS 1

boolean last_state = HIGH;

void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  
  pinMode(BUTTON, INPUT);
  digitalWrite(BUTTON, HIGH);
  
  Wire.begin(THIS_ADDRESS);
  Wire.onReceive(receiveEvent);
  
//Attaches the Servo to our object
  kazServo.attach(servoPin);
}

void loop() {
  
    
    
}

void receiveEvent(int howMany){
  while (Wire.available() > 0){
    boolean b = Wire.receive();
    Serial.print(b, DEC);
    digitalWrite(LED, b);
    kazServo.write(20);
    delay(1000);
  }
  Serial.println();
    
     
}
    Wire.beginTransmission(OTHER_ADDRESS);
    Wire.send(1);
    Wire.endTransmission();

Send a hardcoded value...

  while (Wire.available() > 0){
    boolean b = Wire.receive();
    Serial.print(b, DEC);
    digitalWrite(LED, b);
    kazServo.write(20);
    delay(1000);
  }

Receive and ignore a value. Move the servo to a fixed position.

Please explain how this is a networking issue. Sounds more like a hardware problem. Most likely under-powering the servo. But, you haven't explained a thing about the servo, so the only thing we can say is that it is not a networking issue.

but when i use i2c, after the rotation, the servo have a small vibration inside the motor, it is a normal?

What you should be doing in your callback function is to read the data and set a flag.

In loop, you should be checking the flag, moving the servo if the flag is set, and clearing the flag. That gets rid of the delay().

I2C uses interrupts. The servo library uses interrupts. Mixing delay() and interrupts is rarely a good idea.