Arduino Project
Finger Count 3.0
Course Introduction
In this lesson, we control a servo to open the gate using the gesture password 5-1-5, and close it with the gesture 4, based on data received via serial communication.
Red and green LEDs indicate the gate status: by default, the gate is closed with the red LED on; when the correct password is entered, the gate opens and the green LED lights up.
This code controls the servo and LEDs on an Arduino Uno by receiving finger count data from a Python script, which detects the number of fingers shown to a camera and sends the data to the Arduino via serial communication.
Note
If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.
1.1 Install Arduino IDE(Important)
1.2 Introduction of Arduino IDE
Python Installation Guide
Wiring

Common Connections:
LED
Connect the LEDs cathode to a 1kΩ resistor then to the negative power bus on the breadboard, and the LEDs anode to the 2 to 3 on the Arduino.
Digital Servo Motor
Connect to breadboard’s positive power bus.
Connect to breadboard’s negative power bus.
Connect to 9 on the Arduino.
Writing the Code
Note
You can copy this code into Arduino IDE.
Use the Arduino Library Manager and search for Servo.h install library.
Select the board(Arduino UNO R4 Minima) and then clicking the Upload button.
Then use the Python code FingerCountSender (2) . You can click here FingerCountSender (2).zip to download it.
Update the Python script to use the correct serial port(COMx).
Then run the python code to open the camera window.
#include
// LED pins
const int redLedPin = 2;
const int greenLedPin = 3;
// Servo object and pin
Servo gateServo;
const int servoPin = 9;
// Servo angle definitions
const int GATE_CLOSED = 90;
const int GATE_OPEN = 0;
// Array to store gesture input sequence
int gestureSequence[3] = {-1, -1, -1}; // Initially empty
const int expectedSequence[3] = {5, 1, 5}; // Password sequence
bool gateOpen = false;
void setup() {
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
digitalWrite(redLedPin, HIGH); // Default: red LED on
digitalWrite(greenLedPin, LOW);
gateServo.attach(servoPin);
gateServo.write(GATE_CLOSED); // Default: gate closed
Serial.begin(115200);
Serial.setTimeout(1);
}
void loop() {
// Listen for serial data
if (Serial.available() > 0) {
int value = Serial.readString().toInt();
// Only accept values from 0 to 5 (finger count)
if (value >= 0 && value <= 5) {
updateGestureSequence(value);
// Check if gesture matches 5-1-5
if (isCorrectSequence()) {
openGate();
}
}
}
}
// Update gesture input sequence array
void updateGestureSequence(int newValue) {
gestureSequence[0] = gestureSequence[1];
gestureSequence[1] = gestureSequence[2];
gestureSequence[2] = newValue;
}
// Check if the input sequence matches the expected password
bool isCorrectSequence() {
for (int i = 0; i < 3; i++) {
if (gestureSequence[i] != expectedSequence[i]) {
return false;
}
}
return true;
}
void openGate() {
if (!gateOpen) {
Serial.println("✅ Correct gesture password. Gate opening.");
gateServo.write(GATE_OPEN);
digitalWrite(greenLedPin, HIGH);
digitalWrite(redLedPin, LOW);
gateOpen = true;
}
}
