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

Arduino UNO Controlled Dual Servo Motor System with Button Activation

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Dual Servo Motor System with Button Activation

Circuit Documentation

Summary

This circuit is designed to control two servos using an Arduino UNO and a push button. The servos start at position 0. When the button is pressed, the servos rotate to 150 degrees and then return to 0 at half the speed. If the button is pressed multiple times within 1 second, the cycle operates multiple times accordingly.

Component List

  1. Arduino UNO

    • Description: 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 (x2)

    • Description: Standard servo motor.
    • Pins: gnd, vcc, pulse
  3. 2Pin Push Switch

    • Description: Simple push button switch.
    • Pins: Input +, Output +
  4. 7805

    • Description: Voltage regulator to convert 12V to 5V.
    • Pins: Vin, Gnd, Vout
  5. Ceramic Capacitor

    • Description: Capacitor with a capacitance of 0.1 µF.
    • Pins: pin0, pin1
  6. Electrolytic Capacitor

    • Description: Capacitor with a capacitance of 1 µF.
    • Pins: -, +
  7. Resistor

    • Description: Resistor with a resistance of 10 kΩ.
    • Pins: pin1, pin2
  8. 12V Power Supply

    • Description: Power supply providing 12V.
    • Pins: +, -

Wiring Details

Arduino UNO

Servo (first)

Servo (second)

2Pin Push Switch

7805

Ceramic Capacitor

Electrolytic Capacitor

Resistor

12V Power Supply

Code Documentation

// This Arduino sketch controls two servos using a button. The servos start at
// position 0. When the button is pressed, the servos rotate to 150 degrees
// and then return to 0 at half the speed. If the button is pressed twice
// within 1 second, the cycle operates 2 times. If the button is pressed
// three times within 1 second, the cycle operates 3 times.

#include <Servo.h>

#define BUTTON_PIN 2
#define SERVO_PIN_1 6
#define SERVO_PIN_2 7

Servo servo1;
Servo servo2;

unsigned long lastPressTime = 0;
int pressCount = 0;

void setup() {
  pinMode(BUTTON_PIN, INPUT);
  servo1.attach(SERVO_PIN_1);
  servo2.attach(SERVO_PIN_2);
  servo1.write(0); // Initialize servos to position 0
  servo2.write(0); // Initialize servos to position 0
  delay(500); // Wait for servos to reach position 0
  servo1.detach();
  servo2.detach();
}

void loop() {
  if (digitalRead(BUTTON_PIN) == HIGH) {
    unsigned long currentTime = millis();
    if (currentTime - lastPressTime < 1000) {
      pressCount++;
    } else {
      pressCount = 1;
    }
    lastPressTime = currentTime;
    delay(50); // Debounce delay
    while (digitalRead(BUTTON_PIN) == HIGH);
    delay(50); // Debounce delay
  }

  if (pressCount > 0 && millis() - lastPressTime > 1000) {
    for (int i = 0; i < pressCount; i++) {
      servo1.attach(SERVO_PIN_1);
      servo2.attach(SERVO_PIN_2);
      
      // Move servos to position 0 before starting the sweep
      servo1.write(0);
      servo2.write(0);
      delay(500); // Wait for servos to reach position 0

      servo1.write(150);
      servo2.write(150);
      delay(300); // Wait for servos to reach position 150
      for (int pos = 150; pos >= 0; pos--) {
        servo1.write(pos);
        servo2.write(pos);
        delay(30); // Half the speed of moving to 150
      }
      servo1.detach();
      servo2.detach();
    }
    pressCount = 0;
  }
}

This code initializes the servos to position 0 and waits for a button press. Depending on the number of button presses within 1 second, the servos will rotate to 150 degrees and return to 0 at half the speed. The cycle repeats based on the number of button presses detected.