Published

Arduino OLED Eye Movement with Sensors

Eye movements on an OLED display using an ultrasonic sensor and an Arduino. The setup simulates a pair of eyes that track obj movement

BeginnerFull instructions provided788

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
WaveShare 1.5inch RGB OLED Display
×1
Breadboard (generic)
Breadboard (generic)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
PH2.0 7pin connector
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Hardware Schematic

Code

Main Program

C/C++
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 128

#define SCLK_PIN 13
#define MOSI_PIN 11
#define RST_PIN   8
#define DC_PIN    7
#define CS_PIN    10

#define BLACK 0x0000
#define WHITE 0xFFFF
#define BLUE 0x001F

#define TRIGGER_PIN 6
#define ECHO_PIN 5

unsigned long duration;
int distance;


Adafruit_SSD1351 display = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, CS_PIN, DC_PIN, RST_PIN);

void setup() {
  Serial.begin(9600);
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  delay(250);
  display.begin();
  display.fillScreen(BLUE); // Clear the screen
}

void loop() {
  // Ultrasonic sensor code
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = duration * 0.034 / 2;

  // Adjust eye position based on sensor readings
  int eyeOffset = map(distance, 0, 200, -30, 30); // Map sensor range to eye movement range
  int leftEyeX = display.width() / 2 - 25 + eyeOffset;
  int rightEyeX = display.width() / 2 + 25 + eyeOffset;

  Serial.print("Distance: ");
  Serial.println(distance);
  Serial.print("Eye offset: ");
  Serial.println(eyeOffset);

  // Display eyes
  display.fillCircle(leftEyeX, display.height() / 2, 15, WHITE);
  display.fillCircle(rightEyeX, display.height() / 2, 15, WHITE);
  delay(100); // Adjust delay as needed
  display.fillScreen(BLUE); // Clear the screen
}

Credits

Comments