当前位置:首页 > 科学研究 > 航空航模 > 正文内容

Arduino Project 041 - Ultrasonic Distance OLED 128X64 Display

RonWang4个月前 (12-06)航空航模178

About the Ultrasonic sensor knowledge and infor mation click the link : Arduino Project 038 - Simple Ultrasonic Range HC-SR04

 About OLED Display

Because the OLED display uses I2C communication protocol, wiring is very simple. You just need to connect to the Arduino Uno I2C pins as shown in the table below.

The OLED display doesn’t require backlight, which results in a very nice contrast in dark environments. Additionally, its pixels consume energy only when they are on, so the OLED display consumes less power when compared with other displays.

The model we’re using here has only four pins and communicates with the Arduino using I2C communication protocol. There are models that come with an extra RESET pin. There are also other OLED displays that communicate using SPI communication.

Circuit and Schematic

arduino UltraSonic OLED Ditance Display Circuit


arduino UltraSonic OLED Ditance Display Schematic


Libraries

To control the OLED display you need the adafruit_SSD1306.h and the adafruit_GFX.h libraries. Follow the next instructions to install those libraries.

1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries. The Library Manager should open.

2. Type “SSD1306” in the search box and install the SSD1306 library from Adafruit.


/* Project 41 Ultrasonic Distance OLED 128X64 Display
 * Created by Ronwang 
 * Hardware support by Autaba www.autabaec.com
 * Dec,6th,2024
 */
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
const int trigPin = 9; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int echoPin = 8; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
float duration, distance;
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
String tempString;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }
  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display
  oled.setTextSize(2);      // text size
  oled.setTextColor(WHITE); // text color
  oled.setCursor(0, 10);    // position to display
  tempString.reserve(10);   // to avoid fragmenting memory when using String
}
void loop() {
  // generate 10-microsecond pulse to TRIG pin
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration*.0343)/2;  // Distance unit cm
  // print the value to Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
  tempString  = String(distance, 2); // two decimal places
  tempString += " cm";
  Serial.println(tempString);    // print the temperature in Celsius to Serial Monitor
  oledDisplayCenter(tempString); // display temperature on OLED
}
void oledDisplayCenter(String text) {
  int16_t x1;
  int16_t y1;
  uint16_t width;
  uint16_t height;
  oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
  // display on horizontal and vertical center
  oled.clearDisplay(); // clear display
  oled.setCursor((SCREEN_WIDTH - width) / 2, (SCREEN_HEIGHT - height) / 2);
  oled.println(text); // text to display
  oled.display();
}


版权声明:本文为原创文章,版权归donstudio所有,欢迎分享本文,转载请保留出处!

本文链接:http://parentscn.com/?id=298

标签: Arduino

相关文章

Arduino Project 019 - LED Dot Matrix Display - Basic Animation

Arduino Project 019 - LED Dot Matrix Display - Basic Animation

So far you have dealt with individual 5mm LEDs. LEDs can also be obtained in a package known as a do...

Arduino Project 001 - LED Blink

Arduino Project 001 - LED Blink

Arduino 电子编程--灯项目及控制,主要使用Arduino编程控制LED灯,实现基本控制Project 1 LED闪烁,基本的应用Project 3和4红绿灯项目项目1 Project...

Arduino Project 030 - Line Following Robot

Arduino Project 030 - Line Following Robot

Project 30 Line Following RobotUse L298P Driver Line Following Robot Line o...

Arduino Project 012 - Piezo Sounder Melody Player

Arduino Project 012 - Piezo Sounder Melody Player

Rather than using the piezo to make annoying alarm sounds, why not use it to play a melody? You are...

Arduino Programming Basic - Input and Outpput

Arduino Programming Basic - Input and Outpput

The pins on the Arduino can be configured as either inputs or outputs. This document explains the fu...

C语言调试运行环境Pelles C的安装

C语言调试运行环境Pelles C的安装

C语言调试运行环境之TurboC一文介绍了在32位Windows系统下安装C语言运行环境之TubroC,但是由于TurobC只能在32位系统下运行,导致现在很多Windows10和Windows 11...