-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathultrasonic_driver.ino
More file actions
105 lines (92 loc) · 2.61 KB
/
ultrasonic_driver.ino
File metadata and controls
105 lines (92 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
ultrasonic_driver.ino
Listens for ASCII serial commands 'buzz_on' and 'buzz_off' and drives a piezo ultrasonic
transducer at 23 kHz using PWM.
Supported boards:
- Arduino Uno / Nano / Mega (uses tone())
- ESP32 (uses LEDC hardware PWM)
Wiring:
- Connect piezo + to BUZZ_PIN (see defines below) and piezo - to GND.
- For louder output, use a transistor/MOSFET driver and external power per the piezo datasheet.
- Do NOT drive high-power transducers directly from the MCU pin.
Serial:
- 115200 baud by default. Commands end with a newline ('\n').
- Commands:
buzz_on -> start 23 kHz tone
buzz_off -> stop tone
*/
// ---------- Configuration ----------
#define ULTRASONIC_FREQ_HZ 23000 // 23 kHz target frequency
#if defined(ESP32)
// ESP32 pin selection (GPIO 25 is DAC-capable and commonly free)
#define BUZZ_PIN 25
#define LEDC_CHANNEL 0
#define LEDC_RES_BITS 8
#else
// Arduino AVR boards (e.g., Uno): use a PWM-capable pin (tone() can use any digital pin)
#define BUZZ_PIN 9
#endif
// ---------- Globals ----------
String inputLine;
bool buzzing = false;
void setup() {
pinMode(BUZZ_PIN, OUTPUT);
stopBuzz();
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect (needed for native USB boards)
}
Serial.setTimeout(10);
Serial.println(F("ultrasonic_driver ready. Send 'buzz_on' or 'buzz_off'."));
}
void loop() {
// Read line-buffered commands
while (Serial.available()) {
char c = (char)Serial.read();
if (c == '\r') continue;
if (c == '\n') {
processCommand(inputLine);
inputLine = "";
} else {
inputLine += c;
if (inputLine.length() > 64) {
inputLine.remove(0); // prevent runaway
}
}
}
}
void processCommand(const String &cmd) {
if (cmd.equalsIgnoreCase("buzz_on")) {
startBuzz();
Serial.println(F("OK buzz_on"));
} else if (cmd.equalsIgnoreCase("buzz_off")) {
stopBuzz();
Serial.println(F("OK buzz_off"));
} else if (cmd.length() > 0) {
Serial.print(F("ERR unknown: "));
Serial.println(cmd);
}
}
void startBuzz() {
if (buzzing) return;
#if defined(ESP32)
// Configure LEDC PWM at 23 kHz
ledcSetup(LEDC_CHANNEL, ULTRASONIC_FREQ_HZ, LEDC_RES_BITS);
ledcAttachPin(BUZZ_PIN, LEDC_CHANNEL);
// 50% duty for square wave: for 8-bit resolution, 127 or 128
ledcWrite(LEDC_CHANNEL, 128);
#else
// Use Arduino tone() which uses a hardware timer
tone(BUZZ_PIN, ULTRASONIC_FREQ_HZ);
#endif
buzzing = true;
}
void stopBuzz() {
#if defined(ESP32)
ledcWrite(LEDC_CHANNEL, 0);
ledcDetachPin(BUZZ_PIN);
#else
noTone(BUZZ_PIN);
#endif
buzzing = false;
}