Arduino 编程开发

精选 Arduino Programming 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。 助力嵌入式硬件快速开发与调试。

#🚀 入门指引

#准备必要材料 (Gather Necessary Materials)

  • Arduino 开发板:任何型号,如 Arduino Uno、Mega、Nano 等。
  • USB 连接线:与您的 Arduino 开发板匹配(Uno 常用的为 USB Type-A 转 Type-B 接口)。
  • 计算机:Windows、macOS 或 Linux 系统。

#安装 Arduino IDE

  • Windows / macOS / Linux
  • 访问 Arduino 软件官方下载页面。
  • 下载对应操作系统版本的安装包。
  • 运行安装程序并按照提示提示完成安装。

#连接 Arduino 开发板

  • 使用 USB 连接线将 Arduino 开发板连接至计算机。
  • Arduino 板上的电源 LED 灯应当亮起,表明已正常通电。

#配置 Arduino IDE

  • 打开 Arduino IDE。

  • 选择开发板型号:

  • 打开菜单“工具 > 开发板”(Tools > Board),选择对应的 Arduino 板型号(例如 Arduino Uno)。

  • 选择端口:

  • 打开菜单“工具 > 端口”(Tools > Port),选择 Arduino 所连接的端口(Windows 通常显示为 COMx,macOS 显示为 /dev/cu.usbmodemxxxx,Linux 显示为 /dev/ttyUSBx)。

#基础代码结构 (Basic Structure)

#初始化与主循环 (Setup & Loop)

void setup() {
  // 此处代码仅在开机或复位时执行一次
}

void loop() {
  // 此处代码将无限重复循环执行
}

#注释 (Comment)

// 单行注释

/*
多行
注释
*/

#变量声明 (Variable)

int ledPin = 13; // 整型
float voltage = 5.0; // 浮点型
char letter = 'A'; // 字符型
String text = "Hello"; // 字符串型

#引脚模式设置 (Pin Modes)

pinMode(pin, mode);

#数字量输入/输出 (Digital I/O)

digitalWrite(pin, value);
int value = digitalRead(pin);

#模拟量输入/输出 (Analog I/O)

analogWrite(pin, value);
int value = analogRead(pin);

#串口通信 (Serial Communication)

#开启串口通信 (Begin Serial)

Serial.begin(baudRate);

#打印至串口监视器 (Print to Serial)

Serial.print(data);
Serial.println(data);

#从串口监视器读取 (Read from Serial)

if (Serial.available()) {
  char data = Serial.read();
}

#控制结构

#If 条件语句

if (condition) {
  // 条件为真时执行的代码
} else {
  // 条件为假时执行的代码
}

#For 循环

for (initialization; condition; increment) {
  // 循环执行的代码
}

#While 循环

while (condition) {
  // 条件成立时循环执行的代码
}

#自定义函数 (Functions)

returnType functionName(parameters) {
  // 函数内部代码
  return value;
}

#函数库引入 (Libraries)

#引入头文件 (Include Library)

#include <LibraryName.h>

#使用函数库示例 (Using a Library)

#include <Servo.h>

Servo myServo;

void setup() {
  myServo.attach(9); // 将舵机控制线连接至引脚 9
}

void loop() {
  myServo.write(90); // 设置舵机转动至 90 度
  delay(1000);
  myServo.write(0); // 设置舵机转动至 0 度
  delay(1000);
}

#常用核心函数 (Common Functions)

#毫秒延时 (Delay)

delay(milliseconds);

#获取开机时间 (Millis)

unsigned long currentTime = millis();

#数值映射 (Map)

long outputValue = map(inputValue, fromLow, fromHigh, toLow, toHigh);

#随机数生成 (Random)

long randomValue = random(max);
long randomValue = random(min, max);

#随机数种子 (Random Seed)

randomSeed(analogRead(0)); // 以模拟引脚 0 的读数作为种子

#传感器与模块 (Sensors & Modules)

#使用 DHT 传感器读取温湿度

#include <DHT.h>

#define DHTPIN 2// 连接 DHT 传感器的引脚
#define DHTTYPE DHT11   // 传感器型号 DHT 11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");

  delay(2000);
}

#LED 闪烁示例 (Blinking LED)

const int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);  // 点亮 LED
  delay(1000);                 // 等待 1 秒
  digitalWrite(ledPin, LOW);   // 熄灭 LED
  delay(1000);                 // 等待 1 秒
}

#读取按键输入 (Read Button Input)

const int buttonPin = 2;  // 按键连接的引脚
const int ledPin = 13;    // LED 连接的引脚

void setup() {
  pinMode(buttonPin, INPUT);  // 将按键引脚设为输入
  pinMode(ledPin, OUTPUT);    // 将 LED 引脚设为输出
}

void loop() {
  int buttonState = digitalRead(buttonPin);  // 读取按键状态

  if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);  // 点亮 LED
  } else {
    digitalWrite(ledPin, LOW);   // 熄灭 LED
  }
}


#模拟量输入控制 (Analog Input)

const int potPin = A0;    // 电位器连接的引脚
const int ledPin = 9;     // LED 连接的引脚

void setup() {
  pinMode(ledPin, OUTPUT);  // 将 LED 引脚设为输出
}

void loop() {
  int potValue = analogRead(potPin);  // 读取电位器数值
  int ledBrightness = map(potValue, 0, 1023, 0, 255);  // 将 0-1023 映射至 0-255 亮度范围

  analogWrite(ledPin, ledBrightness);  // 输出 PWM 调节 LED 亮度
  delay(10);  // 微小延时使亮度过渡平滑
}


#串口打印示例 (Serial Communication)

void setup() {
  Serial.begin(9600);  // 开启波特率为 9600 的串口通信
}

void loop() {
  Serial.println("Hello, world!");  // 向串口监视器发送字符串
  delay(1000);  // 等待 1 秒
}


#温度传感器读取 (Temperature Sensor)

const int tempPin = A0;  // TMP36 传感器连接的引脚

void setup() {
  Serial.begin(9600);  // 开启 9600 波特率串口通信
}

void loop() {
  int tempValue = analogRead(tempPin);  // 读取传感器原始数值
  float voltage = tempValue * (5.0 / 1023.0);  // 换算为电压值
  float temperatureC = (voltage - 0.5) * 100;  // 将电压值换算为摄氏度

  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.println(" C");
  delay(1000);  // 等待 1 秒
}


#RGB 三色 LED 控制 (RGB LED Control)

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

const int potRedPin = A0;
const int potGreenPin = A1;
const int potBluePin = A2;

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  int redValue = analogRead(potRedPin);
  int greenValue = analogRead(potGreenPin);
  int blueValue = analogRead(potBluePin);

  analogWrite(redPin, map(redValue, 0, 1023, 0, 255));
  analogWrite(greenPin, map(greenValue, 0, 1023, 0, 255));
  analogWrite(bluePin, map(blueValue, 0, 1023, 0, 255));

  delay(10);
}

#超声波测距传感器 (Ultrasonic Sensor)

const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration;
  int distance;

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(1000);
}

#控制舵机电机 (Controlling a Servo Motor)

#include <Servo.h>

Servo myServo;
const int potPin = A0;

void setup() {
  myServo.attach(9);  // 将舵机控制线附加至引脚 9
}

void loop() {
  int potValue = analogRead(potPin);  // 读取电位器数值
  int angle = map(potValue, 0, 1023, 0, 180);  // 将数值映射为 0 至 180 角度

  myServo.write(angle);  // 设置舵机转动角度
  delay(15);  // 微小延时等待舵机就位
}

#在 LCD 屏幕上显示文本 (Displaying Text on LCD)

#include <LiquidCrystal.h>

// 根据接口引脚编号初始化 LiquidCrystal 库
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  // 设置 LCD 屏幕的列数与行数 (16列2行)
  lcd.begin(16, 2);
  // 向 LCD 打印文本
  lcd.print("Hello, World!");
}

void loop() {
  // 将光标设置在第 0 列,第 1 行(第二行)
  lcd.setCursor(0, 1);
  // 打印开机复位后的秒数
  lcd.print(millis() / 1000);
}

#无线模块 NRF24L01 通信

#发射端代码 (Transmitter Code)

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);  // CE, CSN 引脚

const byte address[6] = "00001";  // 地址

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  const char text[] = "Hello";
  radio.write(&text, sizeof(text));
  delay(1000);
}

#接收端代码 (Receiver Code)

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);  // CE, CSN 引脚

const byte address[6] = "00001";  // 地址

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

#🔗 参考资源