10

I'm attempting an I²C setup in which one master Arduino controls two slaves.

Enter image description here

I'm using two 2000 ohm pullup resistors for the I²C and all boards are Arduino Duemilanoves. Both the I²C setup and the control setup function properly when disconnected from each other, but when connected the Arduinos become unresponsive as soon as the first wire.write function is called. I cease to receive serial messages, the slave Arduinos stop receiving messages from the master, and I loose the ability to turn the system on and off using the button.

I've tried adding short delays after each wire.write function and have used a variety of pullup resistors in an attempt to fix the problem, but nothing seems to work. How can I fix this?

Master Code:

#include <Wire.h>

int potPin1 = 0;    // Select the input pin for the potentiometer
int potPin2 = 1;
int potVal1;       // Variable to store the value coming from the sensor
int potVal2;
int times=0;   // All the below variables are used to control an on-off button
int state=0;
int lastState=0;
boolean pot=false;

void setup()
{
    pinMode(13, OUTPUT);  //LED that turns on when system is activated
    pinMode(3, INPUT);    //Button that turns on system
    Serial.begin(9600);
    Wire.begin();
}

void loop(){
    state=digitalRead(3);
    if(state != lastState){
        if(state==HIGH){
            times++;
            Serial.println(times);
        }
        else{
            Serial.println("off");
        }
    }

    lastState=state;

    if(times%2 ==1)
    {
        turnPotOn();
    }
    else
    {
        turnPotOff();
    }

//All code in the loop up to this point turns the system on and off at button press. //The following code corresponds to I²C based upon potentiometer readings.

    if(pot==true)
    {
        potVal1 = analogRead(potPin1);    // Read the value from the sensor
        potVal2 = analogRead(potPin2);

        if((potVal1>700) && (300<potVal2) && (potVal2<700))
        {
            arduino1_motor1();
        }
        else if ((potVal1<330) && (336<potVal2) && (potVal2<683))
        {
            arduino1_motor2();
        }
        else if ((potVal2>683) && (330<potVal1) && (potVal1<640))
        {
            arduino2_motor3();
        }
        else if ((potVal2<336) && (330<potVal1) && (potVal1<640))
        {
            arduino2_motor4();
        }
        else if ((potVal2<336) && (potVal1<330))
        {
            arduino12_motor24();
        }
        else if ((potVal2>683) && (potVal1>640))
        {
            arduino12_motor23();
        }
        else if ((potVal2>683) && (potVal1<640))
        {
            arduino11_motor23();
        }
        else if ((potVal2<336) && (potVal1>330))
        {
            arduino11_motor24();
        }
        else
        {
            arduino12_still();
        }
    }
    else
    {
        // arduino1_still();
        // arduino2_still();
        Serial.println("OFF");
    }
}

void turnPotOff()
{
    digitalWrite(13, LOW);
    pot=false;
}

void turnPotOn()
{
    digitalWrite(13, HIGH);
    pot=true;
}

void arduino1_motor1()
{
    Wire.beginTransmission(5);
    Wire.write('A');
    Wire.endTransmission();
    arduino2_still();
    Serial.println("A1 in M1 d");
}

void arduino1_motor2()
{
    Wire.beginTransmission(5);
    Wire.write('B');
    Wire.endTransmission();
    arduino2_still();
    Serial.println("A1 in m2 d");
}

void arduino12_still()
{
    arduino1_still();
    arduino2_still();
    Serial.println("A1 & A2 stl");
}

void arduino2_motor3()
{
    arduino1_still();
    Wire.beginTransmission(10);
    Wire.write('M3');
    Wire.endTransmission();
    Serial.println("A2 in M3 d");
}

void arduino2_motor4()
{
    arduino1_still();
    Wire.beginTransmission(10);
    Wire.write('D');
    Wire.endTransmission();
    Serial.println("A2 in M4 d");
}

void arduino12_motor24()
{
    Wire.beginTransmission(5);
    Wire.write('B');
    Wire.endTransmission();
    Wire.beginTransmission(10);
    Wire.write('D');
    Wire.endTransmission();
    Serial.println("A1 & A2 in M2 and M4 d");
}

void arduino12_motor23()
{
    Wire.beginTransmission(5);
    Wire.write('B');
    Wire.endTransmission();
    Wire.beginTransmission(10);
    Wire.write('C');
    Wire.endTransmission();
    Serial.println("A1 & A2 in M2 and M3 d");
}

void arduino11_motor24()
{
    Wire.beginTransmission(5);
    Wire.write('A');
    Wire.endTransmission();
    Wire.beginTransmission(10);
    Wire.write('D');
    Wire.endTransmission();
    Serial.println("A1 & A2 in M1 and M4 d");
}

void arduino11_motor23()
{
    Wire.beginTransmission(5);
    Wire.write('A');
    Wire.endTransmission();
    Wire.beginTransmission(5);
    Wire.write('C');
    Wire.endTransmission();
    Serial.println("A1 & A2 in M1 and M3 d");
}

void arduino1_still()
{
    Wire.beginTransmission(5);
    Wire.write('S');
    Wire.endTransmission();
}

void arduino2_still()
{
    Wire.beginTransmission(10);
    Wire.write('S');
    Wire.endTransmission();
}

Slave 1 Code:

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

Servo myservo1;
Servo myservo2;

void setup()
{
    Serial.begin(9600);          //  setup serial
    myservo1.attach(2);
    myservo2.attach(3);
    Wire.begin(5);
    Wire.onReceive(receiveEvent);


}

void loop()
{
}

void receiveEvent(int howMany)
{
  while(Wire.available())
  {
    char v = Wire.read();

    if(v == 'A')
    {
      myservo1.write(0);
      myservo2.write(180);
      Serial.println("Arduino 1 in motor 1 direction");
    }
    else if(v == 'B')
    {
      myservo1.write(180);
      myservo2.write(0);
      Serial.println("Arduino 1 in motor 2 direction");
    }
    else
    {
      myservo1.write(90);
      myservo2.write(85);
      Serial.println("Arduino 1 still");
    }
  }
}

Slave 2:

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

Servo myservo3;
Servo myservo4;

void setup()
{
    Serial.begin(9600);         // Setup serial
    myservo3.attach(2);
    myservo4.attach(3);
    Wire.begin(10);
    Wire.onReceive(receiveEvent);
}

void loop()
{
}

void receiveEvent(int howMany)
{
    while(Wire.available())
    {
        char v = Wire.read();

        if(v == 'C')
        {
            myservo3.write(0);
            myservo4.write(180);
            Serial.println("Arduino 2 in motor 3 direction");
        }
        else if(v == 'D')
        {
            myservo3.write(180);
            myservo4.write(0);
            Serial.println("Arduino 2 in motor 4 direction");
        }
        else
        {
            myservo3.write(90);
            myservo4.write(90);
            Serial.println("Arduino 2 still");
        }
    }
}
4
  • Do you have access to an oscilloscope to determine e signal integrity and rule out hardware issues?
    – cowboydan
    Nov 3, 2012 at 19:04
  • I haven't checked my setup with an oscilloscope, but I have tested the i2c setup without the additional components (potentiometers, LED, buttons, etc.) and the additional components without the i2c and found both systems to be working individually.
    – pongAssist
    Nov 4, 2012 at 0:30
  • Can you say where the I2C slave addresses are set? Are they set in hardware? I'd try removing one of the arduino slaves and try to get just one to respond.
    – cowboydan
    Nov 4, 2012 at 11:44
  • I removed one of the slaves and ran a version of the master code that can be found here: Master Code With One Slave I used the same slave 1 code as is located above The problem with the system was the same, it was functional when wire.write functions were removed, and non-functional when they were included. Could the problem be the lengths of wire I'm using or the strength of my resistors?
    – pongAssist
    Nov 7, 2012 at 14:55

4 Answers 4

4

the "receive event" is inside an ISR, which doesn't exit until the event function returns

oh by the way, the AVR hardware holds the I2C bus in a frozen state until this event exits, also known as clock stretching

oh guess what, Wire didn't declare the FIFO variables as volatile, so even though he has while (Wire.available()), that becomes an infinite loop because available() will never change, because it's all happening in an interrupt and rxBufferIndex and rxBufferLength are not declared as volatile

http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_volatile

this is one possible cause

insert angry rant about Arduino and its terrible libraries here

solution? use another library, the TWI utilities that "Wire.h" borrows can be used directly if you know how, and it's great, I use it all the time.

1

This might help you out a bit:

Okay so this is going to be a wall of text and I already closed the tab once on accident before posting so I might sound angry, because this "feature" of the Arduino is really bad and it will cost you $$ if you plug a 3.3v device into it. You're using the Wire Library which enables really bad 20k internal pullups on the SDA and SCL pins.

ON EACH ARDUINO. You have a situation where the total pullup resistance is now borked(screwed up). You need to do research on how to disable the internal pullups. I recommend modifying the library. For holistic reasons this sort of control should never be done in software. Double check his math on the required external pullup. The capacitance of each SCL/SDA pin on the Arduino should be 10pF.

Here is the formula direct from the ATMEGA datasheet

https://i.stack.imgur.com/YAZds.png

Here is the ATMEGA datasheet, see section 21 for the I2C section

http://www.atmel.com/Images/doc8161.pdf

1
  • I disagree with this. There is no factual evidence to support any claims.
    – Zimano
    Aug 21, 2018 at 14:40
0

Instead of trying with multiple slave , First Initiate with single master and single slave configuration. And try to keep program as simple as possible. You create function like receive and sending to master/slave

0

You cannot write inside receiveEvent

receiveEvent defines the command then the master should call requestFrom and ask for value and the slave handles (requestEvent) and use write there.

i.e. 1- Master wire.Write (Command Name)

2- slave: handles (receiveEvent) and get the command ID

3- Master sends requestFrom

4- slave handles (requestEvent) and based on the command ID sends the reply to master.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.