Arduino to arduino i2c

Hi guys,

I’ve been researching arduino to arduino i2c using the master as the reader and the slaves as the writers. I got a basic circuit working, but I can’t get the code to work with my circuit.

This is what I have, but it is not working.

I have an OPT101 connected to the arduino analog pins to measure light intensity.
I don’t think there’s anything wrong with the master code. I have problems in my slave code under requestevent. I don’t know how to write the pVolt0 value to the master. I am using the arduino Leonardo

Master:

#include <Wire.h>

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

  Wire.requestFrom(2, 8);
  
  while(Wire.available())
  {
    
    char c = Wire.read();
    Serial.print(c);
  }
}

void loop()
{
}

Slave:

#include <Wire.h>

#define inPin0 0
 
void setup() {
 
  Wire.begin(2);
  Wire.onRequest(requestEvent);
   
}
 
void loop() {

  delay(100);
   
}

void requestEvent()
{  
  int pinRead0 = analogRead(inPin0);
  int pVolt0 = pinRead0 / 1024.0 * 5.0;
  Wire.write((byte)pVolt0);
}

When I set everything up, nothing appears on the serial monitor.
Can anyone help me?
Thank you!

http://forum.arduino.cc/index.php/topic,148850.0.html

7

This is what I have, but it is not working.

In the same way you don't hand your keys to the mechanic and say "my car isn't working", you don't make a post here without describing both what the code is doing, and what you're expecting it to do.

And code tags avoid the smileys in your code and allows helpers to cut and paste to their IDE. You are only sending 1 byte but requesting 8)

The slave device needs an address for the master to communicate with it.

and to write, you need the standard

  Wire.beginTransmission(device_address);
  Wire.write(data);
  Wire.endTransmission();