Arduino

DC 모터의 가변저항과 스위치 방향 제어하기

작성자 임베디드코리아 작성일25-05-15 23:53 조회385회 댓글0건

첨부파일

동영상 : https://youtu.be/SNpqSMAyonQ
<* DC 모터의 가변저항과 스위치 방향 제어 *>
■  DC모터를 적절히 사용하기 위해서는 동작 전압과 전류, 분당 회전 수(RPM, Revolutions Per Minute), 토크(Torque) 등을 알아야한다.
    - 동작 전압과 전류가 충분하지 않으면 DC모터는 움직이지 않는다.
    - 분당 회전 수(RPM)는 모터의 회전수를 결정하기 때문에 적용 분야에 따라 속도가 중요한 경우가 있다.
    - 토크는 회전력이라고 하며 물체가 회전하는데 드는 힘이다.

---- <  DC 모터의 가변저항과 스위치 방향 제어 소스 코드 > -----------------------
int enablePin = 11;      // enablePin을 11로 설정합니다
int in1Pin = 10;          // in1Pin을 10으로 설정합니다.
int in2Pin = 9;          // in2Pin을 9로 설정합니다.

int convertedPin = A0;
int switchPin = 2;

int convertedValue=0;
int speedValue = 0;
boolean switch_inValue = 0;


void setup()
{
  pinMode(enablePin, OUTPUT);    // enablePin을 출력핀으로 설정합니다.
  pinMode(in1Pin, OUTPUT);        // in1Pin을 출력핀으로 설정합니다.
  pinMode(in2Pin, OUTPUT);        // in2Pin을 출력핀으로 설정합니다.
  pinMode(convertedPin, INPUT);
  pinMode(switchPin, INPUT);
}
void loop()
{
  /* 가변저항의 입력 값 읽기 */
  convertedValue = analogRead(convertedPin);

    /* 입력 값 범위를 0 ~ 255로 변환*/
  speedValue = map(convertedValue, 0, 1023, 0, 255);
  switch_inValue=digitalRead(switchPin);
  setMotor(speedValue, switch_inValue);        // setMotor 함수에 255와 1를 넣어 호출합니다.
  delay(20);              // 2초간 지연합니다.
 
}
void setMotor(int speed, boolean reverse)  // setMotor 함수를 선언합니다.
{                                          // 함수는 (속도,방향)을 입력받습니다.
  analogWrite(enablePin, speed);            // 입력 받은 속도 값을 enablePin핀에 아날로그 출력합니다.
  digitalWrite(in1Pin, ! reverse);          // 입력 받은 방향 값을 반전하여 in1Pin핀에 디지털 출력합니다.
  digitalWrite(in2Pin, reverse);            // 입력 받은 방향 값을 in2Pin핀에 디지털 출력합니다.
}