Transferring data using wire library

Hi again
The situation is that I have 2 Arduinos (an Arduino mega and an Arduino Uno Wi-Fi), I have been trying to send signals over wire using the wire library between them both however, I am just getting zeros on the other end. Which tells me the message is not getting through. Please could you advise how I can resolve this problem.
I've checked that the pins are set correctly.
Many Thanks

Code for Arduino Mega

#include <Wire.h>
int potPin = 18;
int potVal = 1;
void setup() {
  // Start the I2C Bus as Master
  Wire.begin(); 
  Serial.begin(9600);
  pinMode(potPin, INPUT);  //Optional, pins are listed as inputs by default
}
void loop() {
  Wire.beginTransmission(0); // transmit to device #9
  Wire.write(potVal);
  Serial.print(potVal);              // sends x 
  Wire.endTransmission();    // stop transmitting
  Wire.requestFrom(0, 6);    // request 6 bytes from slave device #8
  while (Wire.available()) { // slave may send less than requested
    char x = Wire.read(); // receive a byte as character
    Serial.print(x);         // print the character
  }
  
  delay(500);
}

Code for Arduino Uno Wi-Fi

#include <Wire.h>
int ledPin = 0;
int x = 0;
void setup() {
  Serial.begin(9600);
  pinMode (ledPin, OUTPUT);
  // Start the I2C Bus as Slave on address 9
  Wire.begin(0); 
  // Attach a function to trigger when something is received.
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}
void requestEvent()
{
  Wire.write("Hello ");
}
void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}
void loop() {
  delay(1000);
Serial.println(x);
int pwm = map(x,0, 1023, 0, 255); 
analogWrite(ledPin, x);
}

(I have been using code from various tutorials)

Really? Address 0 is a reserved address.

  // Start the I2C Bus as Slave on address 9
  Wire.begin(0); 
...
  Wire.beginTransmission(0); // transmit to device #9

Please post a hand drawn wiring diagram, with pins and all connections clearly labeled.

Which board do you have?

  • Arduino Uno Wifi, with a ATmega328P microcontroller.
  • Arduino UNO R4 WiFi, with a Renesas RA4M1 processor.
  • Arduino UNO WiFi Rev2, with a ATmega4809 microcontroller.

Here's my diagram attempt:
It's an Uno WiFi rev2

A ground connection between the two boards is required.

Which "Arduino Uno WiFi"? Triple check which are the SDA/SCL pins.

Are you trying to make a serial or i2c connection. The pin selection is very confusing,

1 Like

I've realised I've got confused between serial and I2c, I think serial would work better. I've found SDA and SCL on the Mega but I can't find them on the Uno WiFi (which is definitely a rev 2)
I'm sorry I'm being a bit stupid here, thanks for the help.

No need to be sorry, we are here to help.

Does the ATmega4809 have just one Serial port ? Are you going to use SoftwareSerial ?

The Arduino Mega has 1 processor, the UNO WiFi Rev2 has 3 processors. So now you have 4 processors talking to each other :scream:
Can you do your project with a single Arduino board, with a single processor ? For example a ESP32.

So my project is a burglar alarm that can be controlled by an web app. I need the Mega because the Uno WiFi doesn't have enough ports for the lcd, keypad, buzzer and motion sensor. I'll admit I don't know how many serial ports the mega has, I didn't know about SoftwareSerial so if you think it would be beneficial then I'll look at using it.

The Mega has multiple hardware serial ports as does the wifi rev2.

There is no need to use software serial in your project.

1 Like

I see. Thanks. The ATmega4809 has four UART ports, and Arduino selected one as a spare hardware port on pin 0 and 1, called "Serial1". The Serial Monitor is via other pins. That's very nice :sunglasses:

So the drawing in post #4 is correct. Add a GND wire, and it works.
Pins for UART/Serial: https://www.arduino.cc/reference/en/language/functions/communication/serial/

thameslinkrail, we prefer to use a single processor that runs all the code. Extra pins can be created with I/O expanders, for example a chip on the I2C bus with programmable input and output pins.
I hope that you use a I2C display.
Then only the keypad needs extra hardware.
Adafruit has a I2C keypad: https://www.adafruit.com/product/1616

1 Like

Thanks for that, I've added the ground and it's working. In terms of I2C, I'll have a look at it but it might be too late as I've been working on this for a long time.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.