728x90
아두이노 프로그래밍은 C/C++ 언어를 기반으로 하며, 다양한 하드웨어와 센서, 액추에이터를 제어할 수 있도록 설계되었습니다. 다음은 아두이노의 기본 문법과 몇 가지 예제 코드입니다.
기본 문법
1. 변수 선언
int ledPin = 13; // 정수형 변수 선언 및 초기화
float temperature = 23.5; // 부동 소수점 변수 선언
char letter = 'A'; // 문자형 변수 선언
2. 함수 정의
아두이노 스케치는 두 개의 주요 함수로 구성됩니다:
setup()
: 프로그램 시작 시 한 번만 실행됩니다. 보통 초기화 작업을 수행합니다.loop()
: 프로그램이 반복적으로 실행됩니다. 주로 메인 로직이 여기에 위치합니다.
void setup() {
// 초기화 코드
}
void loop() {
// 반복 실행되는 코드
}
3. 제어문
조건문 (if, else)
int sensorValue = analogRead(A0);
void setup() {
Serial.begin(9600);
}
void loop() {
if (sensorValue > 500) {
Serial.println("Value is high");
} else {
Serial.println("Value is low");
}
delay(1000); // 1초 대기
}
반복문 (for, while)
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 10; i++) {
Serial.println(i); // 0부터 9까지 출력
delay(500); // 0.5초 대기
}
int count = 0;
while (count < 10) {
Serial.println(count); // 0부터 9까지 출력
count++;
delay(500); // 0.5초 대기
}
}
예제 코드
1. LED 깜빡이기
LED를 깜빡이는 가장 기본적인 예제입니다. 아두이노 보드의 내장 LED를 제어합니다.
int ledPin = 13; // 내장 LED 핀
void setup() {
pinMode(ledPin, OUTPUT); // LED 핀을 출력 모드로 설정
}
void loop() {
digitalWrite(ledPin, HIGH); // LED 켜기
delay(1000); // 1초 대기
digitalWrite(ledPin, LOW); // LED 끄기
delay(1000); // 1초 대기
}
2. 온도 센서 (LM35) 읽기
LM35 온도 센서의 값을 읽어 시리얼 모니터에 출력합니다.
int sensorPin = A0; // LM35 센서 핀
void setup() {
Serial.begin(9600); // 시리얼 통신 시작
}
void loop() {
int sensorValue = analogRead(sensorPin); // 센서 값 읽기
float voltage = sensorValue * (5.0 / 1023.0); // 전압 변환
float temperature = voltage * 100; // 온도로 변환
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
delay(1000); // 1초 대기
}
3. 서보 모터 제어
서보 모터의 각도를 제어하는 예제입니다.
#include <Servo.h>
Servo myServo; // 서보 객체 생성
void setup() {
myServo.attach(9); // 서보 모터 핀 설정
}
void loop() {
for (int pos = 0; pos <= 180; pos++) { // 0도부터 180도까지 이동
myServo.write(pos); // 각도 설정
delay(15); // 서보 모터가 움직일 시간 대기
}
for (int pos = 180; pos >= 0; pos--) { // 180도부터 0도까지 이동
myServo.write(pos); // 각도 설정
delay(15); // 서보 모터가 움직일 시간 대기
}
}
4. 유무선 통신 예제
시리얼 통신을 통해 데이터 송수신하기
void setup() {
Serial.begin(9600); // 시리얼 통신 시작
}
void loop() {
if (Serial.available()) {
char received = Serial.read(); // 수신된 문자 읽기
Serial.print("Received: ");
Serial.println(received); // 수신된 문자 출력
}
}
Bluetooth 모듈을 통해 데이터 송수신하기
#include <SoftwareSerial.h>
SoftwareSerial bluetooth(10, 11); // RX, TX 핀 설정
void setup() {
bluetooth.begin(9600); // Bluetooth 통신 시작
}
void loop() {
if (bluetooth.available()) {
char received = bluetooth.read(); // 수신된 문자 읽기
bluetooth.print("Received: ");
bluetooth.println(received); // 수신된 문자 출력
}
}
5. 조도 센서 (LDR) 읽기
조도 센서의 값을 읽어 시리얼 모니터에 출력합니다.
int ldrPin = A0; // LDR 핀
void setup() {
Serial.begin(9600); // 시리얼 통신 시작
}
void loop() {
int ldrValue = analogRead(ldrPin); // LDR 값 읽기
Serial.print("LDR Value: ");
Serial.println(ldrValue); // LDR 값 출력
delay(1000); // 1초 대기
}
결론
아두이노는 기본적인 프로그래밍 개념과 하드웨어 인터페이스를 통해 다양한 전자 프로젝트를 구현할 수 있는 강력한 도구입니다. 위의 예제들을 통해 아두이노의 기본 문법과 사용법을 익히고, 이를 바탕으로 더 복잡한 프로젝트를 시도해볼 수 있습니다.
728x90
반응형
'Hardware > 아두이노' 카테고리의 다른 글
아두이노 시작하기 - 소프트웨어 (0) | 2024.08.03 |
---|---|
아두이노 시작하기 - 액추에이터 (0) | 2024.08.03 |
아두이노 시작하기 - 통신 (0) | 2024.07.31 |
아두이노 시작하기 - 디스플레이 2 (0) | 2024.07.31 |
아두이노 시작하기 - 디스플레이 1 (0) | 2024.07.31 |