I2C control LED with pushbutton

Hello, I am new to Arduino and I want to control a LED with a pushbutton via I2C. I have a master Arduino and a slave Arduino.

This is de code for the master:

#include <Wire.h>

const int slaveAddress = 1;
const int buttonPin = 2; 
int buttonState = 0; 
boolean last_state = LOW;

void setup()
{
pinMode( buttonPin, INPUT ); 
Wire.begin(); 
Serial.begin( 9600 ); 
}

void loop()
{
delay(100);

buttonState = digitalRead(buttonPin);                                               
  if (buttonState != last_state){                     
    last_state = buttonState;
    Wire.beginTransmission(slaveAddress);
    Wire.write(buttonState);
    Wire.endTransmission();
    
    delay(10000);
  }
}

Slave:

#include <Wire.h> // <-- remove spaces

int LED = 9;

void setup()
{
pinMode (LED, OUTPUT);
Wire.begin(1);
Wire.onReceive(receiveEvent);
}

void loop(){
  delay(100);
}

void receiveEvent( int howMany )
{
int b = Wire.read();
Serial.println(b);

if (b == HIGH) {
digitalWrite(LED, HIGH);
delay(200);
}
if (b == LOW) {
digitalWrite(LED, LOW);
delay(200);
}

}

I printed the value of the pushbutton and it was sending ones if it was pressed. But the LED would not turn on, can someone see what I am doing wrong?

I cannot see why your led does not light. Try loading the blink example sketch to the slave Arduino and set it for pin 9.

Thank you for your response. I uploaded the code and then the LED blinks but it is not controlled by the button in this way. The blink example is not using I2C right? I think something is going wrong with the communication.

There is a 10 second delay between button reads, change it to something more reasonable like 100 mS to act as a denounce.

That helped a bit, it does turn on and off now. But after pressing the button for a couple of times (around 10 times) the LED will not turn on anymore. I have to reupload the code in order for the LED to react to the button. Does someone know why this is the case?

No, using the blink sketch was only to test that there is no problem with your led or the Arduino pin or your wiring. You might say "of course I tested those things, in other ways" but on this forum we get questions from posters with a huge range of experience levels and often they have not thought to test the most basic components and aspects of their circuits.

But what do you see on serial monitor then?

Here is a more important question than any we have asked you so far: why are you doing this? Why are you using 2 Arduino and communicating between them using i²c? If the reason is to increase the number of input/output pins, analog pins, PWM pins etc above what a single Arduino can provide, then we can suggest better solutions than using multiple Arduinos.

One thing I found out was that your switch needs a pull up resistor. It may seem like it is a 1, but is is bouncing all over the place between a 1 and 0. Fortunately the Uno has built in pull up resistors. Try adding this pinMode( buttonPin, INPUT_PULLUP);
It worked for me

Eventually I want to have a 3 by 3 square matrix form connected graph and each node represents an Arduino. All of the Arduino's have to communicate with each other, so I want to use multimaster (which I read that it is quite hard to do with Arduino's). But before I am going to test the multimaster I wanted to test the communication between a slave and a master with a button and a LED. So it is more for test purposes.

I first had this problem, but I added a pull down resistor to one leg of the switch which fixed the problem. But thank you for suggestion!

Hi, @thomnik
Welcome to the forum.
This where quite a few posts would have been saved if you had posted a circuit diagram.

Can you please post one?
A hand drawn schematic would be fine.

How far apart will each node be?
If it is going to be a 3 x 3 matrix, there are easier and less hardware intense solutions.
Can you tell us your application/project?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

That made no sense to me. I was hoping for an answer that made your reasons for doing this more clear, not less!

Have you ever heard of an "X-Y" problem?

Hi Tom, what I want to achieve is a lot different then the topic in this section, is that a problem to post here or should I make a new topic or send you a message?

I have never heard of it but I looked it up and you are right, I am sorry. But thank you for the answers!

So will you share what "problem X" is for you?

Sure, I want to build a system connected in a 3 by 3 matrix form (later expand this to 5 by 5 or even bigger), each node is then an Arduino and every Arduino has to send and receive data. So for example I want to send data from Arduino 1 to the last one in the matrix, Arduino 9. It has to do this in the shortest path available and if a connection between Arduino's is not available the system has to rearrange and it has to take another route to Arduino 9. I already made a simulation in Python and wanted to test my findings with Arduino's, because they are versatile and quite "easy" to program. I thought that I2C was a good option, because I think that every node has to send and receive data. The picture maybe gives a better idea of what I am talking about.

I think that I have to reconsider the communication protocol, if you have any suggestions please let me know.

Ok, bit that's still a solution to a problem, it's not the problem itself. Why are you wanting this matrix of Arduinos, what real-world problem is that a potential solution to? Perhaps it's just a hypothetical problem for teaching purposes.

I would suggest using UART (=serial) rather than i²c. Your image shows Arduino Megas, and they have 3 or 4 hardware UART interfaces each.

Hi,

As they are all on the same bus, you don't have to send a signal to 1 then 2 then 3 to get to 9.
You can send it directly as all our UNOs will be on the same bus.
UNLESS
As your diagram "infers", you have dedicated I2C buses between adjacent UNOs.
If so you had better also see if the UNO can support four independent I2C comm ports.

If this whole project is just an experiment to see if you can do it, fair enough.

But putting all the UNOs on the single I2C bus would be simpler, or a single CanBus.
The CanBus needing some extra hardware.

Tom... :grinning: :+1: :coffee: :australia:

It is indeed for teaching purposes, I want to see if it is possible to do and how it can be done.

There are software I2C libraries, so in theory the Arduinos can support multiple buses right? I don't know how it will work in practice, but we'll see.