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

Arduino UNO Controlled RGB LED and Servo Motor System with Push Button Activation

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

Circuit Documentation

Summary

This circuit involves an Arduino UNO microcontroller controlling three LEDs (red, green, and blue), a servo motor, and a momentary push button switch. The LEDs are connected through resistors to limit current, and the servo motor is controlled via a pulse pin. The push button switch is used to trigger actions in the circuit, such as lighting up the LEDs and moving the servo motor.

Component List

  1. LED: Two Pin (red)

    • Pins: cathode, anode
    • Description: Red LED
  2. LED: Two Pin (green)

    • Pins: cathode, anode
    • Description: Green LED
  3. LED: Two Pin (blue)

    • Pins: cathode, anode
    • Description: Blue LED
  4. Servo

    • Pins: gnd, vcc, pulse
    • Description: Servo motor
  5. Resistor

    • Pins: pin1, pin2
    • Description: 200 Ohms resistor
  6. Arduino UNO

    • 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
    • Description: Microcontroller
  7. Momentary Push Button Switch

    • Pins: 1, 3, 2, 4
    • Description: Push button switch

Wiring Details

LED: Two Pin (red)

LED: Two Pin (green)

LED: Two Pin (blue)

Servo

Resistor (200 Ohms)

Resistor (200 Ohms)

Resistor (200 Ohms)

Resistor (200 Ohms)

Arduino UNO

Momentary Push Button Switch

Code Documentation

#include <Servo.h>
#include <Wire.h>
#include <rgb_lcd.h> // Grove LCD library

rgb_lcd lcd; // Create an LCD object

Servo myservo; // Create Servo object to control a servo
int RED1 = 9;
int GREEN1 = 10;
int BLUE1 = 7;
int VIB = 6;
int SWITCH = 5; // Switch connected to pin 5
int VAL;
int pos = 0; // Variable to store the servo position
// Servo in to D4
// LCD (if used?!) in to 12C, the one below D8

//VIB MOTOR:
// IN - 6
//VCC - 5V BUT only if not using the swithc as only on 5V output -- so maybe no can do
//GND - GND

bool switchState = false; // Current state of the switch (pressed or not)
bool lastSwitchState = false; // Previous state of the switch (to detect change)
bool actionRunning = false; // To track if the action is running

void setup() {
  pinMode(RED1, OUTPUT);
  pinMode(GREEN1, OUTPUT);
  pinMode(BLUE1, OUTPUT);
  pinMode(VIB, OUTPUT);
  pinMode(SWITCH, INPUT); // Switch as input
  myservo.attach(4);
  lcd.begin(16, 2); // Initialise LCD
  lcd.clear();
}

void loop() {
  // Read the switch state
  switchState = digitalRead(SWITCH);

  // Check for a change in switch state (from LOW to HIGH or HIGH to LOW)
  if (switchState != lastSwitchState) {
    if (switchState == LOW) {
      // Switch pressed: Start the action
      actionRunning = true;
      //lcd.clear();
      //lcd.setCursor(0, 0);
      //lcd.print("Action Started");
      digitalWrite(VIB, HIGH);
      digitalWrite(RED1, HIGH);
      digitalWrite(BLUE1, HIGH);
      digitalWrite(GREEN1, HIGH);
    } else {
      // Switch released: Stop the action
      actionRunning = false;
      //lcd.clear();
      //lcd.setCursor(0, 0);
      //lcd.print("Action Stopped");
      digitalWrite(VIB, LOW);
      digitalWrite(RED1, LOW);
      digitalWrite(BLUE1, LOW);
      digitalWrite(GREEN1, LOW);
      myservo.write(0); // Reset servo to initial position
    }
    // Update the last switch state to the current one
    lastSwitchState = switchState;
  }

  // Run the servo action only if the switch is pressed and action is running
  if (actionRunning) {
    for (pos = 0; pos <= 9; pos++) { // Move servo forward
      myservo.write(pos);
      delay(5);
    }
    for (pos = 9; pos >= 0; pos--) { // Move servo backward
      myservo.write(pos);
      delay(5);
    }
  }
}

This code controls the LEDs, servo motor, and push button switch. When the switch is pressed, the LEDs light up, and the servo motor moves back and forth. When the switch is released, the LEDs turn off, and the servo motor resets to its initial position.