1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで学ぶ Arduino制御の基本:LEDとサーボモーター編

Last updated at Posted at 2025-04-02

はじめに

本記事では、Pythonを使ってArduino制御の基本であるLEDの点灯・消灯のシミュレーションや、サーボモーターの角度に応じたPWMパルス幅の計算方法をわかりやすく解説します。Arduinoを実際に使わなくても、Pythonでシミュレーションできる形にしているため、初学者の方にもおすすめです。


参考リンクまとめ


Arduinoの仕組みをPythonで理解する:LEDとサーボの統合スクリプト

# ================================================
# Pythonで学ぶ Arduino制御の基本(LED と サーボモーター)
# ================================================

# --- LED 点灯・消灯シミュレーション ---
print(" LED 点灯・消灯のシミュレーション")
led_pin = 13           # LED接続ピン番号
state_on = 1           # HIGH = 1(点灯)
state_off = 0          # LOW = 0(消灯)
delay_time_ms = 1000   # 1000ms = 1秒

print(f"Pin {led_pin} を HIGH({state_on})に → LED 点灯")
print(f"{delay_time_ms} ミリ秒 待機")
print(f"Pin {led_pin} を LOW({state_off})に → LED 消灯")
print(f"{delay_time_ms} ミリ秒 待機\n")


# --- サーボモーターのパルス幅計算関数 ---
def calculate_pulse_width(angle, min_us=500, max_us=2400):
    """
    指定した角度に対するサーボモーターのPWMパルス幅を計算する関数
    Calculate servo PWM pulse width for a given angle
    """
    pulse_width = (angle / 180) * (max_us - min_us) + min_us
    return pulse_width


# --- サーボ角度とパルス幅の関係出力 ---
print(" サーボモーターの角度 → パルス幅対応表")
for angle in [0, 45, 90, 135, 180]:
    pulse = calculate_pulse_width(angle)
    print(f"角度 {angle:3d}° → パルス幅 = {pulse:.1f} μs")
print()


# --- PWM制御の比例式 表示と解説 ---
print(" PWM幅の計算式(比例式)")
print("パルス幅 = (角度 / 180) × (max - min) + min")
print("\n【解説】")
print("・角度は 0〜180° の範囲をとる")
print("・min は角度0°のときのパルス幅(今回: 500μs)")
print("・max は角度180°のときのパルス幅(今回: 2400μs)")
print("・角度が大きくなるほどパルス幅が大きくなる(比例関係)")
print("\n【例】")
print("角度 = 90° のとき:")
print("パルス幅 = (90 / 180) × (2400 - 500) + 500")
print("         = 0.5 × 1900 + 500")
print("         = 950 + 500 = 1450μs\n")


# --- ミリ秒とマイクロ秒の換算 ---
print(" 時間単位の換算")
ms = 1
us = ms * 1000
print(f"{ms} ミリ秒 は {us} マイクロ秒")

print("\n【補足】")
print("・delay(1000) → 1000ミリ秒 = 1秒")
print("・delayMicroseconds(1450) → 1450マイクロ秒 = 0.00145秒")
print("・サーボモーターはミリ秒ではなくマイクロ秒単位のパルス幅で動作制御される\n")


# --- Servo.write(angle) 相当のPWM幅出力 ---
print(" Servo.write(angle) に対応するPWM出力")
angles = [0, 30, 60, 90, 120, 150, 180]
for a in angles:
    pwm = calculate_pulse_width(a)
    print(f"servo.write({a:3d}) → PWM: {pwm:.0f} μs")

print("\n【補足】")
print("・Servo.write(角度) は、指定した角度に応じたパルス幅を内部で自動計算する")
print("・たとえば write(0) → 500μs, write(180) → 2400μs のように対応")
print("・write(90) では中央位置(1450μs付近)を指示する")
print("・このようにマイクロ秒単位でPWM信号を与えることで、サーボの軸がその角度に回転する\n")


結果

実行すると、以下のような出力が得られます(一部抜粋):

 LED 点灯・消灯のシミュレーション
Pin 13 を HIGH(1)に → LED 点灯
1000 ミリ秒 待機
Pin 13 を LOW(0)に → LED 消灯
1000 ミリ秒 待機

 サーボモーターの角度 → パルス幅対応表
角度   0° → パルス幅 = 500.0 μs
角度  45° → パルス幅 = 837.5 μs
角度  90° → パルス幅 = 1175.0 μs
...

おわりに

本記事では、Pythonを活用して、ArduinoにおけるLED制御やサーボモーターのPWM信号制御をシミュレーションする方法をご紹介しました。物理的なハードウェアがなくても、プログラミングを通して動作原理を体験できるのがポイントです。今後は実機と連携する方法も取り上げていきますので、ぜひお楽しみに。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?