Help Needed Connecting Mega w/ Uno

Hello,

I hope I'm posting this in the correct place as I am new to this forum. I'm having trouble connecting my Mega2560 R3 and Uno R3 to a breadboard. I was told each would have to be connected to a different computer via USB and that is what i have done. However, my code will not execute whenever they are connected to each other, but it will execute whenever i disconnect them. Can someone please tell me what i'm doing wrong? I have included my code and a couple of pictures below.

Thank you
Ryan

Transmitter code:

const int onBoardLedPin = 13;
const int buttonState0 = 0;
const int buttonState1 = 0;
const int buttonState2 = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonState0 == digitalRead(13);
  buttonState1 == digitalRead(12);
  buttonState2 == digitalRead(11);
  if (buttonState0 == HIGH) {
    Serial.write(1);
  }
  else if (buttonState1 == HIGH) {
    Serial.write(2);
  }
  else if (buttonState2 == HIGH) {
    Serial.write(3);
  }
}

Receiver code:

const int gLED = 13;
const int rLED = 14;
int receiver = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(gLED, OUTPUT);
  pinMode(rLED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Ready to receive:");
}

void loop() {
  // put your main code here, to run repeatedly:

  digitalWrite(gLED, LOW);
  digitalWrite(rLED, LOW);

  if(Serial.available() > 0) {
    receiver = Serial.read();

    Serial.println(receiver);
    if (receiver == 1) {
      digitalWrite(gLED, HIGH);
      digitalWrite(rLED, LOW);
    }
    if (receiver == 2) {
      digitalWrite(rLED, HIGH);
      digitalWrite(gLED, LOW);
    }
    if (receiver == 3) {
      digitalWrite(gLED, HIGH);
      digitalWrite(rLED, HIGH);
    }
  }
}

elliott21:
I was told each would have to be connected to a different computer via USB and that is what i have done. However, my code will not execute whenever they are connected to each other,

That suggests that the problem is the way you have them connected and you have not told us that.

Make a simple pencil drawing showing all the connections and post a photo of the drawing. See this Simple Image Guide

...R

Hi, thank you for your reply. I think I figured out how to post a photo right before you sent me the "how to" guide. Hopefully it works.

Hi,
What is your project supposed to do?
You have press button input to the UNO and the MEGA comm TO the UNO.
330R resistor puul down is way too low, use 10K.

How do you know your code is working with both PC's disconnected?

Thanks.. Tom.. :slight_smile:

Hi Tom,

I guess i don't know for certain that my code is working when the mega and uno are disconnected. I just know that whenever they are connected as shown, my Receiver (the Uno connected to macbook) will not receive the code and i get an error "the programmer is not responding". However my Transmitter (Mega connected to pc) will accept my code but can't transmit it of course because the Receiver will not accept it. When i disconnect the mega from the uno, each takes the code when i run it so something must be wrong with the way i'm hooking it up. However, i'm following the instructions to the letter.

My project is described as a "simple transmitter-receiver module in which two micro-controllers exchange 1-byte messages." It should dynamically display LED patterns such that the LED flash patterns are enjoyable to watch. Each button will cause one of three patterns to run in an infinite loop as long as the button is pressed.

Thank you,
Ryan

elliott21:
I think I figured out how to post a photo right before you sent me the "how to" guide. Hopefully it works.

Not quite. You did not make your images visible.

...R

This has got very confusing because you added an image to your Original Post after I replied to it. For the future, please don't add stuff to older posts.

elliott21:
I just know that whenever they are connected as shown, my Receiver (the Uno connected to macbook) will not receive the code and i get an error "the programmer is not responding". However my Transmitter (Mega connected to pc) will accept my code but can't transmit it of course because the Receiver will not accept it. When i disconnect the mega from the uno, each takes the code when i run it so something must be wrong with the way i'm hooking it up. However, i'm following the instructions to the letter.

I think you are confused between the business of uploading programs and the business of communicating between the Mega and the Uno.

On both boards the Tx0 and Rx0 pins are used for uploading code and if you have anything connected to them then the upload process will probably not work.

As the Mega has 3 spare HardwareSerial ports you don't need to use Tx0 annd Rx0 for communication with the Uno.

Things are a little more complex on the Uno because it has only one HardwareSerial port and it is best to keep that free for communication with the PC. You can use SoftwareSerial to create another serial port on the Uno on two other pins for communication with the Mega. Just be aware that SoftwareSerial cannot work at high speed - start with 9600 baud.

...R
Serial Input Basics - simple reliable ways to receive data.

Okay sorry about that, i won't add anything to an older post again. Okay, based on what you were saying i think i certainly was confused about what was accomplishing which task at a particular time. I will read through the link that you sent later this morning as it is almost 4AM in Dallas and i must get a little sleep. Here is hopefully a photo that is more visible.

Thank you
Ryan

Please make your images visible by following the instructions in the link I have given you - so that I don't have to do it.

...R

And, please note that photos of hardware and wiring are not useful - it is too easy to misinterpret the connections

The type of diagram that you added to your Original Post is much clearer.

...R

Okay that makes sense. I’ll only post images of the diagrams from now on. I’ll also make sure and post the images correctly so no one else has to fix them.

So you’re saying that TX0 and RX0 are simply used to upload programs and you aren’t actually supposed to connect anything to them?

Also, the project called for the use of two TI Launchpads; however, I am unable to get the Launchpads to work with my MacBook, something to do with the fact that it only has USB-C ports is what the teaching assistant told me. So that is why I’m using Arduino products instead. All of the instructions for the project are given as if you are using a Launchpad, but as long as I’m still connecting it to the equivalent pins on the Arduinos this should not make a difference, correct?

Thank you,
Ryan

elliott21:
So you’re saying that TX0 and RX0 are simply used to upload programs and you aren’t actually supposed to connect anything to them?

At your present level of expertise it makes things simpler if you don't connect anything to them.

Also, the project called for the use of two TI Launchpads; however, I am unable to get the Launchpads to work with my MacBook,

What is a TI Launchpad?

...R

It's just a different brand of micro-controller.

I've taken your suggestion and found posts on using SoftwareSerial. Based on what you've said, I won't need to use SoftwareSerial on the mega because it has other serial ports i can use, correct?

Thank you
Ryan

elliott21:
Based on what you've said, I won't need to use SoftwareSerial on the mega because it has other serial ports i can use, correct?

Yes

...R

something to do with the fact that it only has USB-C ports

You can get an adaptor to convert the USB-C to the normal USB.

You can get an adaptor to convert the USB-C to the normal USB.

Yes, i have a couple different USB to USB-C adapters and i've tried both but neither of them allow me to connect the TI Launchpad to my macbook. That's okay, because I like Arduino boards better anyways; although, i'm probably much too inexperienced to even be able to say that. It has made completing my project much more difficult though.

Can someone please help me interpret this, found in the Arduino Reference section:

The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your device’s TX pin, and the ground of your Mega to your device’s ground.

Particularly, this part:

To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor.

What does it mean an additional USB to serial adaptor? I already have two adaptors, one connected from the UNO to my macbook, and one connected from the Mega to my PC. I think i'm misinterpreting this because how would i be able to have two USB adaptors on the Mega?

Thank you,
Ryan

You can buy a USB-TTL cable (aka FTDI cable) that plugs into a USB socket on the PC and appears on the PC as a serial port. The other end of the cable has Rx, Tx and GND connections that can connect to the Tx and Rx pins on the Mega.

Be aware that you can also get USB-RS232 cables that provide a similar function but RS232 uses voltage levels that don't work with an Arduino and might damage it.

You can also get a USB-TTL module that does not have any cable, but IMHO the cables are more convenient.

...R

You can buy a USB-TTL cable (aka FTDI cable) that plugs into a USB socket on the PC and appears on the PC as a serial port. The other end of the cable has Rx, Tx and GND connections that can connect to the Tx and Rx pins on the Mega.

Okay thank you.

In regards to your comment:

You can use SoftwareSerial to create another serial port on the Uno on two other pins for communication with the Mega.

If i did do this, would that allow me to have both boards connected via usb to their respective computers, and have them connected to each other during the upload process?

Thank you,
Ryan

elliott21:
In regards to your comment:
If i did do this, would that allow me to have both boards connected via usb to their respective computers, and have them connected to each other during the upload process?

Yes.

And, in case of doubt, there would be no need for a USB-TTL cable for that purpose.

...R

Yes.

And, in case of doubt, there would be no need for a USB-TTL cable for that purpose.

Okay, thank you I figured that but it's nice to know for sure. I ordered one anyways because I figured it might be a good thing to have.

I have to demonstrate this project to the teaching assistant on Wednesday and I'd rather not have to haul two laptops around with me. That being said, would I be able to do this using only one computer, while of course connecting them to different USB ports?

Thank you,
Ryan