Arduino Project

Arduino Automatic Light Switch with Servo Motor

Course Introduction

In this lesson, you will learn how to use Arduino along with servo motor, buttons, LED, and resistor to create a light play game.

Pressing the button to start.

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

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima/Arduino UNO R4 WIFI

1

2

USB Type-C cable

1

×

3

Breadboard

1

4

Wires

Several

5

220Ω resistor

1

6

Button

2

7

LED

1

8

Digital Servo Motor

1

Wiring

4.webp__PID:ad5135a3-d3a0-4d5d-81d7-9e7cbfe7b915

Common Connections:

LED
Connect the LED cathode to the negative power bus on the breadboard, and the LED anode to a 1kΩ resistor then to 5 on the Arduino.

Button-ON
Connect to breadboard’s negative power bus.
Connect to 2 on the Arduino.

Button-OFF
Connect to breadboard’s negative power bus.
Connect to 3 on the Arduino.

Digital Servo Motor
Connect to breadboard’s positive power bus.
Connect to breadboard’s negative power bus.
Connect to 9 on the Arduino.

Writing the Code

Note
You can copy this code into Arduino IDE.
Don’t forget to select the board(Arduino UNO R4 Minima/Minima) and the correct port before clicking the Upload button.


#include 
#include 
#include 
#include 

// OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define OLED_ADDR 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Pins
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int BUZZER_PIN = 5;
const int LED_PIN = 6;

// LED strip
const int LED_COUNT = 8;
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

// Distance settings
const int SAFE_DISTANCE_CM = 20;
const int LED_START_CM = 15;
const int DANGER_DISTANCE_CM = 3;

// Buzzer timing
unsigned long lastBeepTime = 0;
bool buzzerState = false;

// Passive buzzer frequency
const int BUZZER_FREQ = 2000;

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  noTone(BUZZER_PIN);

  strip.begin();
  strip.show();

  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
    while (true);
  }

  display.clearDisplay();
  display.display();
}

void loop() {
  float distanceCM = readDistanceCM();
  int distanceMM = distanceCM * 10;

  updateOLED(distanceMM, distanceCM);
  updateLEDStrip(distanceCM);
  updateBuzzer(distanceCM);

  delay(50);
}

float readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);

  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  long duration = pulseIn(ECHO_PIN, HIGH, 30000);

  if (duration == 0) {
    return 999;
  }

  return duration * 0.0343 / 2.0;
}

void updateOLED(int distanceMM, float distanceCM) {
  display.clearDisplay();

  display.setTextColor(SSD1306_WHITE);

  display.setTextSize(1);
  display.setCursor(0, 0);
  display.print("Reverse Radar");

  display.setTextSize(2);
  display.setCursor(0, 22);

  if (distanceCM > SAFE_DISTANCE_CM) {
    display.print("SAFE");
  } else {
    display.print(distanceMM);
    display.print("mm");
  }

  display.display();
}

void updateLEDStrip(float distanceCM) {
  if (distanceCM > SAFE_DISTANCE_CM) {
    clearLEDs();
    return;
  }

  if (distanceCM > LED_START_CM) {
    clearLEDs();
    return;
  }

  int ledsOn = 0;

  if (distanceCM <= 0) {
    ledsOn = 1;
  } else {
    ledsOn = ceil(distanceCM / 2.0);
  }

  ledsOn = constrain(ledsOn, 1, LED_COUNT);

  uint32_t color;

  if (distanceCM >= 9 && distanceCM <= 15) {
    color = strip.Color(0, 255, 0);       // Green
  } else if (distanceCM > 5 && distanceCM < 9) {
    color = strip.Color(255, 180, 0);     // Yellow
  } else {
    color = strip.Color(255, 0, 0);       // Red
  }

  clearLEDs();

  for (int i = 0; i < ledsOn; i++) {
    strip.setPixelColor(i, color);
  }

  strip.show();
}

void updateBuzzer(float distanceCM) {
  if (distanceCM > SAFE_DISTANCE_CM) {
    noTone(BUZZER_PIN);
    buzzerState = false;
    return;
  }

  if (distanceCM < DANGER_DISTANCE_CM) {
    tone(BUZZER_PIN, BUZZER_FREQ);  // Long beep
    return;
  }

  int beepInterval = map(distanceCM, 3, 20, 100, 800);
  beepInterval = constrain(beepInterval, 100, 800);

  if (millis() - lastBeepTime >= beepInterval) {
    lastBeepTime = millis();

    buzzerState = !buzzerState;

    if (buzzerState) {
      tone(BUZZER_PIN, BUZZER_FREQ);
    } else {
      noTone(BUZZER_PIN);
    }
  }
}

void clearLEDs() {
  for (int i = 0; i < LED_COUNT; i++) {
    strip.setPixelColor(i, 0, 0, 0);
  }
  strip.show();
}