A newbie here. I am thinking of interfacing Raspberry Pi with Arduino via I2c to control a few servo motor (as RPi have limited PWM pins). Meaning I would send input from RPi to Arduino to allow to the Arduino to control the servo.

The problem is this: When I was testing the I2C communication alone, it worked fine. The RPI can send a char, and the Arduino Uno can receive and recognize it.

However, when I added in the function to get the Arduino to send PWM signals to the servo, then the program becomes wonky (e.g. when I send over "0x1", the Arduino led should light up, when I send "0x2", the led should turn off. However, once I tried to PWM the servo, it flickers a while and does no light up when I send the signals, and the servo just vibrate a bit, instead of turning to intended position).

And the funny thing is, the code runs (meaning the LED turns on/off accordingly) as long as I do not plug in the servo's signal pin to the Arduino.

What am I missing? Why does the PWM seems to interfere with the I2C, and is there a way to get around this?

My code is below, do pardon me if anything is unclear, I will be more than happy to clarify.

Thanks guys!

#include <Wire.h>
#include <Servo.h>

Servo myservo_1; // create servo object to control lift servo
Servo myservo_2; // create servo object to control entry servo
Servo myservo_3; // create servo object to control exit servo
// twelve servo objects can be created on most boards

int statusLED = 0;
int statusServo = 0;
int pos;

const int ledPin = 13; // onboard LED
const int servo_1 = 3;
const int servo_2 = 5;
const int servo_3 = 9;

static_assert(LOW == 0, "Expecting LOW to be 0");

void setup() {
  Wire.begin(0x8);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent);   // register event
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);     // turn LED off

  myservo_1.attach(servo_1);  // attaches the servo on pin 3 to the servo object
  myservo_2.attach(servo_2);  // attaches the servo on pin 5 to the servo object
  myservo_3.attach(servo_3);  // attaches the servo on pin 9 to the servo object


void loop() {

  if (statusServo == 1){

  else if (statusServo == 2){

  else {


// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {

  int c;

  while (Wire.available()) { // loop through all but the last
    c = Wire.read(); // receive byte as a character 


    if (c == 0x1){
          digitalWrite(ledPin, HIGH); // turn it on
          statusServo = 1;


    else if (c == 0x2){
          digitalWrite(ledPin, LOW); // turn it on
          statusServo = 2;

  • 3
    current draw? underpowered? – Juraj Jul 12 '19 at 9:22
  • Make sure, that the servo gets enough power. Also don't power it through the arduino. Instead connect the power source directly to the servo – chrisl Jul 12 '19 at 16:41
  • Hi. I am powering the servo using 4x AA Battery (6V), so it doubt its a power issue. Also, according to my code, the interrupt function should turn the LED on/off cleanly, depending on the byte sent. However, the LED flickers instead when I send a single byte, so I am quite perplexed. – user58068 Jul 13 '19 at 13:28
  • I see you have a Serial print inside the ISR, thats currently commented out. Have you confirmed, that you are receiving the correct data? The symptoms really look like a power issue. Servos can draw much power. Is the Arduino powered from another source (for example USB)? If it's also connected to the same battery pack: how is it connected? Vin normally needs a nit more than 6V max to correctly output 5V – chrisl Jul 13 '19 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.