IOT

아두이노[2]

Break-Limits 2023. 7. 7. 18:18

서보모터 제어


기본 설정


#  일반적으로 서보 모터는 0부터 180 사이의 각도 값을 받습니다.

 

 

 

특정 각도 입력하여 서보모터 제어 


# 시리얼 모니터로 특정 각도를 입력하면 입력 각도로 서보모터 위치가 제어됨.

 

[아두이노 배선]

[코드 작성]

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object
  myservo.write(0);   // 초기 서보 position angle = 0
}

void loop() {
  if (Serial.available() > 0) {
    int pos = Serial.parseInt();
    Serial.println(pos);
    myservo.write(pos);
    delay(1000);
  }
}

 

 

서보 위치각 변화에 딜레이 추가

 

[코드 작성]

 

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int pos = 0;
int beforePos = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(8);  // attaches the servo on pin 8 to the servo object
  // myservo.write(0);
}

void loop() {
  if (Serial.available() > 0) {
    beforePos = pos;
    pos = Serial.parseInt();

    Serial.print("before :");
    Serial.println(beforePos);
    Serial.print("current :");
    Serial.println(pos);

    if(beforePos <= pos){
      for(int i = beforePos; i <= pos; i +=1){
        // Serial.println(i);
        myservo.write(i);
        delay(10);
      }
    }else{
      for(int i = beforePos; i >= pos; i -=1){
        // Serial.println(i);
        myservo.write(i);
        delay(10);
      }
    }
    Serial.parseInt();
    // myservo.write(0);
  }
}

 

서보모터 방향제어

 

int motorA1 = A0;
int motorA2 = A1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(motorA1, OUTPUT);
  pinMode(motorA2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available()>0){
    int speed = Serial.parseInt();
    Serial.println(speed);
    analogWrite(motorA1,speed);
    analogWrite(motorA2,0);
    delay(5000);

    analogWrite(motorA1,0);
    analogWrite(motorA2,speed);
    delay(5000);

    analogWrite(motorA1,0);
    analogWrite(motorA2,0);
  }
  
}

https://www.circuitbasics.com/how-to-control-dc-motor-speed-and-direction-using-l293d-and-arduino/

 

 

 

릴레이


 

릴레이란? https://gdnn.tistory.com/45

 

[아두이노 배선]

 

 

 

 

[코드 작성]

#include <dht11.h>

int pin = 5;
int DHT11PIN = 7;

dht11 DHT11;

void setup() {
  Serial.begin(9600);
  pinMode(pin, OUTPUT);
  digitalWrite(pin, HIGH);
}

void loop() {
  Serial.println();

  int chk = DHT11.read(DHT11PIN);

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);
  Serial.print("Temperature  (C): ");
  Serial.println((float)DHT11.temperature, 2);

  delay(2000);

  if(DHT11.temperature >= 30){
    digitalWrite(pin, LOW);
  } else {
    digitalWrite(pin, HIGH);
    }
}