IR sensor people counterTinkerPieCircuit tutorial 20 of 24 · Intermediate · 20 minutes
Use an infrared obstacle sensor to count how many times something passes in front of it.
Open this circuit in TinkerPieCircuit (free, runs in your browser)
What you will learn
- How an IR obstacle sensor works
- Detecting a change (an edge) in code
- Using variables to count
Parts
Steps
Wire the sensor
VCC →
3V3, GND →GND, OUT →P10.Wire the LED
S1→ resistor → LED (+), LED (−) →GND.Read the sensor
The sensor output is LOW when something is in front.
digitalRead(P10) == LOWmeans "blocked".Count once
Keep a variable
wasBlocked. Count only when the sensor changes from clear to blocked.Test
Press ▶ Start. Click the orange OBSTACLE switch on the sensor twice. The Serial monitor shows the count.
The code
let count = 0;
let wasBlocked = false;
function setup() {
pinMode(P10, INPUT);
pinMode(S1, OUTPUT);
}
function loop() {
const blocked = digitalRead(P10) == LOW;
digitalWrite(S1, blocked ? HIGH : LOW);
if (blocked && !wasBlocked) {
count = count + 1;
Serial.println("People counted: " + count);
tone(BUZZER, 880);
delay(80);
noTone(BUZZER);
}
wasBlocked = blocked;
delay(20);
}
Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.
How it works
The IR sensor sends invisible light and sees it bounce back from an object. The module pulls its output LOW when it sees a reflection. Counting only the change stops one person being counted many times.
← Car parking sensor · All 24 circuit tutorials · Automatic door with IR and a servo →