How to upload .ino to Teensy 4.1 using avrdude or teensy_loader_cli?

dlab

Active member
I have tried to upload to the Teensy 4.1 using the Arduino IDE headlessly, but it's a very finicky and unreliable process. Is there any way to upload a .ino file using the command line only?
 
I had issues like this when I first started with Teensy. I found programming to be very erratic.

I learned quickly that if you use a specific set of rules it always works first time.

1. Cut the 5V to USB trace on the Teensy board that links USB power to the external power.

2. Use 5V external power to the Teensy. If the supply is less than 5V the Teensy works but programmimg it is very flaky. Sometimes it will work - sometimes not.

3. Don't plug in or out the USB cable when the device is powered. This also messes up comms.

4. Make sure you have the latest Teensy loader program. Previous ones don't work - or at all.

It took me a while to come to all of the above conclusions but since doing this programming via the IDE works very well.

I always press the "tick" button at the top left of the IDE window. This saves, compiles and checks the sketch first. When you get the "Done compiling" message (on the blue bar) merely press the button on the Teensy. It should upload the program.

A couple of "gotcha's"...
If it doesn't work, reboot the computer before doing anything else. The USB port can become "stuck" sometimes and not work. Re-booting will clear it - then proceed as above.

Very occasionally, the Teensy loader doesn't load up. If you minimise the IDE window you should see the Teensy loader small window sitting there. It pops up when you press the "tick" (Verify) button on the IDE. If it's not there then you need to find out why it's not loading. Re-install it to make sure.

If you follow these simple points you should find it always works. No doubt, there will be some people here that would disagree with some of these points but if I do all this, the Teensy always works for me. You can make your own decision based on that I guess.

Hope this helps.
 
Some of those problems sound like the bugs in pre-10 versions of Windows.

Thankfully Microsoft finally shipped pretty good USB drivers with Windows 10.
 
I am using Linux (Raspberry Pi), and it is vital that the process be headless because interaction with the Raspberry Pi happens over SSH.
The biggest problem I faced was converting the .ino file to a .hex file, I could not find any way of doing this. The .hex file is needed because then teensy_loader_cli can then upload it to the Teensy

I have tried to upload to the Teensy 4.1 using the Arduino IDE headlessly
I use xvfb-run to use the IDE headlessly but there are many issues with this, teensy loader not running, the port ("/dev/ttyACM0") randomly closes, takes too long (5 - 6 minutes)
 
Several years ago, when I was playing with RPIs on a robot, and using a teensy as the Servo controller. I would do this headless as well.

Sometimes I did this, using Arduino IDE running on the RPI using VNC setup to my PC.

Most of the time however, I would build on the PC. I would logically use the Sketch->Export Compliled Binary command, which would put a copy
of the hex file in the sketch folder. I would then SCP the file over to the RPI, where I would then use teensy_loader_cli to program the teensy.

As I mentioned before in other threads, at one point I hacked up the boards.txt/platform.txt stuff, such that I had menu item like upload using that could choose local or remote build and on the remote one I hard coded in network address of the RPI, with a command line SCP that would automatically transfer the file to specific directory on the RPI (or at that time might have been BBBK or Edison or Odroid), and I would have a script running on the RPI (or other), that would detect if a file was modified, and if so call of to teensy_loader_cli with it....
But I have since lost that setup as it was at least 5 years ago.

At some point you will probably be able to do it using ArduinoCLI running on the RPI.
I believe they are currently building 64 bit and maybe 32 bit ARM versions these days.
https://arduino.github.io/arduino-cli/0.29/installation/

But I don't believe we have version of the ARM teensy builds yet?
 
Are you aware of any xvfb command that makes Arduino IDE generate the .hex file? It needs to be headless because I'm using Python + subprocess (library to call shell commands) to do everything, and the upload process is done automatically periodically.

I guess another way to generate a hex file is using avrdude but I was unable to convert the ino file to a cpp file for avrdude to do its thing, would greatly appreciate some pointers on how to do this.
 
Last edited:
Sorry I am sort of lost here.

The build or verify commands of the Arduino IDE generates the hex files. This is part of the linking phase.

As I mentioned if you use the export binary command it will put a copy of it in your sketch folder.

avrdude has nothing to do with this! avrdude is an exe that can upload the binary files for AVR based boards to program them. It does not compile the sources and link them into the executable that is needed to program the processor.

Other options: use make
There are makefiles that can build teensy.
That is you can download and maybe need to install Arduino 1.8.x as well as teensyduino onto your RPI.
There are installs for this up on the arduino.cc and https://www.pjrc.com/teensy/td_download.html
There are makefiles in the <arduino install>/hardware/teensy/avr/cores/teensy4 directory as well as teensy3 directory
for building using make.

I don't remember if it by default will try to use teensy app to program your teensy or if it is up to you to do so. Been a long time since I tried it.
 
Hey I have a great update. I have found a way to upload to the Teensy 4.1 using the terminal only:

Generating .hex from .ino & storing the .hex file at a specified location:

arduino-cli compile --fqbn teensy:avr:teensy41 --output-dir /path/to/output/ /path/to/sketch.ino

Uploading it to the Teensy:

teensy_loader_cli --mcu=TEENSY41 -w blinky.ino.hex -v

However there is one teeny problem, this requires me to manually press the Program Mode button on the Teensy, is there any way to upload without pressing the button?

edit:
nevermind, you can use the -s flag for the teensy to be force reset.
The command becomes:

teensy_loader_cli --mcu=TEENSY41 -w blinky.ino.hex -v -s
 
Last edited:
Back
Top