---
slug: "ラズベリーパイでリレーを使ってUSB機器をオンオフする"
title: "Turning USB Devices On and Off with a Relay Using Raspberry Pi"
description: "I wanted to use external speakers to play music with my Raspberry Pi, but I thought it would be a waste of energy to keep the speakers powered on all the time. So, I decided to create a system that turns the power on only when needed.\n\nYou can easily achieve this with a single relay. The relay is controlled by the GPIO.\n\nWiring Diagram\n"
url: "https://www.ytyng.com/en/blog/ラズベリーパイでリレーを使ってUSB機器をオンオフする"
publish_date: "2014-05-31T03:29:46Z"
created: "2014-05-31T03:29:46Z"
updated: "2026-02-27T02:13:53.000Z"
categories: ["Raspberry-Pi"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/7533d4654ee840d1aff088f45ed5f8ff.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Turning USB Devices On and Off with a Relay Using Raspberry Pi

I wanted to use external speakers to play music with my Raspberry Pi, but I thought it would be a waste of energy to keep the speakers powered on all the time. So, I decided to create a system that turns the power on only when needed.

You can easily achieve this with a single relay. The relay is controlled by the GPIO.

<h2>Wiring Diagram</h2>
<img src="http://ytyng.com/picture/raspberry-pi/relay/raspberry-with-relay.png"/><br/>

Normally, the Raspberry Pi gets its 5V power from the Micro USB port, but I read that there is a regulator in place, and the current from the +5V pin of the GPIO is quite weak (700mA?). Conversely, the +5V of pin 2 on the GPIO can also be used as a power input, so I tried supplying power from there. However, since this bypasses the regulator, you need to be careful about overcurrent.

When I tried it, the wiring became very neat. It’s a good setup.

<img src="http://ytyng.com/picture/raspberry-pi/relay/IMG_3226.JPG"/><br/>

<h2>Test Video</h2>
<iframe allowfullscreen="" frameborder="0" height="315" src="//www.youtube.com/embed/S8m9UgJtsLg" style="max-width:100%" width="560"></iframe><br/>
From ipython, I turned GPIO7 ON to power the USB-powered speakers.

<h2>Code</h2>
Just before playing the audio, turn GPIO7 ON, and turn it OFF once the playback is finished. Here’s how it looks in a bash script.

<pre>#!/bin/bash
gpio mode 7 out
gpio write 7 1
mpg321 "$@"
gpio write 7 0
</pre>

(You can install the gpio command from <a href="https://projects.drogon.net/raspberry-pi/wiringpi/download-and-install/" target="_blank">here</a>.<br/>
You can install mpg321 with <code>sudo apt-get install mpg321</code>.)

Save this script with a name like play-music and make sure the path is set.

<pre>$ play-music music/xxxx.mp3
</pre>

This will automatically turn the speakers ON → play → turn the speakers OFF.

This time, I used a physical switch relay, so there’s a "click" noise when turning the power ON/OFF. While this may not be noticeable if enclosed in a case, using a solid-state relay can help reduce the noise.
