10
Optical Speed Sensor
-
Optical Speed Sensor
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

Circuit diagram
Sensor PinArduino Pin
VCC5V
GNDGND
D0D2

Arduino IDE Code

Download .ino file

optical-speed.ino
// 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();
  }
}