ZUGYUUUN!

Examples > I/O Relay

Description

スマートフォンから「ON」「OFF」ボタンのタップでリレーの制御を行います。

Raspberry PiのGPIOは3.3vの電圧を制御可能ですが、3.3v以外の電圧や大きな電流を扱う事ができません。
そこでモーターやソレノイド等、電流をたくさん使う部品を制御する場合、GPIOに直接接続すると
正しく動作しなかったり、場合によってはRaspberry Piの電源が落ちる場合があります。

上記の対応方法としてRaspberry PiからGPIOで制御可能なリレーを挟む方法があります。
今回のサンプルでは5vで駆動するソレノイドをRaspberry Pi上の5v電源から供給しつつ、電源のON・OFFを制御しています。
Raspberry Piと同じ電源を用いていますので、ソレノイドの数を増やしたり、ソレノイドの性能を上げた場合は
正しく動作しない可能性がありますのでその場合は別途電源を用意する必要があります。

Hardware Required



リレーは直接ブレッドボードに挿さりませんので、写真のように一旦ハンダ付けします。

Circuit

Raspberry Piの17番ピンとリレーの9番ピン、Raspberry PiのGNDとリレーの2番ピンを繋ぎます。
(リレーに極性は無いので、逆に繋いでも問題ありません。)
リレーの5番ピンと5vを繋ぎます。(リレーの5番ピンと6番ピンは直結していますので、どちらでも問題ありません。)
リレーの1番ピンとソレノイドを繋ぎます。(ソレノイドに極性は無いので、どちらに繋いでも問題ありません。)
ソレノイドとRaspberry PiのGNDを繋ぎます。

ソレノイドはソレノイドに流れている電流を止めた際に逆起電力というものが発生します。
ここでは詳しく触れませんが、ダイオードをソレノイドと並列に接続することで回路の故障などを防ぐ事ができます。

Code

github.com/zugyuuun/example_io_relay

index.html
<!DOCTYPE html>
<html>
<head>
<title>I/O Relay | ZUGYUUUN! Examples</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css" />
<link rel="stylesheet" href="style.css" />
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://cdn.zugyuuun.com/libs/0.9/zgn.js"></script>
<script src="app.js"></script>
<body class="example">
  <button id="on" class="btn">ON</button>
  <button id="off" class="btn">OFF</button>
</body>
</html>
app.js
ZGN(function()
{
  // 17番ピンで動作させます
  var relayPin = '17';

  // TerminalのGPIOインスタンスを取得します
  var gpio = ZGN.term('1').gpio;

  // 指定ピンを出力に設定
  gpio.pinMode(relayPin, ZGN.OUTPUT);

  // ONボタンをクリック
  $(document).on('click', '#on', function() {
    gpio.digitalWrite(relayPin, ZGN.HIGH);
  });

  // OFFボタンをクリック
  $(document).on('click', '#off', function() {
    gpio.digitalWrite(relayPin, ZGN.LOW);
  });
});
style.css
@charset "utf-8";

* {
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  width: 100%;
  height: 100%;
  display: table;
}

body {
  min-height: 100%;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  -webkit-text-size-adjust: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: rgba(0,0,0,0);
}

.btn {
  -webkit-appearance: none;
  appearance: none;
  background: -moz-linear-gradient(top, #fff 0%, #e6e6e6);
  background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#e6e6e6));
  border-radius: 20px;
  -moz-border-radius: 20px;
  -webkit-border-radius: 20px;
  -moz-box-shadow: inset 1px 1px 1px rgba(000,000,000,0.3);
  -webkit-box-shadow: inset 1px 1px 1px rgba(000,000,000,0.3);
  padding: 10px 15px;
  color: #111;
  cursor: pointer;
  outline: none;
}

.example .btn {
  display: block;
  width: 80px;
  height: 80px;
  margin: 50px auto;
  font-size: 0.5em;
}

See Also

ZGN.ready( )
ZGN.term( )
Gpio.pinMode( )
Gpio.digitalWrite( )