注釈

こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。

参加する理由は?

  • エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。

  • 学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。

  • 独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。

  • 特別割引:最新製品の独占割引をお楽しみください。

  • 祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。

👉 私たちと一緒に探索し、創造する準備はできていますか?[ ここ]をクリックして今すぐ参加しましょう!

モーター

概要

このレッスンでは、モーターの使用方法を学びます。モーターの動作原理は、通電されたコイルが磁場内で強制的に回転し、その結果モーターのローターが回転し、ピニオンギアがエンジンのフライホイールを回転させることです。

必要なコンポーネント

このプロジェクトには、以下のコンポーネントが必要です。

全セットを購入するのが便利です。こちらがリンクです:

名称

このキットのアイテム数

リンク

Elite Explorer Kit

300+

Elite Explorer Kit

以下のリンクから個別に購入することもできます。

コンポーネント紹介

購入リンク

Arduino Uno R4 WiFi

-

ブレッドボード

購入

ジャンパーワイヤー

購入

TA6586 - モータードライバーIC

-

DCモーター

購入

電源モジュール

-

配線図

この例では、パワーサプライモジュールを使用してブレッドボードの陽極と陰極に電力を供給します。

注釈

モーターは動作中に多くの電力を必要とするため、使用中は電源モジュールを充電ケーブルに接続したままにしてください。

../_images/24-motor_bb.png

回路図

../_images/24_motor_schematic.png

コード

注釈

  • ファイル 24-motor.inoelite-explorer-kit-main\basic_project\24-motor のパスで直接開くことができます。

  • または、このコードをArduino IDEにコピーしてください。

24-motor.ino
 1/*
 2  The code controls a motor using an Arduino Uno R4 and a TA6586 chip. It 
 3  sets the rotation direction of the motor based on the user's serial input 
 4  ('A' for clockwise and 'B' for anticlockwise). After performing the rotation 
 5  for 3 seconds, the motor is stopped.
 6
 7  Board: Arduino Uno R4
 8  Component: Motor and TA6586 chip
 9*/
10
11// Pin definitions for motor control
12const int motorBI = 10;
13const int motorFI = 9;
14
15// Initialization function
16void setup() {
17  // Set motor control pins as OUTPUT
18  pinMode(motorBI, OUTPUT);
19  pinMode(motorFI, OUTPUT);
20  
21  // Initialize serial communication and prompt user
22  Serial.begin(9600);
23  Serial.println("Please input 'A' or 'B' to select the motor rotate direction.");
24}
25
26// Main loop function
27void loop() {
28  // Check if there is available data on the serial port
29  if (Serial.available() > 0) {
30    int incomingByte = Serial.read(); // Read incoming data
31    
32    // Determine motor direction based on user input
33    switch (incomingByte) {
34      case 'A':
35        clockwise(255); // Rotate motor clockwise
36        Serial.println("The motor rotates clockwise.");
37        break;
38      case 'B':
39        anticlockwise(255); // Rotate motor anticlockwise
40        Serial.println("The motor rotates anticlockwise.");
41        break;
42    }
43  }
44  
45  delay(3000); // Wait for 3 seconds
46  stopMotor(); // Stop the motor
47}
48
49// Function to rotate the motor clockwise
50void clockwise(int Speed) {
51  analogWrite(motorBI, 0);
52  analogWrite(motorFI, Speed);
53}
54
55// Function to rotate the motor anticlockwise
56void anticlockwise(int Speed) {
57  analogWrite(motorBI, Speed);
58  analogWrite(motorFI, 0);
59}
60
61// Function to stop the motor
62void stopMotor() {
63  analogWrite(motorBI, 0);
64  analogWrite(motorFI, 0);
65}

UNOボードにコードをアップロードした後、シリアルモニターで「A」または「B」と入力することで、モーターの回転方向を選択できます。

コード解析

モーターは、モーターの両側の銅板間に電圧差を与えることで駆動できます。 したがって、銅板の一方の側の電圧を0V、もう一方の側を5Vに設定するだけで済みます。書かれたアナログ信号値を変更することで、方向と速度を調整できます。

// Function to rotate the motor clockwise
void clockwise(int Speed) {
  analogWrite(motorBI, 0);
  analogWrite(motorFI, Speed);
}

// Function to rotate the motor anticlockwise
void anticlockwise(int Speed) {
  analogWrite(motorBI, Speed);
  analogWrite(motorFI, 0);
}

この例では、Serial.Read()を使用してモーターの方向を制御しています。

シリアルモニターで「A」と入力すると、clockwise(255)関数が呼び出され、モーターが255の速度で回転します。 「B」と入力すると、モーターは逆方向に回転します。

void loop() {
  // Check if there is available data on the serial port
  if (Serial.available() > 0) {
    int incomingByte = Serial.read(); // Read incoming data

    // Determine motor direction based on user input
    switch (incomingByte) {
      case 'A':
        clockwise(255); // Rotate motor clockwise
        Serial.println("The motor rotates clockwise.");
        break;
      case 'B':
        anticlockwise(255); // Rotate motor anticlockwise
        Serial.println("The motor rotates anticlockwise.");
        break;
    }
  }

  delay(3000); // Wait for 3 seconds
  stopMotor(); // Stop the motor
}