Serial data is transmitted only when it is connected to a PC

Hello,

I am new to Arduino. The first thing I am doing is to monitor temperature using DS18B20 sensor.

I have uploaded the following sketch to the Arduino Uno board:

#include <OneWire.h>
String path = "/api/Temperature/"; 
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
OneWire ds2(DS18S20_Pin); // on digital pin 2 (IN)

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

void loop(void) {
 float temperature = getTemp();
 Serial.print(path + round(temperature));
 //writeString(path + round(temperature));
 
 delay(5000); // Envía la temperatura cada 5 segundos
}

/*
void writeString(String stringData) {
  for (int i = 0; i < stringData.length(); i++)
  {
    Serial.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }
}
*/

float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius
 byte data[12];
 byte addr[8];

 if ( !ds2.search(addr)) {
   //no more sensors on chain, reset search
   ds2.reset_search();
   return -1000;
 }
 if ( OneWire::crc8( addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1000;
 }
 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1000;
 }
 ds2.reset();
 ds2.select(addr);
 ds2.write(0x44,1); // start conversion, with parasite power on at the end

 byte present = ds2.reset();
 ds2.select(addr);  
 ds2.write(0xBE); // Read Scratchpad

 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds2.read();
 }
 
 ds2.reset_search();
 byte MSB = data[1];
 byte LSB = data[0];

 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 return TemperatureSum;
}

All works perfectly. When I use serial monitor, I can see the data displayed. I see even the TX led in the board flashing.

When I disconnect USB cable from the PC, TX led stops flashing immediately.

On the other hand, I have other device that has a RS232 interface (it is actually a MODEM). I need to send the serial data from the Arduino to that MODEM. I connected both by a USB - RS232 cable.

Since in this case I don’t have USB power, I used an AC/DC adapter whose output is 12 V (I have read that voltage is OK) plugged in the power barrel. When I power the Arduino in this way, the green LED goes on showing that it is powered.

What is going on here? why transmission stops immediately when I disconnect the USB cable from the PC? Because of that, obviously when connecting the cable to the MODEM, it will not work also.

Any help will be appreciated, thanks

Jaime

If you have nothing connected to the USB cable and nothing connected to the Rx and Tx pins then it hardly matters whether the Tx LED is flashing.

I don't understand this - maybe a simple diagram would help.

On the other hand, I have other device that has a RS232 interface (it is actually a MODEM). I need to send the serial data from the Arduino to that MODEM. I connected both by a USB - RS232 cable.

If you have a device that works at RS232 signal levels you need an RS232 to TTL converter such as a MAX232 if you want to connect it to an Arduino. RS232 signal levels may damage an Arduino.

...R

Have a look at the schematic for the Arduino Uno. The TX and RX LED are connected to a separate microcontroller which handles the USB communication. If you do not use the USB connection that microcontroller will not switch the LEDs.

Please provide your schematic of how you connected your MODEM to the Arduino. Simple paper drawing picture is good enough.

Hello... I can post a schematic tomorrow, however, I don't think it is necesary. Only TX and GND are used.

As I told, the USB cable from Arduino is connected to a RS232 cable converter, like this one:

As you can see, that cable has USB Type A Male connector, so I used an adapter.

In summary, I have a USB cable connected to the Arduino Board (USB type B connector). The USB Type A connector is connected to an adapter to transform that connector into a Female connector. That way, I can connect that to the cable shown in link above.

Finally, I connected a DB9 female connector to the MODEM. TX in the DB 9 into RX of the MODEM. GND in DB 9 into GND of the MODEM.

As I told you, I can draw that for you to see, but it is very simple. Just TX coming from Arduino going to RX in the MODEM. GND coming from Arduino going to GND in the modem.

I don't think there is a voltage level problem. The cable that converts USB to RS232 is responsible to adjust the levels. And in fact, if I connect that cable to the USB port in the PC, I can control the MODEM communication through its RS232 interface.

And regarding TX led. Will it flash only if connected to the USB port in the PC? If it does not flash, doesn't it mean it isn't sending data?

If I am using the USB port connected to other device, is there a way to debug the program? Or maybe, using a PIN to send the serial data. Or maybe, to store some kind of log in the Arduino memory so that I can see the steps run by the program.

Thanks
Jaime

Does the board have ~5v on the 5v pin and the Tx pin when you disconnect the USB cable?

jstuardo:
As you can see, that cable has USB Type A Male connector, so I used an adapter.

In summary, I have a USB cable connected to the Arduino Board (USB type B connector). The USB Type A connector is connected to an adapter to transform that connector into a Female connector. That way, I can connect that to the cable shown in link above.

That won't work. The only way that could work is if the Uno had USB host capability, which it doesn't.

The best solution is to use a TTL to RS232 adapter and connect that to pins 0 and 1, as Robin2 recommended earlier. You can buy them for ~$1 USD w/ free shipping from China on eBay.

Hello,

I have seen the MODEM input specification that is TTL level.

So, I could connect Arduino PIN 1 to RX2 of the MODEM and GND together, is that right?

Should I do a change to the program?

When using

Serial.print(path + round(temperature));

That data is sent to both TX PIN and USB port?

So, when doing that, cannot I have the Arduino connected the PC in order to show some debugging messages? As far as I have read, I cannot use PIN 0 and 1 and USB port simultaneously. Is it correct?

Thanks
Jaime

jstuardo:
That data is sent to both TX PIN and USB port?

Yes, on the Uno pins 0 and 1 are Serial and are also connected to the USB interface chip.

These pins are also used to upload sketches to your Uno. This means that you might need to disconnect the modem from those pins in order to be able to upload a sketch.

jstuardo:
So, when doing that, cannot I have the Arduino connected the PC in order to show some debugging messages? As far as I have read, I cannot use PIN 0 and 1 and USB port simultaneously. Is it correct?

It might be possible to do that, but you will be sending the debug messages to the modem as well as the computer. Depending on the modem’s communication protocol, that could be problematic.

The alternative is to use the SoftwareSerial library to create a serial port on any two free pins on the Uno other than pins 0 and 1, then use that software serial port to communicate with the modem, and the Serial port to communicate with the computer.
https://www.arduino.cc/en/Reference/SoftwareSerial

Hello…

I am using SoftwareSerial now and the same problem I am seeing. Garbage is transmitting from the TX pin.

I am using PIN 11 for TX and PIN 10 for RX. To isolate the problem, I have connected RX in Arduino to TX in a DB9 connector. TX in Arduino to RX in a DB9 connector. Then, I used the DB9 to USB cable adapter and monitor in my PC what Arduino is sending.

GND of the DB9 is connected to one of the GND PIN’s in the Arduino.

Every 5 seconds, I can see what Arduino sends, as in this image:

![|500x85](http://www.desytec.com/comm arduino.png)

As you see, only garbage is sent. The pattern that is repeated is: “hO{EOT}Kh{NAK}MI{EOT}M#O{XON}E#Mh32hO{EOT}Kh{NAK}MI{EOT}M#O{XON}E#Mh32”

The program that uses SoftwareSerial is this:

#include <OneWire.h>
#include <SoftwareSerial.h>

String path = "/api/Temperature/"; 
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2
OneWire ds2(DS18S20_Pin); // on digital pin 2 (IN)
SoftwareSerial modem(10, 11); // RX, TX

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

void loop(void) {
 float temperature = getTemp();
 Serial.println(path + round(temperature));

 int bytesSent = sendToModem(path + round(temperature));
 Serial.print("Enviados ");
 Serial.print(bytesSent);
 Serial.println(" caracteres al Modem");

 if (modem.available())
    Serial.println(modem.read());
 
 delay(5000); // Envía la temperatura cada 5 segundos
}

int sendToModem(String stringData) {
  int sent = 0;
  for (int i = 0; i < stringData.length(); i++)
  {
    sent += modem.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }

  return sent;
}

float getTemp(){
 //returns the temperature from one DS18S20 in DEG Celsius
 byte data[12];
 byte addr[8];

 if ( !ds2.search(addr)) {
   //no more sensors on chain, reset search
   ds2.reset_search();
   return -1000;
 }
 if ( OneWire::crc8( addr, 7) != addr[7]) {
   Serial.println("CRC is not valid!");
   return -1000;
 }
 if ( addr[0] != 0x10 && addr[0] != 0x28) {
   Serial.print("Device is not recognized");
   return -1000;
 }
 ds2.reset();
 ds2.select(addr);
 ds2.write(0x44,1); // start conversion, with parasite power on at the end

 byte present = ds2.reset();
 ds2.select(addr);  
 ds2.write(0xBE); // Read Scratchpad

 for (int i = 0; i < 9; i++) { // we need 9 bytes
  data[i] = ds2.read();
 }
 
 ds2.reset_search();
 byte MSB = data[1];
 byte LSB = data[0];

 float tempRead = ((MSB << 8) | LSB); //using two's compliment
 float TemperatureSum = tempRead / 16;
 return TemperatureSum;
}

As you see, I am using 9600bps. Of course, monitoring program is set to the same baudrate.

when I connect the USB cable to Arduino and the PC in order to monitor what is sending, COM monitor shows the number if bytes sent, which is correct.

For some reason, it is not sending the right bytes. Can you check if my sendToModem function is correct? or can you detect what else is missing in the code?

Thanks
Jaime

This function

int sendToModem(String stringData) {
  int sent = 0;
  for (int i = 0; i < stringData.length(); i++)
  {
    sent += modem.write(stringData[i]);   // Push each char 1 by 1 on each loop pass
  }

  return sent;
}

can be reduced to a single line

int sendToModem(String stringData) {
  return modem.write(stringData.c_str());
}

It doesn’t really affect the behavior of your program. You can also use model.print() vs. .write()

Hello... thanks for the suggestion. It doesn't actually make any difference in behaviour.

By the way, this are my connections:

Thanks
Jaime

"I have connected RX in Arduino to TX in a DB9 connector. TX in Arduino to RX in a DB9 connector. "

Are you trying to connect an RS232 level line to a TTL level line?

jstuardo:
By the way, this are my connections:

No pictures.

Please get into the habit of checking your Posts after you submit them.

...R
Simple Image Posting Guide

Robin2:
No pictures.

Please get into the habit of checking your Posts after you submit them.

...R
Simple Image Posting Guide

I do see the pictures in the post. I will reduce size since each images are about 1 MB. Maybe your connection is not so good.

zoomkat:
"I have connected RX in Arduino to TX in a DB9 connector. TX in Arduino to RX in a DB9 connector. "

Are you trying to connect an RS232 level line to a TTL level line?

The RS232 to USB cable does the magic.

However, that is not the problem.

I have connected TX in Arduino directly to RX in the modem (which works with TTL levels) and exactly the same data is received by it.

Since I saw Modem was receiving incorrect data, I decided to connect the Arduino to the PC and see the same data sent.

"No pictures."

I see pictures. My experience is that some times viewing the forum using MS IE, pictures do not display.

Here are the images again.

Finally, the problem was that the Arduino sends the data using invert logic.

I have used this SoftwareSerial constructor

SoftwareSerial modem(10, 11, 1);

And it worked!!

I asked the question in https://arduino.stackexchange.com and there someone told me the solution.

Regards
Jaime

Good info to know if levels 5v.

Just for info, I still see no pictures. I am using Opera on Linux Mint.

…R