0Pricing
Arduino & IoT Academy · レッスン

Servoライブラリとattach

Servoをインクルードし、ピンに割り当てます。

「Servoライブラリとattach」はCoddyKit上の無料Arduino & IoT Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはArduino & IoT Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Arduino & IoT Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Let a Library Help

Generating precise pulses by hand is painful. The built-in Servo library does the timing for you, so you just say the angle you want. 😌

Include It First

Bring the library into your sketch with an #include at the very top. That one line unlocks the Servo tools.

#include <Servo.h>

Make a Servo Object

Next, create a Servo object. Think of it as your remote control: you give it a name, then send commands through it.

Servo myServo;

Bind It to a Pin

Tell the object which pin the signal wire uses with attach(). After this, that pin starts sending servo pulses.

myServo.attach(9);

Where attach Goes

Call attach() inside setup(), since you only need to bind the pin once when the board starts up.

void setup() {
  myServo.attach(9);
}

Pick a PWM Pin

The Servo library can drive almost any pin, but a PWM-capable pin like 9 or 10 is the safe, conventional choice on an Uno.

Move with write()

Command an angle using write(). Pass a number from 0 to 180 and the servo heads straight to that position.

myServo.write(90);

It Stays Attached

Once attached, the object keeps holding the last angle you wrote. You only call write() again when you want it to move somewhere new.

Custom Pulse Limits

If your servo undershoots or overshoots, attach() can take min and max pulse widths in microseconds to fine-tune its travel.

myServo.attach(9, 600, 2400);

Free the Pin

Call detach() to stop sending pulses and release the pin. Handy when you want the servo to go limp and save power.

myServo.detach();

One Object Each

Driving several servos? Create a separate Servo object for each one and attach them to different pins. They run independently.

Quick Check

Let's test how you connect a servo to a pin in code.

Recap

Include Servo.h, make an object, attach it to a pin in setup, then move it with write(). That's the whole setup pattern. 🔧

よくある質問

「Servoライブラリとattach」レッスンは無料ですか?

はい。「Servoライブラリとattach」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Arduino & IoT Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Arduino & IoT Academyコースには全4レッスンが含まれています。

「Servoライブラリとattach」で何を学びますか?

Servoをインクルードし、ピンに割り当てます。 ブラウザで直接実行するハンズオンコードでArduino & IoT Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Arduino & IoT Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのArduino & IoT Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Servoライブラリとattach」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このArduino & IoT Academyレッスンでコードを書いて実行できますか?

はい。すべてのArduino & IoT Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. サーボが角度を認識する仕組み
  2. Servoライブラリとattach
  3. 0度から180度までスイープする
  4. つまみで操作するサーボ
← Arduino & IoT Academyに戻る