Uno resets every time my Pi script opens /dev/ttyUSB0
Translated from Español by AI ·
Bit of a beginner so sorry if this is obvious. I have an Arduino Uno clone (CH340 chip) plugged into a Raspberry Pi with USB. The Uno counts pulses from my gas meter’s reed switch and prints a line every 10 s, and a Python script on the Pi reads the lines and sends them to Hermetry.Problem: every time the Python script starts (and it gets restarted by cron if it crashes), the Uno resets. The pulse count goes back to 0 and the first few seconds of serial are lost. On the board in Hermetry I get these weird drops in the gas total.
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
# Uno LED blinks, "boot" line comes in, count=0
I read it’s the DTR line. How do I turn DTR off on ttyUSB0 so the Pi stops resetting the Arduino?
Bit of a beginner so sorry if this is obvious. I have an Arduino Uno clone (CH340 chip) plugged into a Raspberry Pi with USB. The Uno counts pulses from my gas meter’s reed switch and prints a line every 10 s, and a Python script on the Pi reads the lines and sends them to Hermetry.
Problem: every time the Python script starts (and it gets restarted by cron if it crashes), the Uno resets. The pulse count goes back to 0 and the first few seconds of serial are lost. On the board in Hermetry I get these weird drops in the gas total.
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=2)
# Uno LED blinks, "boot" line comes in, count=0
I read it’s the DTR line. How do I turn DTR off on ttyUSB0 so the Pi stops resetting the Arduino?
Easiest fix: don’t restart the script. Run it as a systemd service with Restart=always, open the port once and never close it.
What is DTR? I have the same kind of setup for a water meter
DTR is one of the old modem control lines. On the Uno it goes through a 100 nF capacitor to the reset pin, so the IDE can reset the board before an upload. Any program that opens the port toggles DTR, so the board resets. I fought this on a line counter at work last year.
Software side, two things together:
stty -F /dev/ttyUSB0 -hupcl # don't drop DTR when the port closes
ser = serial.Serial()
ser.port = '/dev/ttyUSB0'
ser.baudrate = 9600
ser.timeout = 2
ser.dtr = False
ser.open()
Be warned that on Linux the driver may still raise DTR for a moment on the very first open after boot, so this reduces resets, it doesn’t always kill them.
The sure fix is hardware: a 10 µF capacitor between RESET and GND on the Uno, minus to GND. That swamps the reset pulse. You then have to pull it out (or press reset by hand at the right moment) when you upload new firmware.
And for the drops in your total: also keep the count in EEPROM every few minutes, or have the Uno send a running total since it was flashed instead of since boot. A reset then costs you a few pulses, not the whole total.
+1 on sending a total. If you ever need consumption per hour, the history API and charts can work it out from the counter.
Thanks James. -hupcl alone wasn’t enough, the first open after a reboot still reset it, like you said. Put the 10 µF cap on (with a label on the board saying “remove to upload”, learned that the hard way already) and the resets are gone. Also moved the script to systemd like twilson said. Total is stable now.
- You must be logged in to reply to this topic.
Join the conversation
Sign in to ask and answer, like helpful posts and mark the reply that solved your problem. Every contribution earns points and levels.

