Uno on a 12 V adapter and USB at the same time: will it fry my laptop?
hi, first year student, so sorry if this is obvious.
I have an Uno reading a capacitive soil moisture sensor on A0 and switching a 12 V solenoid valve through a relay module. The Uno talks to my laptop over USB serial, and a small Python script on the laptop reads the lines and posts them to Hermetry with the device HTTP API. That part works, I can see soil_moisture on my board.
The valve needs 12 V so I want to plug a 12 V adapter into the barrel jack. Is it OK to have the barrel jack AND the USB cable plugged in at the same time? I’m scared of back-feeding into my laptop’s USB port.
Also two weird things: when the valve clicks, the moisture reading jumps from around 410 to 600-something for a few samples, and sometimes the COM port disappears from the laptop and my script crashes with “could not open port”.
I think you have to choose one or the other? I always unplug the USB when I use an adapter.
No, the Uno handles both. But how is the relay powered, and where do the grounds meet? The jumping readings and the disappearing port sound like the valve, not the supply.
Relay module VCC is on the Uno’s 5V pin. Valve is 12 V through the relay contact, from the same adapter. Everything shares one ground on the breadboard. No diode anywhere, I didn’t know I needed one.
Both at once is fine. The Uno has a comparator that switches the 5 V rail to the regulator when VIN is above roughly 6.6 V, and USB data keeps working because the serial lines don’t care where power comes from. Your laptop is not at risk from that.
The trouble is the solenoid. A solenoid coil with no flyback diode throws a big voltage spike every time the relay opens, and that spike goes through your shared breadboard ground. That’s the jump in the analog reading and very likely the USB dropping out. What I’d change:
- A 1N4007 across the solenoid, cathode to +12 V.
- Take the relay coil power from the 12 V side or a separate 5 V supply, not the Uno’s 5V pin. The onboard regulator gets hot feeding a relay off 12 V in.
- Run the valve’s ground back to the adapter, and join it to the Uno ground at one point only, not through the breadboard rails the sensor uses.
- 100 nF plus 10 µF across the sensor supply, close to the sensor.
- In the sketch, don’t sample for a second after switching the valve, and average 8 readings.
On the laptop side, send only clean readings and let Hermetry stamp the time unless your laptop clock is synced:
import serial, requests
s = serial.Serial("/dev/ttyACM0", 9600, timeout=5)
URL = "https://<your-host>/api/v1/telemetry"
HDR = {"Authorization": "Bearer <device token>", "Content-Type": "application/json"}
while True:
line = s.readline().decode(errors="ignore").strip()
if line.isdigit():
requests.post(URL, json={"soil_moisture": int(line)}, headers=HDR, timeout=10)
Payload rules are on Device HTTP API.
It was the diode. Put a 1N4007 on the valve and moved the relay to its own 5 V supply. The COM port hasn’t dropped once in two days and the reading stays around 405-415 when the valve switches. Also added the one second pause. Thanks!
Ha, I stand corrected. Learned something too.
- 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.

