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'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.
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
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.
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
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
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.