# Olimex/ATMega328 Reading from UART and sending to USB

I'm getting frustrated with my electronic knowledge, because I want to do this simple thing:

Olimex 328/ATMega328 reads data from UART (Olimex MOD-PULSE or whatever) and forwards it to the USB Port (my computer).

I don't understand how I can do this. I wrote following program:

void setup()         // run once, when the sketch starts
{
Serial.begin(9600);
Serial1.begin(9600);
}

void loop()                       // run over and over again
{
Serial.println(Serial1.read());  // prints hello with ending line break
}


But it doesn't work, since Serial1 does not exist. Could you give me a hint? I can't find anything that I can understand on the Internet.

Apparently the site didn't like my attempt to complete my sign-up. So further to your comment on my previous answer(as user46333):

On the Uno (the board I am using for development), it is possible to communicate with the board using the UART in two ways. There is the USB connection, and there are the Rx/Tx pins (2 and 3, respectively). It is possible to connect a device (ie RS232->TTL connection) to the Rx and Tx pins to communicate serially with the controller. When the USB is connected to the computer, it communicates on the same lines, indirectly, by going through the ATmega16U2. The two connect to the ATmega328 via the same pins, as demonstrated by the RED lines in the picture below (schematic from Arduino Uno documentation):

do they read/write on the same cable?

They read/write on the same pins.

Also, to correct my code, and simplify it:

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

void loop() {
if (Serial.available() > 0) {
}
else {Serial.println("HELLO");}
delay(2000);
}


This will receive any data sent to the controller on the Rx pin, and then immediately print it to the Tx pin.

• ah i see.. thanks for the great and extensive answer!!! – BvuRVKyUVlViVIc7 Jun 27 '14 at 20:18

ATMega328 has only one hardware serial port, i.e. using Serial1 makes no sense. How do you suppose you transfer it to USB CDC device? You need serial2USB bridge (prabably via second UART) or chip supporting USB (e.g. ATMEGA1286/7).

• But I connect the Arduino via USB to my computer..? Is it not possible to receive data from UART and send it to the USB port? – BvuRVKyUVlViVIc7 Jun 27 '14 at 8:45

On the Arduino Uno (example that uses the ATmega328), data can be sent to and from the device via the UART. Example code:

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

void loop() {
if (Serial.available() > 0) {
Serial.println(getString);
}
else {Serial.println("HELLO");}
delay(2000);
}


Will look for incoming serial data, and print it back to the serial connection, otherwise printing simply hello. This code can be modified to accept different variable types, and still print them out to the serial connection.

Also, it is possible to use other I/O ports to simulate a UART connection, using compiled code libraries (one exists on Arduino's site), which should be more than adequate with a low baud rate.

• So this receives from UART and sends it to the USB port? Its the same class you use..? Sorry, im totally newbie here.. – BvuRVKyUVlViVIc7 Jun 27 '14 at 8:46
• Yes, if you are sending code and/or receiving serial communications from the Arduino via a USB port, that is exactly what it does. In the case of the Uno, the USB connection is the UART connection. – user46333 Jun 27 '14 at 10:55
• but there are 2 contacts for cables? the UART connection and the mini-USB connection? Or do they read/write on the same cable? – BvuRVKyUVlViVIc7 Jun 27 '14 at 14:20