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

Arduino Project 041 - Ultrasonic Distance OLED 128X64 Display

RonWang2周前 (12-06)航空航模46

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 002 - LED SOS Morse Code Singal

Arduino Project 002 - LED SOS Morse Code Singal

Arduino 电子编程--灯项目及控制,主要使用Arduino编程控制LED灯,实现基本控制Project 2 LED闪烁S.O.S信号。项目2 Project 2 S.O.S...

Arduino Project 026 - Dual Servo Control

Arduino Project 026 - Dual Servo Control

This project you’ll create another simple project, but this time you’ll control two servos using com...

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

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

上大学时学习《C语言程序设计》(第二版)作者谭浩强,大部分编程时间是在学校机房度过,每次点开桌面的TurboC图标,就开始在里面敲代码,然后保存程序Abc.C,下一步进行编译,如果编译成功的话,就可以...

Arduino Project 007 - Pulsating Lamp

Arduino Project 007 - Pulsating Lamp

You are now going try a more advanced method of controlling LEDs. So far, you have simply turned the...

Arduino Project 008 - RGB LED Mood Lamp

Arduino Project 008 - RGB LED Mood Lamp

In the last project, you learned how to adjust the brightness of an LED using the PWM capabilities o...

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...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。