I2C Comm Problem

Hi all!

I am trying to make a complicated I2C project where it involves changing a loop of the LED's by using a sound sensor. But for me to be able to do that, I should do a simpler project first.

I tried to create a simple sound sensitive lights but it seems that the data isn't being passed to the Slave board.

If there is an error on my code, please let me know. Thank you!

Here is the Master's code:

#include <Wire.h>

int DA = A0; // Pin for Analog Output
int sensorvalue = 0;

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  
  sensorvalue = analogRead(DA);
  byte MasterSend = map(sensorvalue,0,1023,0,127);
  Serial.print("Analog: ");
  Serial.println(sensorvalue);
  Wire.beginTransmission(8);
  Wire.write(MasterSend);
  Wire.endTransmission();
}

And here's the Slave's code:

#include <Wire.h>

byte SlaveReceived = 0;
int threshold = 20 ;

void setup() {
  Wire.begin(8);
  Wire.onReceive(receiveEvent);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

}

void loop() {
    if (SlaveReceived >= threshold) {
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
 
  }
  else {
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
  }
}

void receiveEvent() {
  Wire.read();
  }

Does the masterWriter example work? That is known good code.

Are there external pullups on the SDA and SCL lines? The internal pullups may be too weak.

Put serial prints in your code to see what, if anything, is sent and received.

groundFungus:
Does the masterWriter example work? That is known good code.

Are there external pullups on the SDA and SCL lines? The internal pullups may be too weak.

Put serial prints in your code to see what, if anything, is sent and received.

I have placed serial prints, there aren't any data being received, but data are being sent. What could be done for this? :frowning:

Does the masterWriter example work? That is known good code.

void receiveEvent() {
  Wire.read();
  }

You do not store the value that is read so it is lost.

if (SlaveReceived >= threshold) {

SlaveReceived is never updated so it will always be 0.

My advice is to get the master_writer and slave_receiver examples working and build off of that.

I tried the masterWriter code, but it doesn't work on my end. I am wondering. :astonished:

groundFungus:

void receiveEvent() {

Wire.read();
  }



You do not store the value that is read so it is lost.



if (SlaveReceived >= threshold) {



SlaveReceived is never updated so it will always be 0.

My advice is to get the master_writer and slave_receiver examples working and build off of that.

Hi, I am already receiving data. However, I am not sure how to manage the data being received. The bytes sent by the master are just 1 and 255.

The bytes sent by the master are just 1 and 255.

How do you know that? Post the sender and receiver code that gives that output so that I can try it.

I have 2 Unos connected by I2C. Connected SDA to SDA and SCL to SCL with no external pullups. The master_reader/slave_receiver sketches work just fine in my setup. I think that you would be better off getting known good code to work before writing your own.