Atmega328 to Uno (or other Atmega328) communication through rx tx

I have been trying to find a way to connect an Atmega328 with Arduino Uno or another Atmega328 in order to send data from one to the other and vice-versa. Unfortunately I have not found anything so if anyone has any idea how to do this, I 'd be grateful !! Also how would I send a byte or an entire array and how would I read it ?

Thanks in advance,
Alex

Have you tried Google ? ("arduino serial communication between UNOs")

Well given that one of them was just an Atmega328, no I had not tried this title.. :confused: I hope this works...

Connect Rx to Tx, Tx to Rx, Gnd to Gnd.
Serial.write on one side to send the data,
on the other side:
if (Serial.available()>0){
incomingByte = Serial.read();
// and do something with the data
}

Well given that one of them was just an Atmega328, no I had not tried this title..

Why not ? It's the same processor. Same code . Same everything.
Did you try Google the way I suggested ?

How is that helpful raschemmel? Provide the simple answer instead.

Did you try Google the way I suggested ?

Yes and I found part of the answer !

Connect Rx to Tx, Tx to Rx, Gnd to Gnd.

But where will the Atmega328 get the power from ? Will I connect VCC, AVCC to +5V on the Arduino ?

Oh, you do not even have a barebones Arduino set up?
Search Barebones Arduino, has been posted many time.

How is that helpful raschemmel? Provide the simple answer instead.

This might help. Crossroads already gave you the schematic but here is tutorial on the circuit.

That's more like it.

Oh, you do not even have a barebones Arduino set up?

Actually I have it set up ! But in the first schematic the J1 +5in and GND are from say a battery ? And I connect its GND to the computer-connected uno's GND ?

Battery, 5V wallwart, 7805 regulator, another Arduino, whatever.
Gnd to Uno's Gnd.

Alright so far so good but here 's another problem...

Arduino Uno : sender, connected to pc
Atmega328 : receiver, connected to Uno
common GND and power

Receiver Code

char var;

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
}

void loop(){
  if (Serial.available()>0)
  var = Serial.read();

  
  if (var=='0'){digitalWrite(13,LOW);}
  else if (var=='1'){digitalWrite(13,HIGH);}
}

Sender Code (works)

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

void loop(){
  Serial.write("0");
  delay(500);
  Serial.write("1");
  delay(500);
}

Sender Code (does not work)

int var;

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

void loop(){
  var = Serial.read();
  
  if (var=='0'){Serial.write("0");}
  else if (var=='1'){Serial.write("1");}
}

Any ideas why the last code does not light the led ?

I always use an extra conditional with serial receive, such as:

void loop(void)
{
    if (Serial.available() > 0)
    {
      char temp = Serial.read();
      Serial << temp;  // for diagnostic and Arduino term echo
      switch (temp)
        {
          case '\n' :
            ++nRow; nColumn = 0;
            if (nRow == 6) nRow = 0;
            LcdString(BlankLine[0]);  // clear line

          case '\r' :
            ++nRow; nColumn = 0;
            if (nRow == 6) nRow = 0;
            LcdString(BlankLine[0]);  // clear line

          default :
            gotoXY(nColumn, nRow);
            if (temp > 31 && temp < 128) SendCharLCD(temp);
            if (nColumn == 0 ) LcdString(BlankLine[0]);  // clear new line
        }
    }
}

A nice project to build:
http://www.hackster.io/rayburne/the-qbf-signal-generator

Ray

I found the problem ! It seems that I have to create another Serial port and use that one to communicate with the Atmega328 while I communicate through the default Serial port with the UNO.

#include <SoftwareSerial.h>

SoftwareSerial mp(10, 11); // RX, TX

char var, var2;

void setup(){
  Serial.begin(9600);
  
  Serial.println("CONNECTED");
  mp.begin(9600);
}

void loop(){
  var = Serial.read();
  var2 = mp.read();
  
  if (var=='0'){ mp.write("0"); Serial.println("0");}
  else if (var=='1'){ mp.write("1"); Serial.println("1");}
  
  if (var2=='H'){digitalWrite(13,LOW);}
  else if (var2=='L'){digitalWrite(13,HIGH);}
}

Yes, if you want 2-way comm's with each.
Or if echoing the data received from the PC back to the PC while passing it to the 2nd Uno is a problem.

I have one more question: if I have 50 inputs (from 50 robots with their own atmega328s) and want to upload data from them to 10 atmega328s (through RX-TX), is there a more efficient way other than having a "manager" atmega328 which will control a grid of transistors (just like a led matrix) in order to connect the robots with the 10 atmega328s ? The goal is to process information faster which means that groups of 5 robots will need to be directly connected to one of 10 atmega328s

In other words:
50 inputs
50 robots which will connect to those inputs (RX-TX) in a random order (I cannot know which robot will go where)
10 atmega328s for data processing

How will I connect the inputs to the 10 atmega328s (so that each input can connect to each atmega328 but will connect to only one -the right one) ?

In other words:
50 inputs
50 robots which will connect to those inputs (RX-TX) in a random order (I cannot know which robot will go where)
10 atmega328s for data processing

How will I connect the inputs to the 10 atmega328s (so that each input can connect to each atmega328 but will connect to only one -the right one) ?

What you are discussing is the old technology "channel" approach of multiplexing ... 5::10 to have 50 total channels, but only 10 active at a time. In the wireless space today, "mesh" networks deal with this issue kind of like Ethernet deals with packet collision, through random delay back off algorithms.

A better approach for such a low population would be polling from the master controller. Each robot would listen for their unique ID and only transmit after receiving the go-ahead. If you wanted to have lots of fun, the 10 hubs could accept data from any robot and community the "I Have ### @ HH:MM:SSSS" Which would force any other hub to drop older data for ###. A zillion ways to have fun with host polling.

Ray

A better approach for such a low population would be polling from the master controller. Each robot would listen for their unique ID and only transmit after receiving the go-ahead. If you wanted to have lots of fun, the 10 hubs could accept data from any robot and community the "I Have ### @ HH:MM:SSSS" Which would force any other hub to drop older data for ###.

If I understood correctly, what you are suggesting is that I have a master microcontroller and this mc will collect all the data. But for 50 robots will this not take too much time ? Apart from collectiing there will also be data processing and then sending info back ! How fast will it be with only one mc ?