I am not really 100% too sure how to do this but I am trying to make my arduino UNO communicate with my arduino MEGA. The reason why I needed to do this was because I was using the RFID module along with a TFT display and the problem I faced is that on the arduino mega they both needed pin 53. Luckily I have an arduino UNO so I just went for that instead for the display. I am trying to use the analogWrite()
function to send analog signals from my MEGA over to the UNO so that the TFT display on the UNO can display the results. The UNO would also read the analog inputs coming from the MEGA. This did not work very well. The analog inputs are being sent from the MEGA but the UNO is not reciving them. I am using a PWN pin on the MEGA to send the analog output to the UNO.
This diagram shows what I mean:
These are the 2 compotents I am using:
https://i.sstatic.net/CqXdB.png TFT display
RFID
This shows how the libary sets out the pins. I am only a beginner so I personally don't want to mess around with the libary.
2 Answers 2
You can use the I2C interface.
TX:
// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
}
byte x = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
x++;
delay(500);
}
RX:
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
while (1 < Wire.available()) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
Schematic:
Image by Dronebot Workshop; check out their tutorial for more info: https://dronebotworkshop.com/i2c-arduino-arduino/
-
This is perfect! Thank you so much! :)The_Indestructible_Cat– The_Indestructible_Cat2020年07月01日 08:26:01 +00:00Commented Jul 1, 2020 at 8:26
-
You are welcome!DragonflyRobotics– DragonflyRobotics2020年07月01日 16:07:11 +00:00Commented Jul 1, 2020 at 16:07
-
What is the address 8 part?The_Indestructible_Cat– The_Indestructible_Cat2020年07月02日 11:14:23 +00:00Commented Jul 2, 2020 at 11:14
-
That is just any address both the boards communicate on. It can be any number but it has to be the same on both sides.DragonflyRobotics– DragonflyRobotics2020年07月02日 15:21:58 +00:00Commented Jul 2, 2020 at 15:21
-
Isn't the RX pin pin 0 and the TX pin is pin 1 thought or vice versa?The_Indestructible_Cat– The_Indestructible_Cat2020年07月02日 20:45:12 +00:00Commented Jul 2, 2020 at 20:45
Pin 53 is just the default "slave select" pin. There is nothing special about it. You can use any other pin as slave select, as long as the libraries for your devices allow it (check the documentation and tutorials for those libraries).
You cannot transmit information with analogWrite()
since it isn't really analogue (it's PWM), and analogue doesn't have enough definition for reliably representing more than a handful of discrete values anyway.
-
I realize I'm later here, but: As an AVR SPI master, you cannot allow your SS pin to go low as input. So if you chose something other than 53 (or SS on an AVR generally) to be your slave select line, you'd still want to set 53 to be OUTPUT, or INPUT_PULLUP, or INPUT and pulled up externally. Otherwise you may be taken out of master mode by noise on the pin. Not that it goes against your main point, but here is still one thing to be aware of that is special about 53/SS with regard to SPI master.timemage– timemage2020年11月21日 02:01:59 +00:00Commented Nov 21, 2020 at 2:01
Explore related questions
See similar questions with these tags.
#define SS_PIN ...
in them where you can change the pin that you want to use instead of 53. The constructor hasMFRC522 mfrc522(SS_PIN, RST_PIN)
where you can put any pin numbers you like (or just redefine the macros to be something else).