Platformio, teensy, serial output, not working

I’m using this code: https://playground.arduino.cc/Main/sourceblock_1/index.txt?action=sourceblock&num=1

Which is a very simple program to list out i2c devices. A key snippet:

  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}
void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");
...

Here is the relevant part of my platformio.ini

[env:teensy41imu]
board = teensy41
monitor_speed = 115200
build_src_filter = 
    -<*>
    +<i2c_scanner.cpp>

I am not sure what to do next. I have a raspberry Pi, connected to a teensy which has an IMU connected over i2c (as well as other devices.) On my Mac I am accessing the Pi remotely via vscode, using the platformio addin for platformio. All works well.

I upload the sketch successfully:

Processing teensy41imu (board: teensy41; platform: teensy; framework: arduino)
------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/teensy/teensy41.html
PLATFORM: Teensy (4.18.0) > Teensy 4.1
HARDWARE: IMXRT1062 600MHz, 512KB RAM, 7.75MB Flash
DEBUG: Current (jlink) External (jlink)
PACKAGES: 
 - framework-arduinoteensy @ 1.158.0 (1.58) 
 - tool-teensy @ 1.158.0 (1.58) 
 - toolchain-gccarmnoneeabi-teensy @ 1.110301.0 (11.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 105 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Wire @ 1.0
Building in release mode
Linking .pio/build/teensy41imu/firmware.elf
Checking size .pio/build/teensy41imu/firmware.elf
Building .pio/build/teensy41imu/firmware.hex
Configuring upload protocol...
AVAILABLE: jlink, teensy-cli, teensy-gui
CURRENT: upload_protocol = teensy-cli
Rebooting...
Uploading .pio/build/teensy41imu/firmware.hex
Teensy Loader, Command Line, Version 2.2
Read ".pio/build/teensy41imu/firmware.hex": 25600 bytes, 1.3% usage
Unable to soft reboot with USB error: Success
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read ".pio/build/teensy41imu/firmware.hex": 25600 bytes, 1.3% usage
Programming......................
Booting
======================================= [SUCCESS] Took 5.48 seconds =======================================

Environment    Status    Duration
-------------  --------  ------------
teensy41imu    SUCCESS   00:00:05.477
======================================= 1 succeeded in 00:00:05.477 =======================================
 *  Terminal will be reused by tasks, press any key to close it.

Nothing happens. Guessing that maybe I need to click on Monitor, I see this:

Executing task in folder firmware: platformio device monitor --environment teensy41imu 
--- Terminal on /dev/ttyUSB0 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H

And. nothing more. What am I missing?

Probably a baudrate missmatch?

vs.

But it looks like, you’re using the wrong Serial.

“The most common issue with serial ports on Teensy is use of code designed for Arduino Uno with Serial within the code. On Teensy, Serial accesses the USB only. USB and Serial1 (pins 0 & 1) are not shared on Teensy. For hardware serial ports, Serial1, Serial2, Serial3, Serial4, Serial5, Serial6, Serial7 or Serial8 must be used.”

Source: Teensyduino: Using the UART (real serial) with Teensy on the Arduino IDE

Just to add another thought: There may be more /dev/ttyUSB* or /dev/ttyACM* devices and PlatformIO might have picked the wrong one. Do an ls /dev/ttyUSB* and ls /dev/ttyACM* to check for that and set monitor_port = ... in the platformio.ini as needed (docs).

Another thought would be that some Serial USB implementations require the serial monitor to assert the virtual DTR signal. If this is not done, no output is shown, and especially the line

while (!Serial);             // Leonardo: wait for serial monitor

hangs up the sketch until a proper serial connection is detected.

For that, add

monitor_dtr = 0

(or 1) as needed in the platformio.ini (docs)

Well I know that on the Pi (Linux) side the port is called /dev/linobase. Is that what I should be using? I thought this was what it is called on the Teensy side.

[env]
platform = teensy
framework = arduino
upload_protocol = teensy-cli
upload_port = /dev/linobase

Thanks all. I got it to work with your help. I know this is a common problem but I was having a hard time solving it. In the end, there were several changes.

  1. From another post here I learned how to have two different “main” programs and launch one or the other:
[env]
platform = teensy
framework = arduino
upload_protocol = teensy-cli
upload_port = /dev/linobase
; [env:mega]
; platform = atmelavr
; framework = arduino
; board = megaatmega2560

[env:teensy41] 
board = teensy41
build_src_filter = 
    -<*>
    +<firmware.cpp>

[env:teensy41imu]
board = teensy41
monitor_speed = 9600
monitor_port = /dev/linobase
build_src_filter = 
    -<*>
    +<i2c_scanner.cpp>
  1. I had to correct the monitor_speed and also tell platformio what the “monitor_port” was. I supplied that and used the unix name for the USB connecting to the teensy. On my setup it is /dev/linobase. That is achieved with Udev rules that I have had.

  2. I did NOT have to use Serial1 etc. The code from the arduino example worked with simple Serial.printf.

Thank you