1 軟體 : Arduino ( v1.8.12 ) 會隨版本更新

2 元件及材料 :

ESP 32 、 超音波 、蜂鳴器 、 杜邦線x6、

Micro USB線x1

Arduino

教學 : https://zanrobot.com/uncategorized/3699

下載 : tps://www.arduino.cc/zh/main/software?setlang =cn

超音波蜂鳴器接線圖接

BZ(+) 接P12,
BZ ( – ) 接GND
trigPin 接 5 (GPIO5)
echoPin 接 4 (GPIO4)
Vcc 接 3.3 V

ESP 32 前置

教學 : https://zanrobot.com/uncategorized/3699

程式編碼

const int trigPin = 5;

const int echoPin = 4;

const int ledPin = 12;

const int freq = 2000;

const int ledChannel = 0;

const int resolution = 8;

long duration; int distance;

int safetyDistance;

void setup() { pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

Serial.begin(115200);

ledcSetup(ledChannel, freq, resolution);

ledcAttachPin(ledPin, ledChannel);

ledcWriteTone(ledChannel, 0);

}

void loop()

{ digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

{ distance= duration*0.034/2;

safetyDistance = distance; if (safetyDistance <= 30){ ledcWriteTone(ledChannel,0);

delay(100);

}

if (safetyDistance < 5){ ledcWriteTone(ledChannel,1730);

delay(100);

}

if (safetyDistance < 20){ ledcWriteTone(ledChannel,1046);

delay(100); }

}

Serial.print(“Distance: “);

Serial.println(distance);

}

影片

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>