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

Arduino Project 041 - Ultrasonic Distance OLED 128X64 Display

RonWang5个月前 (12-06)航空航模191

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://www.donstudio.cn/?id=298

标签: Arduino

相关文章

Arduino Programming Basic - Serial Monitor

Arduino Programming Basic - Serial Monitor

Arduino 程序基础,介绍Arduino程序的基本组成,第一部分编写数个例程,讲解关于变量及变量名称,串口监视器,if循环,for循环,while循环等。第二部分介绍了函数,全局变量,局部变量和静...

Arduino Project 010 - Serial Controlled Mood Lamp

Arduino Project 010 - Serial Controlled Mood Lamp

For Project 10, you will revisit the circuit from Project 8 — RGB Mood Lamp, but you’ll now delve in...

Arduino Project 035 - Keypad 4X4 or 4X3

Arduino Project 035 - Keypad 4X4 or 4X3

Most of the time we are used key, button, or switch to get input value in our projects. When we inte...

 Arduino Project 038 - Simple Ultrasonic Range HC-SR04

Arduino Project 038 - Simple Ultrasonic Range HC-SR04

Ultrasonic range finders measure distance by emitting a pulse of ultrasonic sound that travels throu...

Arduino Project 050 - IR Remote Control Light

Arduino Project 050 - IR Remote Control Light

Project 50 IR Remote Control Light/* Project 50 IR Remote Control Ligh...

Arduino Project 029 - Control Stepper Motor ULN2004A

Arduino Project 029 - Control Stepper Motor ULN2004A

Stepper motors, due to their unique design, can be controlled to a high degree of accuracy without a...