Arduino Project

Dino Run 2.0

Course Introduction

In this project, you’ll learn how to use a light sensor and a servo motor with the Arduino R4 UNO to create a dino run game version 2.0.

When the ambient light suddenly drops—such as when a shadow passes—the servo will quickly activate to simulate a mechanical response, like pressing a button.

This project demonstrates basic sensor input, signal filtering, and actuator control.

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

1

2

USB Type-C cable

1

×

3

Breadboard

1

BUY

4

Wires

Several

5

Digital Servo Motor

1

6

Photoresistor

1

Wiring

3.png__PID:1c1c5497-dcf5-480f-a6a8-ccd395bac394

Common Connections:

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

Photoresistor Module
VCC:
Connect to A0 on the Arduino.
GND: Connect to breadboard’s negative power bus.

Writing the Code

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


#include 

// Pin definitions
const int lightSensorPin = A0;
const int servoPin = 9;

// Detection parameters
const int lightDropThreshold = 400;  // Increase threshold: trigger only if drop exceeds 400
unsigned int baselineLight = 0;      // Baseline light level at startup

// Trigger cooldown
const unsigned long detectionCooldown = 1000;
unsigned long lastDetectionTime = 0;

Servo myServo;

void setup() {
  myServo.attach(servoPin);
  myServo.write(0);
  pinMode(lightSensorPin, INPUT);
  Serial.begin(9600);

  // Initialize baseline light value: average over multiple readings
  delay(1000);  // Wait for stable startup
  baselineLight = getAverageLight(10);  // Average of 10 readings
  Serial.print("Baseline light value: ");
  Serial.println(baselineLight);
}

void loop() {
  int currentLight = getAverageLight(5);  // Current light average (more stable)
  unsigned long currentTime = millis();

  Serial.print("Current light: ");
  Serial.println(currentLight);

  // Check if there's a significant drop in light
  if ((baselineLight - currentLight) > lightDropThreshold && currentTime - lastDetectionTime > detectionCooldown) {
    myServo.write(90);   // Rotate
    delay(150);          // Hold the position
    myServo.write(0);    // Return to original position
    lastDetectionTime = currentTime;
  }

  delay(10);
}

// Get average light value to avoid accidental triggers
int getAverageLight(int samples) {
  long total = 0;
  for (int i = 0; i < samples; i++) {
    total += analogRead(lightSensorPin);
    delay(1);  // Small delay between readings
  }
  return total / samples;
}