Arduino Project

Finger Count 2.0

Course Introduction

This code controls a series of LEDs on an Arduino Uno based on finger count data received from a Python script. The script detects the number of fingers shown to a camera and sends this data to the Arduino via serial communication.
The Arduino then lights up the corresponding number of LEDs.

The LEDs are connected to digital pins 2 through 6, with the number of lit LEDs corresponding to the finger count detected by the Python script.

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

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima

1

2

USB Type-C cable

1

×

3

Breadboard

1

BUY

4

Wires

Several

5

1kΩ resistor

Several

6

LED

Several

Wiring

7.webp__PID:3b0acd59-fa5d-4cb7-92d7-21c4c59c3f22

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 6 on the Arduino.

Writing the Code

Note
You can copy this code into Arduino IDE.
Select the board(Arduino UNO R4 Minima) and then clicking the Upload button.
Then use the Python code FingerCountSender (3) . You can click here FingerCountSender (3).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.


// Define the LED pins
const int ledPins[] = {2, 3, 4, 5, 6}; // Pins where the LEDs are connected
const int numLeds = 5; // Number of LEDs

void setup() {
  // Initialize the LED pins as outputs
  for (int i = 0; i < numLeds; i++) {
    pinMode(ledPins[i], OUTPUT);
    digitalWrite(ledPins[i], LOW); // Ensure all LEDs are off initially
  }

  // Start serial communication
  Serial.begin(115200);
  Serial.setTimeout(1);
}

void loop() {
  // Check if data is available on the serial port
  if (Serial.available() > 0) {

    // Read the incoming data
    int value = Serial.readString().toInt();

    // Ensure the value is within the range 0-5
    if (value >= 0 && value <= numLeds) {
      // Turn off all LEDs first
      for (int i = 0; i < numLeds; i++) {
        digitalWrite(ledPins[i], LOW);
      }

      // Turn on the appropriate number of LEDs
      for (int i = 0; i < value; i++) {
        digitalWrite(ledPins[i], HIGH);
      }

    }
  }
}