Skip to content

Instantly share code, notes, and snippets.

@calvinli
Created April 4, 2015 05:09
Show Gist options
  • Save calvinli/520373545ea83105a527 to your computer and use it in GitHub Desktop.
Save calvinli/520373545ea83105a527 to your computer and use it in GitHub Desktop.
Automatic software reset for the Arduino Leonardo/Micro
#!/usr/bin/env python
from __future__ import print_function
import serial, sys
from time import sleep
#
# Arduino Leonardo/Micro automatic software reset tool.
#
# Calvin Li, 2015-04-03
#
# command-line argument is the port the Arduino is on
serialPort = sys.argv[1]
print("Resetting the Arduino Leonardo/Micro at {}... ".format(serialPort), end="")
# First we open and close the port at some normal baudrate.
ser = serial.Serial(port=serialPort, baudrate=9600)
ser.close()
# Now, we open the port at the magic 1200 baud.
ser.baudrate = 1200
ser.open()
# Tricky, completely undocumented bit: RTS must be HIGH and DTR LOW.
# Otherwise absolutely nothing happens!
# ...yeah, I know.
ser.setRTS(True)
ser.setDTR(False)
# Close the 1200 baud connection, triggering a reset of the Arduino.
# The bootloader will run for 8 seconds (14 blinks).
ser.close()
# yay!!!
print("done.")
# Addendum: If this is used right before uploading new code,
# we need to give the bootloader some time to start up.
time.sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment