USB controlled mains switch

Jan 18, 2025 Last reply: 1 year ago 37 Replies

I'm looking for a USB controlled mains switch (230V AC) that I can connect to a PI to turn a mains power supply on and off.



I've done a bit of digging around on the net, and the options seem to fall into two categories, either a relay device which means having to rewire the device (which I'd rather avoid), priced around 30Euros or one that sits in line (between the device and the supply). The latter appears to be in excess of 100Euros, which is considerably more than I want to pay.



Any suggestions on where to look ? I'm not interested in a wireless solution, as it is likely to be used in places where signals are not reliable.



Thanks



Adrian


I've a similar thread in c.o.l.misc in particular. Although that's headed 'smart plugs?' and looking for a wireless solution.

My own feeling is that the bigger the gap between mains and my electronics, the better, and wireless is a good way for this.

I suspect off-the-shelf products, at least consumer-grade, will be wireless. Might there be mileage in running network wires to local wireless repeaters, then using wireless switches? Or use a network-over-mains scheme?

Oh, and if you only need a PI for local control, with no networking, don't forget the PI can be an AP and run a nearby wireless device without a central router.

(FWIW I'm leaning towards the Shelly Plus Plug, at around £20. IIRC there's a euro version too. It's the only one I've found that seems not to need cloud servers.)

I don't understand the difference. Surely anything that's going to turn something on/off needs to be "one that sits in line (between the device and the supply)" doesn't it?

Do you mean (the ones that cost 100 Euros or more) something that already has a mains plug and socket on it? I think they're going to be expensive simply because they aren't much in demand. What 'the world' wants is a WiFi connected switch and they're two a penny of course.

I think you'll probably have to bite the bullet and get a relay board of some sort. Does it **have** to be USB? There's lots of quite neat relay hats for the Pi.

formatting link
formatting link

I have some Shelly switches (not plugs), they are good too, just a bit more expensive and not open-source firmware.

I wouldn't guarantee the electrical safety of any of them, but have run a 2.4 KW heater off an Athom plug, for a short period of time, weeks (As opposed to continuously for 3-4 years for my freezer and fridge)

Why USB controlled? Is there a specific reason?

IMHO it's much easier (and cheaper) to use something like

formatting link
, connect it to the PI via WLAN and control it via MQTT. The PI can work as AP if there's no WLAN available.

HTH, Stefan

What do you mean not reliable? Active interference? Otherwise if you have a device close enough for USB, it will be close enough for Bluetooth or WiFi.

Pi Pico, transistor buffer and a big fat mains relay, plus code. You can talk down the same USB you use to power it.

Less work and more cost - use a Pi with more USB onboard

They aren't especially able to handle 230V at sane currents.

I made my own board up. Featuring onboard PSU and mains chokes and filers and a fuse, a 13A capable relay and a pi PICO. Also has an optional temperature sensor hooked up to the ADC

Can send you the Gerbers if interested

Sadly that is not a given. You wont get much wifi to a board inside a tin box, but you could make a hole for a USB cable...

serial over USB can probably do a hundred meters at a pinch, too.

The Pi already exists, and is connected to my home LAN (Ethernet). A range of a few feet should work for WiFi (if I used the PI as an AP), but home WiFi is at best flaky where I want to run this, hence why the Pi is on Ethernet.

£20 is nearer the make for what my soggy finger in the air suggested would be a sensible price, the LocalBytes site mentioned elsewhere in the thread comes out at £12.75. Although I quoted prices in euros, I'm in the UK, but all the items I'd found so far were priced in euros.

Thanks

Adrian

The cheaper option involves cutting the plug off the item, and hard wiring it into the board. I've got two problems with that. Firstly, it means that I can't readily reuse that item elsewhere as I'd have to replace the plug (a bit of a faff if it is temporary move), and secondly if I'm trying to control something fed via a wall wart then it gets messy unless I leave the wall wart powered all the time (which I'd rather not do), and switch between it and the device.

Trust me to be awkward :-)

No, but USB seemed to be the most likely solution as it works equally as well for Non-Pi situations. Power may also be an issue. My initial requirement is for a low power device, which (without checking the numbers) might be within the capability of running directly from the Pi, but I can foresee needs where that won't be the case, so a standard solution has its attractions.

Thanks

Adrian

By WiFi, in this case, I'm referring to my home WLAN.

This isn't a new install, I'm looking at extending what I do with that particular Pi. I've tried using the Pi on WiFi in that location and it can disappear off the network for prolonged periods of time. Likewise if I'm stood next to it with my mobile, it has reverted to 4G rather than WiFi for data, so the location is not really practical for a WiFi based solution.

Using the Pi as an AP could work, that isn't something I've tried yet. The Pi lives in a plastic IP55 box, so I would hoped that short range (couple of feet) WiFi / Bluetooth would work.

Thanks

Adrian

A slight caveat, I don't want to mislead, I use WiFi.

I see some of the plugs use Bluetooth, my older ones are WiFi only. I also think some of the plugs that advertise Bluetooth may use it only for configuration as opposed to a primary communication method. If you want to go that route, read the spec carefully.

My Wifi plugs are reliable, but my house had good coverage from multiple Wifi access points (Mesh/Fast Roaming). Cheap stuff, but it works better than anything I had previously.

I did a bit of searching around. AliExpress sell various models of USB mains-rated relay boards. Various models offer 5V, 12V or 24V operation. I see from a later posting that your RPi is in an IP55 plastic box, so I'm jumping to the conclusion that you're happy with a bit of DIY to turn it from a board into a solution.

If I were doing it this way, I think I'd go for a 12V powered board and have a separate PSU, rather than burden the RPi's supply further as many people have problems that are traceable to inadequate power supplies.

It sounds like you may have to build a USB library for the relay boards - I don't know.

If I were doing the overall job that you're doing, I wouldn't use USB, I'd use a GPIO pin to switch a different relay board on/off. Much easier (and I've done lots of work on USB firmware and drivers).

David

You could just chop up a mains extension cable and insert your relay in the middle.

The advantage of wireless is you get isolation for free, so you don't need to worry about mains backfeeding into the Pi, which makes construction cheaper. It doesn't need to be wifi, there are also options using 433MHz 'remote control' frequency, which is point to point and longer range:

formatting link
also with power measurement:
formatting link
The transmitter speaks GPIO and there's Python code to control them from the Pi.

Theo

In message <ASw* snipped-for-privacy@news.chiark.greenend.org.uk>, Theo <theom+ snipped-for-privacy@chiark.greenend.org.uk> writes

Thanks, that looks interesting.

Adrian

Adrian,

Whut ? As others have already mentioned, you could take a short extension cord and put the relais* in that cord. Makes it reusable too.

  • also consider a solid-state relais.

Also, you say USB controlled. How do you imagine that ? As some kind of special USB device (for which it is hard to find a driver) ?

Perhaps consider a simple USB-to-serial thingly*, where you can use the DTR line to signal the relais board to switch on. After that you can use the RPi build-in serial API to switch the relais.

  • I would strongly suggest to use one of RPi's gazillion I/O pins for it, but as you specifically mentioned USB controlled ...

The only thing you will need to do (and which costs money) is to put the relais (and the USB-to-signal-line thingamagochy ?) in a shielding plastic box. 220 is lethal even when you touch it by accident. :-) And make sure the 220v and the relais-driving low-voltage electronics are well seperated. Your RPi doesn't like 220v either. :-o

Regards, Rudy Wieser

As I have said previously, it's easy. E.g.

formatting link
'usbrelay' to control (apt-get install usbrelay')

Chris,

Whohoo! They actually did it, a hobby-level relay driven by USB. I'm amazed. And I see there are no drivers needed as long as its used with Win XP or 7. I stand corrected.

One question though : (where and) how did you find that Linux driver ?

Wait, what ?

Under "Technical details" :

Voltage ?5 Volts Wattage ?50 watts

Something is fishy there ... and I only now realize that I do not see any mentioning for which voltage its output is rated. If the above is correct than they have rated it for 5 volts (5V x 10A = 50W), not 220 ...

Do *NOT* use stuff above its voltage rating. The electrical track seperation between the (low voltage) driving electronic and (high voltage) output circuits might not be large enough.

Regards, Rudy Wieser

Look at the damn pictures of the relays, you can see the ratings and the model number used.

SR-05VDC-SL-C

5V DC SONGLE Power Relay SRD-05VDC-SL-C (5 Pin mit NC / NO) Contact form: 1a, 1b, 1c Rated load: 10A 250VAC/28VDC,10A 125VAC/28VDC,10A 125VAC/28VDC Contact resistance: <= 100 mOhm Electrical life: 100,000 Mechanical life: 10,000,000 Coil rated voltage: 3-48VDC Coil power: 0.36W, 0.45W Coil pick-up voltage: <= 75% Coil drop-out voltage: >=10% Ambient temperature: -25 degrees Celsius to +70 degrees Celsius Coil and contacts:1500VAC/min Contact and contacts: 1000VAC/min Insulation resistance: >=100M (ohm)

formatting link

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required