Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino UNO-Based IR Ball Counter with LCD Display and Servo Control

Image of Arduino UNO-Based IR Ball Counter with LCD Display and Servo Control

Circuit Documentation

Summary

This circuit is designed to count the number of balls passing through two IR sensors and display the count on a 16x2 I2C LCD. The circuit also includes a servo motor and a reset button to reset the count. The Arduino UNO microcontroller is used to control the entire setup.

Component List

  1. Arduino UNO

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. Servo

    • Description: A servo motor used for precise control of angular position.
    • Pins: gnd, vcc, pulse
  3. IR Sensor

    • Description: An infrared sensor used to detect the presence of a ball.
    • Pins: out, gnd, vcc
  4. 16x2 I2C LCD

    • Description: A 16x2 character LCD with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  5. Red Pushbutton

    • Description: A pushbutton used to reset the ball count.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4

Wiring Details

Arduino UNO

  • 5V: Connected to VCC of both IR sensors, VCC of the Servo, and VCC of the 16x2 I2C LCD.
  • GND: Connected to GND of both IR sensors, GND of the Servo, and GND of the 16x2 I2C LCD.
  • A4: Connected to SDA of the 16x2 I2C LCD.
  • A5: Connected to SCL of the 16x2 I2C LCD.
  • D12: Connected to out of the first IR sensor.
  • D11: Connected to out of the second IR sensor.
  • D10: Connected to Pin 1 of the Red Pushbutton.
  • D9: Connected to pulse of the Servo.

Servo

  • vcc: Connected to 5V of the Arduino UNO.
  • gnd: Connected to GND of the Arduino UNO.
  • pulse: Connected to D9 of the Arduino UNO.

IR Sensor (First)

  • vcc: Connected to 5V of the Arduino UNO.
  • gnd: Connected to GND of the Arduino UNO.
  • out: Connected to D12 of the Arduino UNO.

IR Sensor (Second)

  • vcc: Connected to 5V of the Arduino UNO.
  • gnd: Connected to GND of the Arduino UNO.
  • out: Connected to D11 of the Arduino UNO.

16x2 I2C LCD

  • VCC: Connected to 5V of the Arduino UNO.
  • GND: Connected to GND of the Arduino UNO.
  • SDA: Connected to A4 of the Arduino UNO.
  • SCL: Connected to A5 of the Arduino UNO.

Red Pushbutton

  • Pin 1: Connected to D10 of the Arduino UNO.
  • Pin 3: Connected to GND of the Arduino UNO.

Documented Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h> // Include the Servo library

// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address 0x27, 16 columns, 2 rows

// IR Sensor Pins
int Ir_In = 11;  // IR sensor for ball IN
int Ir_Out = 12; // IR sensor for ball OUT

// Counters
int count_in = 0;
int count_out = 0;

// Variables to store previous states
int prev_in = HIGH;
int prev_out = HIGH;

// Reset Button
int resetButton = 10; // Reset button connected to pin 10

void setup() {
  // Initialize LCD
  lcd.init();
  lcd.backlight();

  // Set IR sensor pins as input
  pinMode(Ir_In, INPUT);
  pinMode(Ir_Out, INPUT);

  // Set Reset button as input with pull-up resistor
  pinMode(resetButton, INPUT_PULLUP);

  // Display initial message
  lcd.setCursor(0, 0);
  lcd.print("IR Ball Counter");
  delay(2000);
  lcd.clear();
}

void loop() {
  // Check for reset button press
  if (digitalRead(resetButton) == LOW) {
    count_in = 0;
    count_out = 0;
    lcd.clear(); // Clear LCD display
    lcd.setCursor(0, 0);
    lcd.print("Session Reset");
    delay(1000); // Display reset message for 1 second
    lcd.clear(); // Clear the screen again
  }

  // Read sensor values
  int val_in = digitalRead(Ir_In);
  int val_out = digitalRead(Ir_Out);

  // Check ball IN detection
  if (val_in == LOW && prev_in == HIGH) { // Detect transition from HIGH to LOW
    count_in++; // Increment IN counter
    delay(200); // Debounce delay
  }
  prev_in = val_in; // Update previous state

  // Check ball OUT detection
  if (val_out == LOW && prev_out == HIGH) { // Detect transition from HIGH to LOW
    count_out++; // Increment OUT counter
    delay(200);  // Debounce delay
  }
  prev_out = val_out; // Update previous state

  // Display the count on LCD
  lcd.setCursor(0, 0);
  lcd.print("IN: ");
  lcd.print(count_in);
  lcd.print(" OUT: ");
  lcd.print(count_out);

  lcd.setCursor(0, 1);
  lcd.print("Remaining: ");
  lcd.print(count_in - count_out);

  delay(500); // Refresh rate
}

This code initializes the LCD, sets up the IR sensors and the reset button, and continuously monitors the sensors to update the ball count on the LCD. The reset button resets the count when pressed.