Serial and SoftwareSerial on a Leonardo

I am not able to get the code below to work on a Leonardo. The library cozir uses a SoftwareSerial to communicate with the co2 sensor, but the program hangs. If I comment out COZIR czr(nss); and change float temp = czr.Celsius(); to use a hard coded value the program works. I realise the Leonardo requires the RX to be on certain pins, but the program still hangs. Are the serial and software serial conflicting ? How do I get the program to work ?

#include <SoftwareSerial.h>
#include <cozir.h>

int led1 = 7;

SoftwareSerial nss(10,11);
COZIR czr(nss);

void setup(void)
{

Serial.begin(57600);
while (!Serial){};

Serial.println(“Setup”);
pinMode(led1, OUTPUT);

}

void loop(void)
{

float temp = czr.Celsius(); //32

Serial.println(temp);

digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
delay(200); // wait for 200 ms
digitalWrite(led1, LOW);
delay(200);

}

What is the cozir library for? Post a link to its documentation.

A Leonardo has a spare HardwareSerial port (Serial1) on pins 0 and 1. Why not use that?

...R

The library is for a CO2 sensor, the library is at Arduino/libraries/Cozir at master · RobTillaart/Arduino · GitHub

The code only allows for a SoftwareSerial not a Serial.

Thanks.

I think you could change lin 59 of cozir.h to use Serial1

Alternatively, maybe newSoftSerial would work on the Leonardo

What pins are you using for SoftwareSerial on the Leonardo? The RX pin must be one that can use pinChange interrupts.

...R

What pins are you using for SoftwareSerial on the Leonardo? The RX pin must be one that can use pinChange interrupts.

As stated in the code in my first post SoftwareSerial nss(10,11);

The problem seems to be when I upload the code, the port to the pc disappears and I can no longer use the serial monitor. This possibly hangs the program as it is waiting to communicate with the pc ? I am using linux by the way. Why is this happening in this sketch when others work ?

I have changed the library to allow using a hardware serial but I get the same result.

plantgrower:
The problem seems to be when I upload the code, the port to the pc disappears and I can no longer use the serial monitor.

What happens if you disconnect and re-connect the Leonardo (or just press its reset button) after you upload the program?

The Leonardo reset and serial systems are different from an Uno or Mega because the 32U4 MCU is used for the USB connection.

What PC operating system are you using?

I have changed the library to allow using a hardware serial but I get the same result

Did you change the connections to the pins for Serial1?

Why are you using a Leonardo for this project?

...R

Robin2:
What happens if you disconnect and re-connect the Leonardo (or just press its reset button) after you upload the program?

I get a diferent port than the one I uploaded on but the program still isn't responding.

What PC operating system are you using?

Ubuntu

I have changed the library to allow using a hardware serial but I get the same result.

Did you change the connections to the pins for Serial1?

Yes.

Why are you using a Leonardo for this project?

It's the only Arduino I have! I have had this library working on this board but it was a while back(2+ years).

I think I'll get another board.

@Robin2
One can not directly change the constructor to use Serial1 because SoftwareSerial is coded in the library; a compile error will be the result. the fact that there is no nss.begin(...) in OP's code basically gives it away.

@OP
I think that one however first must understand the behaviour of the Leonardo. I'm still wrapping my head around that but know that while(!Serial); will block code till a terminal program is opened. So the code in the opening post will never blink the led if no serial connection is made using a terminal program.

Although I don't know the side effects in the lower level 32U4 code, it feels like the line while(!Serial); is not strictly necessary. OP can try a small testsketch like below.

side note:
I noticed that on my system (Win8, IDE 1.6.6) the ports in device manager change; Leonardo bootloader on COM13, running Leonardo on COM12. Because I managed to get the Leonardo recognised (not sure how, probably selected the port at the wrong time during the Leonardo's bootload timing) on COM13 and selected that in the ports menu, uploads failed and I needed to reset the board to be able to upload and I could not start the Serial Monitor. Once I managed to change it to Leonardo on COM12, the Leonardo basically behaved like an Uno from a reset / upload / run perspective.

Shouldn't there be a "nss.begin(baudrate);" somewhere?

sterretje:
@Robin2
One can not directly change the constructor to use Serial1 because SoftwareSerial is coded in the library; a compile error will be the result. the fact that there is no nss.begin(...) in OP's code basically gives it away.

I am not surprised that it should be a bit more complex than what I suggested in Reply #3. I was just trying to nudge the OP towards some exploration.

I presume with a bit of perseverance the library could be made to use Serial1

@plantgrower, I have Linux Mint (a version of Ubuntu) on this laptop and when I plugged in my Leonardo and disconnected and reconnected it showed up as TTYACM0 every time. This was with my own program on it.

I find it hard to understand how a problem with SofwareSerial could affect the USB connection unless it is causing a major crash of the Leonardo. What happens if you load a simple "hello world" program onto the Leonardo?

...R

johnwasser:
Shouldn't there be a "nss.begin(baudrate);" somewhere?

In the library :wink: