Basic serial I/O from PIN 0/1

Hi,
I started this new topic but first of all I apologize for a, I suppose, boring question.

As many of us, I'm trying to collect data from a serial data source (e.g. a GPS, but also other sources) and to do something with this data (e.g. store the data in a file on a SD).

When the serial data source is the USB and I use the serial monitor of IDE everything works using the best-known code "serialEvent" like:

//***********************************************************************
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;

    // blocco di debug per vedere sescrive ogni carattere sul file e se blink IL LED 13
    f.print(inChar);
    f.flush();
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(100);              // wait for a second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    delay(100);  
    
    
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

This means that the function serialEvent() is called properly when something arrives in the serial port; in this piece of code there are also some "blink" to see if the function is really called.

But, when I detach the USB cabel and attach the wires of the serial source to pin 0,1 and GND (TX, RX and Ground) and restart the program, it seems that nothing arrives from the serial port and the serialEvent() is never called.

This following simplified test code proof the fact, as no blnk can be seen even if he blink instruction is now the first instrucion of serialEvent()

int incomingByte = 0;   // for incoming serial data
String inputString="";
boolean stringComplete=false;

//***********************************************************************
void serialEvent() {
  
 digitalWrite(13, HIGH);   
 delay(1000); 
 digitalWrite(13, LOW);
 
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();

    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    
    // digitalWrite(13, HIGH);   
    // delay(200);           
    // digitalWrite(13, LOW);   
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}



//******************************************************************
void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
  inputString.reserve(200);
}



//***********************************************************************
void loop() {
}

The serial data source works properly, this has been checke attaching the wires directly to USB wires and checking the dada via terminal server.

serialEvent is not well,-known; I don't think that a lot of people use it.
2)
The last part of your post is worrying; if you connect wires from the device directly to usb (of the pc?) and ger data, the device is a usb device and not a serial device. Can you elaborate?

Ah, maybe you focused the problem!

My "serial data source" (or what I suppose it is) is a GPS tracker with a mini-USB phisical interface.
What I did is:

1 - cut the cable connected to mini-USB
2 - identify the black, the green, the red and the white wire
3 - connect the black wire to a GND pin of Arduino
4 - connect the green wire to PIN 0 of Arduino
5 - connect the white wire to PIN 1 of Arduino
(I tried also green to PIN 1 and white to PIN 0)

6 - in another test I also connected the red wire to a 5V pin of Arduin, just to power directly the GPS

Usually usb connectors indicate that the device communicates via usb :slight_smile:

:frowning: :frowning:

If your GPS device has a typical serial interface (not USB), it will be inverted and have negative-going voltages.
You don't want to connect that to your Arduino, without using something like a MAX232 converter.

Thanks a lot to everyone, I've been far away from electronics for too much time... :slight_smile:

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thanks al loto Robin,
it seems really interesting.

In fact my software works well when the source of data is the Serial Monitor of the IDE connect to the USB plug in the Arduino board. This situation is also easy for debugging, as each Serial.print() made by the code can be seen in the Serial Monitor.
The problem is when, having an Arduino Uno (with just one serial input) I can't have simultaneously the Serial Monitor for debugging using Serial.println() and the real I/O port available for reading data from the source.

In order to correct my "absolute beginner error" to connect USB source directly to serial TX/RX, I've ordered a USB/Serial converter so I hope this can convert the input to PIN 0/1

Meanwhile I have also tried to connect the USB data source to the USB plug of Arduino, but with the same code (that should blink when the SerialEvent() is called, I had no results. Needless to say that if I send characters from the Serial Monitor everithing works well.

DeluCapp:
The problem is when, having an Arduino Uno (with just one serial input) I can't have simultaneously the Serial Monitor for debugging using Serial.println() and the real I/O port available for reading data from the source.

The usual solution for that is to use SoftwareSerial to create another serial port on two other pins for the GPS and leave pins 0 and 1 (Serial) free for debugging.

Or, if you have a USB-TTL cable you could use it to connect the SoftwareSerial pins to the PC for debugging.

...R

Yes I've read of it.

But having a GPS with a USB output, in any case I need a USB-TTL converter.

It seems strange that, connecting directly USB GPS to USB ARDUINO, nothing happens, while connecting directly USB PC (with Serial Monitor or just another Terminal Emulator program) and USB ARDUINO the program works correctly.

DeluCapp:
But having a GPS with a USB output, in any case I need a USB-TTL converter.

It seems strange that, connecting directly USB GPS to USB ARDUINO, nothing happens, while connecting directly USB PC (with Serial Monitor or just another Terminal Emulator program) and USB ARDUINO the program works correctly.

I find all that confusing.

Post a link to the datasheet or user manual for the exact GPS device that you refer to.

If it has a USB connection then it is a USB slave and is intended for connection to a USB host - like a PC. And an Arduino is also a USB slave. (Hope "slave" is the correct term - but I think my meaning will be clear).

...R

And your last post gave me the right answer... it's a matter of master and slave!!!!
Thanks a lot!