본문 바로가기

Embedded/Arduino

초음파센서+브라킷+서보모터

LiquidCrystal i2c 드라이버 설치

 

 

I2C 주소확인

#include <Wire.h>

void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);           
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;
  Serial.println("Scanning...");
  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }

  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
  delay(5000);
}

 

 

테스트 코드 작성

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// 0x27 I2C 주소를 가지고 있는 16x2 LCD객체를 생성합니다.(I2C 주소는 LCD에 맞게 수정해야 합니다.)
LiquidCrystal_I2C lcd(0x27, 16, 2);

// 실행시 가장 먼저 호출되는 함수이며, 최초 1회만 실행됩니다.
// 변수를 선언하거나 초기화를 위한 코드를 포함합니다.
void setup() {
  // I2C LCD를 초기화 합니다..
  lcd.init();
  // I2C LCD의 백라이트를 켜줍니다.
  lcd.backlight();
}

void loop() { 
  lcd.setCursor(0,0);             // 0번째 줄 0번째 셀부터 입력
  lcd.print("  MY LCD TEST!! ");  // 문구를 출력합니다.
  lcd.setCursor(0,1);             // 1번째 줄 0번째 셀부터 입력
  lcd.print("  Hello World!!  "); // 문구 출력
  // 1초간 대기합니다.
  delay(1000);
  // LCD의 모든 내용을 삭제합니다.
  lcd.clear();
  
  lcd.setCursor(0,0);             // 0번째 줄 0번째 셀부터 입력
  lcd.print(" Hello Everyone");  // 문구를 출력합니다.
  lcd.setCursor(0,1);             // 1번째 줄 0번째 셀부터 입력
  lcd.print("Nice to meet you  "); // 문구 출력
  // 1초간 대기합니다.
  delay(1000);
  // LCD의 모든 내용을 삭제합니다.
  lcd.clear();
  delay(500);
}

서보+초음파+LCD 융합 코드

#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

// 0x27 I2C 주소를 가지고 있는 16x2 LCD객체를 생성합니다.(I2C 주소는 LCD에 맞게 수정해야 합니다.)
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define trigPin 2
#define echoPin 3
long duration;
int distance;
int pos = 0;
Servo servo_9;
void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  servo_9.attach(9, 500, 2500);
  // I2C LCD를 초기화 합니다..
  lcd.init();
  // I2C LCD의 백라이트를 켜줍니다.
  lcd.backlight();
  Serial.begin(9600);
}
void loop()
{
  lcd.setCursor(0,0);             // 0번째 줄 0번째 셀부터 입력
  for (pos = 0; pos <= 180; pos += 45) {
    // tell servo to go to position in variable 'pos'
    servo_9.write(pos);
    digitalWrite(trigPin, LOW);    
    delayMicroseconds(5);
    // Trigger the sensor by setting the trigPin high for 10 microseconds:
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
    duration = pulseIn(echoPin, HIGH);
    // Calculate the distance:
    distance = duration * 0.034 / 2;
    // Print the distance on the Serial Monitor (Ctrl+Shift+M):
    Serial.print("Distance = ");
    Serial.print(distance);
    Serial.println(" cm");
    lcd.print("Dist = ");
    lcd.print(distance);
    lcd.print(" cm");
    delay(1000);
    lcd.clear();
  }
  for (pos = 180; pos >= 0; pos -= 45) {
    // tell servo to go to position in variable 'pos'
    servo_9.write(pos);
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    // Trigger the sensor by setting the trigPin high for 10 microseconds:
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
    duration = pulseIn(echoPin, HIGH);
    // Calculate the distance:
    distance = duration * 0.034 / 2;
    // Print the distance on the Serial Monitor (Ctrl+Shift+M):
    Serial.print("Distance = ");
    Serial.print(distance);  
    Serial.println(" cm");
    lcd.print("Dist = ");
    lcd.print(distance);
    lcd.print(" cm");
    delay(1000);
    lcd.clear();
  }
}

'Embedded > Arduino' 카테고리의 다른 글

02. Tinkercad 시작하기  (0) 2021.06.09
01. Arduino 설치하기  (0) 2021.06.09
Servo 제어  (0) 2020.11.21
CH08 -1) 푸시버튼 사용하기 - II  (0) 2020.10.23
CH07 -1) 푸시버튼으로 LED제어  (0) 2020.10.12