Alfredo Bombace
Published

Arduino : OLED distance

Arduino project to display distance on a SSD1306 OLED.

BeginnerShowcase (no instructions)30 minutes2,701
Arduino : OLED distance

Things used in this project

Story

Read more

Schematics

The Circuit

Here is the circuit made with Fritzing. For connect ultrasounds sensor:
VCC - 5V
Trig - Digital PIN 2
Echo - Digital PIN 3
GND - GND
For connect OLED display (basic connections):
VCC - 5V
GND - GND
SCL - Analog PIN A5
SDA - Analog PIN A4

Code

The Code

C/C++
Here is the code. Important: you must include the following libraries: Adafruit GFX, Adafruit SSD1306, SPI and Wire (that you can found on the Arduino Library Manager).
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NewPing.h>
#define PIN_TRIG 2
#define PIN_ECHO 3
#define MAX_DIST 450
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

NewPing rilevatore(PIN_TRIG,PIN_ECHO,MAX_DIST);

void setup()   {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  delay(1000);
  display.clearDisplay();
}

void loop(){
	delay(100);
	unsigned int uS = rilevatore.ping();
  int cm = rilevatore.convert_cm(uS);
  Serial.println(cm);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(5,0);
  display.println("cm:");
  display.println(cm);
  display.display();
}

Credits

Alfredo Bombace
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.