I2C Issues

All GND connections are good,

Im using 4.7 kilo ohm resistors for pull ups connected to the 5V pin.

I dont know about voltage drops, so I removed all the motors and left only one to check.....

And I think the circuitry is ok, because when I change the first byte that is transmitted the motor changes its speed accordingly

Just out of curiosity, what happens if you pass Wire.write() the correct data type?

Wire.write((byte)2);

Still no change....

Now that you are sure the master sends the right data, next thing to check is that slave receives it ok.
You could try to put a Serial.print(data) just before the "switch " instruction in the slave sketch , then you'll see
what is actually received.

UPDATE*

I removed a Lilypad that was connected as another slave but was powered through the 5V pin of my Arduino Uno which i was using as the slave device and now the Uno works perfectly! No other changes!

Any idea why this happened? Because I do need the LilyPad to work as well!

Because I do need the LilyPad to work as well!

Then get it it’s own power supply!

Ok, I thought it was possible to power it through the 5V pin for the Uno, since I saw it on one of the threads.

I'm glad that is working.

Using the 5V pin for the Lilypad is possible. The Lilypad runs only at 8MHz, it doesn't need a lot of current.

But if the Arduino Uno is powered with USB, the 5V might be too weak.
Using a power supply of 7V or 8V for the Uno and having the Lilypad powered with the 5V pin should work.

Yeah I am powering the Uno through a 9V battery and connected its 5V pin to the + pin on the LilyPad, by the way Erdin thanks for your advice, could you send me your name I would like to acknowledge your help in my project report

Yeah I am powering the Uno through a 9V battery

What a waste of effort - this whole thread about getting I2C to work, when your REAL problem is a useless power source. Get a real battery.

What battery do you suggest?

PaulS:

  Wire.write(2);              // sends one byte

This comment is right, isn't it? It sends one byte, whose value is 2. Doesn't it?

Oh yeah, I must have been tired when I wrote that. Still, the comment doesn't add much.

PaulS:

Yeah I am powering the Uno through a 9V battery

What a waste of effort - this whole thread about getting I2C to work, when your REAL problem is a useless power source. Get a real battery.

I wouldn't say it was a total waste of effort. The OP now seems to understand more about I2C now, or more specifically more about how the Wire library functions. (True, that didn't lead to finding the cause of this problem, but this was a learning experience in it's own right.)

Something not working evolving into something that is working, is not a waste.

nishant_91, I read this thread once more to see if getting results could be faster and I noticed that I wrote about buffers in two different ways.
Normally when a number of bytes is received, those bytes are often stored in a buffer (I'm testing that myself at the moment).
You have only one byte, so you don't need a buffer. You store the incoming byte into a variable to remember it so to speak, or one could say, it is 'buffered'.
The Wire library does use 5 internal buffers, but you can forget that.

I hope this makes sense.

Yeah the buffers now seem to make sense, the whole program runs very well with the Uno now!
Many thanks Erdin!

Just trying to figure out how to power my LilyPad, I mean it has no JST connectors so no LiPo batteries.

And it dosent run through my Uno. Will running the LilyPad with 2xAA batteries be abetter idea? Or should I power my Uno with 4xAA batteries and then try to power the LilyPad through the 5V pin?

Because I am based in Singapore and Sparkfun products aren't easy to get here, it can take almost a week to get the LilyPad power supply from local retailers since it is currently not in stock.

Communication between two devices with different voltages is a problem.

The Lilypad can be powered with two AA batteries, but the pull-up resistors of 4k7 will start pushing current into the SDA and SCL pin via the microcontroller into the batteries. That is not safe.

I would suggest to keep them all running at 5V.
Try a few different power supplies.
If you have an old USB cable, you could use the 5V to power the Lilypad from another USB connector.
Use a multimeter, or ask someone who has a multimeter to measure the voltages.

If the voltage for the Uno is high (9V or 12V), the voltage regulator could get hot, and it will lower the current to prevent overheating. Rule of thumb: if you can not keep your finger on the voltage regulator it is too hot.

Yeah I will try to experiment and see what happens, since the LilyPad only has an input range of 2.7V to 5.5V, 2 or 3 AA batteries will be less and 4 will be too high.....

Will reducing the I2C voltage to 3.3V help?

It might help, but it might introduce other problems.

If you want to mix different I2C voltages, you should use a I2C level shifter.

One side of the level shifter could be the 5V I2C level, and the other side could be 2V to 5V I2C level.
The I2C signal is going in both directions through the level shifter and the level is adapted for each side.

In my opinion, using everything at 5V is still the best option.

Yup I'll probably try a couple of different batteries before trying the level shifter