
No. 10 • Digital
Optical Speed Sensor
นับพัลส์จากช่องแสงเพื่อวัดรอบหรือความเร็ว
Component NameOptical Speed Sensor
Part No. / Model-
Interface TypeDigital
PinsVCC, GND, D0
Note: เหมาะกับ encoder wheel หรือจานเจาะรู
Wiring Example
ตัวอย่างการต่อวงจรกับ Arduino UNO
| Sensor Pin | Arduino Pin |
|---|---|
| VCC | 5V |
| GND | GND |
| D0 | D2 |
Arduino IDE Code
// Optical Speed Sensor
// Board: Arduino UNO
// Count pulses from the optical slot sensor.
const int SENSOR_PIN = 2;
volatile unsigned long pulseCount = 0;
unsigned long lastTime = 0;
void countPulse() {
pulseCount++;
}
void setup() {
Serial.begin(9600);
pinMode(SENSOR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), countPulse, FALLING);
}
void loop() {
if (millis() - lastTime >= 1000) {
noInterrupts();
unsigned long count = pulseCount;
pulseCount = 0;
interrupts();
Serial.print("Pulses per second: ");
Serial.println(count);
lastTime = millis();
}
}