Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Turn Counter with Ultrasonic Sensor and LCD Display

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Turn Counter with Ultrasonic Sensor and LCD Display

Circuit Documentation

Summary

This circuit is designed to count the number of turns made by an object using an ultrasonic sensor and display the count on a 16x2 I2C LCD. The circuit also includes a pushbutton to reset the count and a solid-state relay (SSR) to control a DC motor. The Arduino UNO microcontroller is used to control the entire system.

Component List

  1. DC Motor

    • Description: A motor that converts electrical energy into mechanical energy.
    • Pins: pin 1, pin 2
  2. 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
  3. HC-SR04 Ultrasonic Sensor

    • Description: A sensor that measures distance by using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  4. SSR-40A

    • Description: A solid-state relay used to control high-power devices.
    • Pins: -, +, Lin, Lout
  5. POWER SUPPLY 12V 5AMP

    • Description: A power supply that provides 12V DC output.
    • Pins: 220V Positive Pole (AC), 220V Negative Pole (AC), GND, GND (DC), 12V-24V Output (DC)
  6. Pushbutton

    • Description: A simple pushbutton switch.
    • Pins: Pin 2, Pin 1, Pin 3, Pin 4
  7. 16x2 I2C LCD

    • Description: A 16x2 character LCD with I2C interface.
    • Pins: GND, VCC, SDA, SCL
  8. Ac Supply

    • Description: An AC power supply.
    • Pins: +ve, -ve

Wiring Details

DC Motor

Arduino UNO

HC-SR04 Ultrasonic Sensor

SSR-40A

POWER SUPPLY 12V 5AMP

Pushbutton (first instance)

Pushbutton (second instance)

16x2 I2C LCD

Ac Supply

Code Documentation

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Pin definitions for Ultrasonic Sensor and LCD
const int trigPin = 9;       // Ultrasonic sensor trigger pin
const int echoPin = 10;      // Ultrasonic sensor echo pin
const int resetButton = 8;   // Reset button pin

// Pin definitions for the Relay and Button
const int buttonPin = 2;     // Button Pin
const int relayPin = 11;      // Relay Pin (Solid State Relay)

const int maxTurns = 10;      // Maximum number of turns before stopping
int turnCount = 0;            // Counter for the number of turns
bool objectDetected = false;   // Flag to track if an object is currently detected

long duration;                 // Duration of the ultrasonic pulse
int distance;                  // Distance measurement
int buttonState = 0;          // To store the state of the reset button

// States for button and relay control
bool lightON = false;         // Relay State
bool pushed = false;          // Button Toggle State

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

void setup() {
    // Pin Modes
    pinMode(trigPin, OUTPUT);          // Set the trigger pin as output
    pinMode(echoPin, INPUT);           // Set the echo pin as input
    pinMode(resetButton, INPUT_PULLUP); // Set the reset button as input with an internal pull-up resistor
    pinMode(buttonPin, INPUT_PULLUP);   // Set button as input with pull-up
    pinMode(relayPin, OUTPUT);          // Set relay as output

    // LCD Initialization
    lcd.init();                         // Initialize the LCD
    lcd.backlight();
    lcd.setCursor(0, 0);
    lcd.print("Turn Counter");
    lcd.setCursor(0, 1);
    lcd.print("Turns: ");
    lcd.print(turnCount);
    
    Serial.begin(9600);                // For debugging purposes
}

void loop() {
    // Check the reset button state
    buttonState = digitalRead(resetButton);
    if (buttonState == LOW) {
        // Reset the turn counter when the button is pressed
        turnCount = 0;
        objectDetected = false; // Reset object detection flag

        // Update the LCD display after reset
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Turn Counter");
        lcd.setCursor(0, 1);
        lcd.print("Turns: ");
        lcd.print(turnCount);
        
        delay(500); // Debounce delay for button press

        // Turn off the relay when the reset button is pressed
        digitalWrite(relayPin, LOW); // Relay OFF (SSR is on with HIGH)
        lightON = false;
    }

    // Only count if turnCount is less than maxTurns
    if (turnCount < maxTurns) {
        // Measure distance using the ultrasonic sensor
        digitalWrite(trigPin, LOW);
        delayMicroseconds(2);
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);

        // Calculate the distance
        duration = pulseIn(echoPin, HIGH);
        distance = duration * 0.034 / 2;

        Serial.print("Distance: ");
        Serial.print(distance);
        Serial.println(" cm");

        // If an object is detected within a certain range (less than 10 cm) and hasn't been counted yet
        if (distance < 10 && !objectDetected) {