displaying M0 Express code in COM port window

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
earthres
 
Posts: 221
Joined: Fri May 28, 2021 10:48 am

displaying M0 Express code in COM port window

Post by earthres »

When I write Arduino code for the Feather M0 Express, I'm having trouble getting Serial.print() to work, to display results on the COM port window, using Serial.begin(115200). When I open the COM port window, nothing displays. This SEEMS like the M0 still thinks it's supposed to be communicating with the computer on the COM port used to upload a sketch. Does this problem make sense? Does this have something to do with the yield() function? How do I get stuff to display in the COM port window?

User avatar
earthres
 
Posts: 221
Joined: Fri May 28, 2021 10:48 am

Re: displaying M0 Express code in COM port window

Post by earthres »

I should have included a code snippet. If I use a sketch to set date/time on the FeatherWing RTC+SD, I can't see these Serial.print() messages in the COM port window.

Serial.begin(115200);
Wire.begin();
if (rtc.begin()) Serial.println("clock running...");
else Serial.println("clock not running...");

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: displaying M0 Express code in COM port window

Post by adafruit_support_carter »

Try adding a serial wait line:

Code: Select all

 Serial.begin(115200);
 while (!Serial);
That way it will wait for the Serial Monitor to open before outputting anything.

User avatar
earthres
 
Posts: 221
Joined: Fri May 28, 2021 10:48 am

Re: displaying M0 Express code in COM port window

Post by earthres »

Indeed, this is the solution! I'm curious as to why the M0 Express requires a while(!Serial) statement and UNOs, etc. don't.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: displaying M0 Express code in COM port window

Post by adafruit_support_bill »

The M0 processor is a native USB device and can emulate a USB/Serial device. That device is not 'available' until the COM port on the other end makes the connection.

The Atmeg328 processor in the UNO only has TTL Serial. It uses an external chip to perform the USB/Serial function. Since the processor has no way of knowing whether there is anything at the other end, they use the serial DTR pin to pull the reset line on the Atmega328 when a connection is made.

User avatar
earthres
 
Posts: 221
Joined: Fri May 28, 2021 10:48 am

Re: displaying M0 Express code in COM port window

Post by earthres »

I hope I'm not the only long-time UNO, etc., user who has trouble using the Feather M0 Express!

On my WIndows 10 system, the available COM port switches back and forth between 11 and 12. When I try to upload, double-clicking the reset button, the messages SAY it's selected a port and uploaded, but then nothing happens. When I look at the COM ports again, it's switched from 11 to 12 or 12 to 11. Also, I once got the sketch running with the USB cable connected. But when I unplugged the cable and then tried to restart the sketch by powering up again with the USB cable, the sketch didn't run -- what that means in the context of the sketch is that the FeatherWing OLED display didn't light up at all, even to display the Adafruit image at the beginning, after the first display.display();

Here's the complete sketch

Code: Select all

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <RTClib.h>
RTC_PCF8523 rtc;
int YR,MON,DAY,HR,MIN,SEC;
Adafruit_SH1107 display = Adafruit_SH1107(64, 128, &Wire);
void setup() {
  Serial.begin(115200); while (!Serial);
  //Serial.println("Running...");
  Wire.begin(); rtc.begin();
  DateTime now=rtc.now();
  YR=now.year(); MON=now.month(); DAY=now.day();
  HR=now.hour(); MIN=now.minute();SEC=now.second();
  delay(250); // wait for the OLED to power up
  display.begin(0x3C, true); // Address 0x3C default
  display.display();
  delay(1000);
  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setRotation(1);  
  display.clearDisplay();display.display();
  display.setCursor(0,0);
  display.println("HELLO WORLD");
  //display.println("Date/time YYMMDD");
  display.print(YR);display.print('/');
  display.print(MON);display.print('/');
  display.print(DAY);display.print(' ');
  display.print(HR); display.print(':');
  display.print(MIN);display.print(':');
  display.println(SEC);
  display.display();
}
void loop() {}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: displaying M0 Express code in COM port window

Post by adafruit_support_bill »

On my WIndows 10 system, the available COM port switches back and forth between 11 and 12.
This can happen when the M0 processor resets. Since the USBSerial emulation is done in the processor itself, the reset also kills the serial connection. If it tries to reconnect before Windows has had a chance to shut down the previous port, it will show up on a different port number. The UNO doesn't have this issue because the USB/Serial chip is separate from the processor and doesn't get reset when the processor does.
But when I unplugged the cable and then tried to restart the sketch by powering up again with the USB cable, the sketch didn't run
Did you connect to it with the Serial Monitor? The "while (!Serial);" will wait indefinitely for the COM connection from the PC. You could add a timeout for when you want to run without the serial monitor. Something like:

Code: Select all

while (!Serial && (millis() < 3000));

User avatar
earthres
 
Posts: 221
Joined: Fri May 28, 2021 10:48 am

Re: displaying M0 Express code in COM port window

Post by earthres »

Well, for this particular sketch I don't really need to display anything with Serial.print() statements. If I just take out those statements, everything is fine when the system is powered up once the code is loaded. So I guess the solution for any project that will be used "off line" is not to have any Serial.print() statements. This is a big change in thinking about code for UNO, etc., users, because they (including me) are used to writing Serial prints, perhaps for initial code testing, and just leaving them in when the system is no longer connected to a computer. Then there's no longer any COM port window to display text or values, but it doesn't matter.

Anyhow... perhaps now I can continue to develop this code (I need to include reading from some sensors) without more problems. For I2C sensors in particular, during code development it may be important to include some code that displays information about whether the sensor is working properly or not. But once everything is working and the system is used off-line, the assumption must be that the sensors are working and the serial prints no longer make any sense. Or, you could assume that you know how to hook up the sensors properly so there's no reason to have to test that they're working :-) Code for sensors can be written and tested on what I would describe as a much easier board to work with.

Having said that, M0 Express-based systems are well worth the effort if for no other reason than they are easily powered with a LiPo battery. Also, the fact that they're 3.3V power/logic boards is also a plus for I2C sensors that aren't 5V compliant. (Not all vendors are as diligent as Adafruit about designing breakouts that will work with 3.3V or 5V boards.)

Finally, thanks once again for your patience and help!

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: displaying M0 Express code in COM port window

Post by adafruit_support_bill »

I'm with you completely on the benefits of a good old-fashioned serial port. Since they work regardless of whether anyone is actually listening at the other end, they make ideal diagnostic ports for embedded systems.

Dynamic "plug-and-play" port assignment for USB devices is another annoyance for embedded systems. I design high-end instrumentation systems that typically have a dozen or so subsystems - many of which are connected via USB/Serial. Despite being a static configuration, it still requires a whole extra level of startup logic to detect which ports are available, then figure out what is actually connected to each one.

But enough of my ranting. It sounds like you have a handle on the differences in serial communication between the UNO and the M0. Good luck with your project :-)

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”