Week 7

Testing OLED display output and triggering a buzzer based on measured distance.

OLED + Distance Sensor + Buzzer

This week I built a small setup with a 0.96" OLED display, a HC-SR04 ultrasonic sensor, and a buzzer, all connected to an Arduino Nano.

The display shows the current measured distance. If the object gets too close (less than 15 cm), the buzzer will activate.

Below is a quick video demo:

Power Consumption Measurements

I measured the power drawn by the OLED display in two different modes using a DT830D RANGE red multimeter. During normal operation, the measured voltage was 4.73 V and the current was 0.80 mA. This gives a power consumption of: 4.73 V × 0.80 mA = 3.78 mW.

When the display was set to full white, the current increased to 20.20 mA while the voltage remained at 4.73 V. This results in: 4.73 V × 20.20 mA = 95.55 mW.

The table below summarizes these measurements:

OLED Mode Voltage (V) Current (mA) Power (mW)
Normal Operation 4.73 0.80 3.78
Full White 4.73 20.20 95.55

Arduino Code

This sketch measures distance with the ultrasonic sensor, displays it, and controls the buzzer accordingly.

Copied!
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDRESS 0x3C

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

const int trigPin = 9;
const int echoPin = 10;
const int buzzerPin = 8;
const int thresholdDistance = 15; // cm

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(buzzerPin, OUTPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDRESS)) {
    while (true);
  }
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  long distance = duration * 0.034 / 2;

  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Distance:");
  display.setCursor(0, 12);
  display.print(distance);
  display.println(" cm");
  display.display();

  if (distance > 0 && distance < thresholdDistance) {
    tone(buzzerPin, 2000);
  } else {
    noTone(buzzerPin);
  }

  delay(200);
}