ค้นหาโค้ด

รหัส Arduino และวิดีโอสำหรับเซนเซอร์วัดระยะอัลตราโซนิก HC-SR04 พร้อมจอแสดงผล OLED SSD1306

รหัส Arduino และวิดีโอสำหรับเซนเซอร์วัดระยะอัลตราโซนิก HC-SR04 พร้อมจอแสดงผล OLED SSD1306

ในบทช่วยสอนนี้ เราจะสาธิตวิธีการใช้เซนเซอร์วัดระยะอัลตราโซนิก HC-SR04 ร่วมกับจอแสดงผล OLED SSD1306 เพื่อแสดงผลการวัดระยะทาง โปรเจกต์นี้ช่วยให้คุณวัดระยะทางได้โดยไม่ต้องใช้คอมพิวเตอร์ ทำให้เหมาะสำหรับการใช้งานต่างๆ รวมถึงหุ่นยนต์ เมื่อสิ้นสุดโปรเจกต์นี้ คุณจะได้ชุดอุปกรณ์ที่ใช้งานได้จริงซึ่งแสดงค่าการวัดระยะทางบนหน้าจอ OLED โดยตรง

How HC-SR04 ulrasonic sensor work
SSD1306 OLED display

เมื่อเราดำเนินการต่อไป ผมจะอธิบายการเชื่อมต่อสายไฟที่จำเป็นสำหรับทั้งสองส่วนประกอบ และให้คำแนะนำเกี่ยวกับโค้ด Arduino ที่รวมส่วนประกอบเหล่านี้เข้าด้วยกัน ซึ่งจะช่วยให้คุณเข้าใจว่าเซนเซอร์วัดระยะทางทำงานอย่างไร และวิธีส่งข้อมูลนั้นไปยังจอแสดงผล OLED เพื่อความชัดเจนเพิ่มเติม คุณสามารถอ้างอิงวิดีโอที่เกี่ยวข้องกับบทช่วยสอนนี้ได้ (ในวิดีโอที่ 00:00)

อธิบายฮาร์ดแวร์

ส่วนประกอบหลักของโปรเจกต์นี้คือเซนเซอร์วัดระยะอัลตราโซนิก HC-SR04 และจอแสดงผล OLED SSD1306 HC-SR04 ทำงานโดยใช้คลื่นเสียงอัลตราโซนิก โดยจะส่งพัลส์เสียงผ่านพินทริกเกอร์ (trigger) และฟังเสียงสะท้อนที่พินเอคโค่ (echo) โดยการคำนวณเวลาที่เสียงใช้ในการกลับมา เซนเซอร์สามารถกำหนดระยะทางไปยังวัตถุได้ เซนเซอร์นี้มีประสิทธิภาพมากสำหรับการวัดระยะใกล้ โดยทั่วไปสูงสุดประมาณ 400-500 ซม.

จอแสดงผล OLED SSD1306 เป็นจอแสดงผลที่ใช้อินเทอร์เฟซ I2C ซึ่งช่วยให้คุณแสดงข้อความและกราฟิกได้ โดยสื่อสารผ่านสองพิน: SDA (สายข้อมูล) และ SCL (สายสัญญาณนาฬิกา) ในการตั้งค่าของเรา พินเหล่านี้เชื่อมต่อกับพินเฉพาะของ Arduino ทำให้ง่ายต่อการรวมและควบคุม

รายละเอียดจาก Datasheet

ผู้ผลิตหลายราย
หมายเลขชิ้นส่วนHC-SR04
แรงดันไฟฟ้าลอจิก/IO5 V
แรงดันไฟฟ้าจ่าย5 V
กระแสเอาต์พุต (ต่อช่อง)15 mA
กระแสสูงสุด (ต่อช่อง)20 mA
คำแนะนำความถี่ PWMไม่มี
เกณฑ์ลอจิกอินพุต0.3*Vcc ถึง 0.7*Vcc
แรงดันตก / RDS(on) / อิ่มตัวไม่มี
ขีดจำกัดความร้อน0-70 °C
แพ็คเกจโมดูล 4 พิน
หมายเหตุ / รุ่นต่างๆโมดูลมาตรฐาน

  • ตรวจสอบให้แน่ใจว่าแหล่งจ่ายไฟ (5 V) เพียงพอสำหรับทั้งสองส่วนประกอบ
  • ใช้ตัวต้านทาน pull-up หากจำเป็นสำหรับการสื่อสาร I2C
  • รักษาสายทริกเกอร์และเอคโค่ให้สั้นเพื่อหลีกเลี่ยงการรบกวน
  • ตรวจสอบทิศทางของเซนเซอร์อัลตราโซนิกเพื่อการอ่านค่าที่แม่นยำ
  • ใช้พื้นผิวที่มั่นคงสำหรับจอแสดงผลเพื่อหลีกเลี่ยงการสั่นสะเทือนทางกล

คำแนะนำการเดินสายไฟ

Arduino wiring for HC-SR04 ultrasonic SSD1306 128x64 OLED
Arduino wiring for HC-SR04 ultrasonic SSD1306 128x64 OLED

ในการเชื่อมต่อเซนเซอร์อัลตราโซนิก HC-SR04 ให้เริ่มต้นด้วยการเดินสายพิน VCC (โดยปกติเป็นสีแดง) ไปยังพิน 5V บน Arduino จากนั้นเชื่อมต่อพิน GND (โดยทั่วไปเป็นสีดำหรือสีเหลือง) เข้ากับพิน GND ตัวใดตัวหนึ่งของ Arduino พินทริกเกอร์ (มักเป็นสีน้ำเงิน) ควรเดินสายไปยังพินดิจิทัล 12 บน Arduino ในขณะที่พินเอคโค่ (โดยปกติเป็นสีเขียว) ควรเชื่อมต่อกับพินดิจิทัล 11

สำหรับจอแสดงผล OLED SSD1306 ให้เชื่อมต่อพิน VCC ไปยังพิน 5V บน Arduino และพิน GND ไปยัง GND พิน SCL ควรเชื่อมต่อกับพิน A5 และพิน SDA กับพิน A4 บน Arduino หากคุณใช้ Arduino รุ่นอื่นที่มีพิน I2C เฉพาะ ให้เชื่อมต่อจอแสดงผลเข้ากับพินเหล่านั้นโดยตรงแทน

ตัวอย่างโค้ดและคำอธิบาย

ในฟังก์ชัน setup เราจะเริ่มต้นจอแสดงผลและการสื่อสารแบบอนุกรม ตัวอย่างต่อไปนี้สาธิตวิธีการตั้งค่าจอแสดงผล:

void setup() {
   Serial.begin(9600);// ตั้งค่าจอแสดงผลแบบอนุกรมที่ 9600 baud
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // เริ่มต้นด้วยที่อยู่ I2C 0x3D
}

ที่นี่ จอแสดงผลถูกเริ่มต้นด้วยที่อยู่ I2C ทำให้สามารถสื่อสารกับ Arduino ได้ จอแสดงผลแบบอนุกรมยังถูกตั้งค่าเพื่อพิมพ์ค่าระยะทาง

ในลูปหลัก เราจะล้างจอแสดงผล วัดระยะทาง และพิมพ์ค่า โค้ดตัวอย่างต่อไปนี้แสดงวิธีจัดการการวัดระยะทาง:

void loop() {
   display.clearDisplay();
   String distance  = String(sonar.ping_cm());// รับระยะทางและแปลงเป็นสตริงสำหรับแสดงผล
   robojaxText(distance +"cm", 3, 20, 3, false);
   display.display();
   delay(50); // รอ 50ms ระหว่างการส่งพัลส์
}

ลูปนี้จะวัดระยะทางอย่างต่อเนื่องและอัปเดตจอแสดงผลด้วยค่าปัจจุบัน ฟังก์ชัน `robojaxText` ใช้เพื่อแสดงข้อความบนหน้าจอ OLED

การสาธิต / สิ่งที่คาดหวัง

เมื่อการตั้งค่าเสร็จสมบูรณ์และอัปโหลดโค้ดแล้ว คุณควรเห็นระยะทางแสดงบนหน้าจอ OLED เป็นเซนติเมตร เมื่อคุณขยับวัตถุเข้าใกล้หรือไกลจากเซนเซอร์ ค่าที่แสดงควรเปลี่ยนแปลงตามไปด้วย ตรวจสอบให้แน่ใจว่าเซนเซอร์อยู่ในทิศทางที่ถูกต้องและไม่มีสิ่งกีดขวาง (ในวิดีโอที่ 02:30)

ไทม์สแตมป์ของวิดีโอ

  • 00:00 - แนะนำโปรเจกต์
  • 01:15 - คำแนะนำการเดินสายไฟ
  • 02:30 - คำอธิบายโค้ด
  • 04:00 - การสาธิตเซนเซอร์

ภาพ

HC-SR04 ulrasonic sensor
HC-SR04 ulrasonic sensor
How HC-SR04 ulrasonic sensor work
How HC-SR04 ulrasonic sensor work
Arduino wiring for HC-SR04 ultrasonic SSD1306 128x64 OLED
Arduino wiring for HC-SR04 ultrasonic SSD1306 128x64 OLED
SSD1306 OLED display
SSD1306 OLED display
SSD1306 OLED display-dimensions
SSD1306 OLED display-dimensions
SSD1306_128x64_OLDE-4
SSD1306_128x64_OLDE-4
SSD1306 OLED display-back
SSD1306 OLED display-back
44-This is the Arduino code for an HC-SR04 ultrasonic distance sensor with an SSD1306 display.
ภาษา: C++
/*
 * This is the Arduino code for the HC-SR04 Ultrasonic Distance Sensor with SSD1306 Display
 * to measure the distance using Arduino for a robotic car and other applications
 * Watch the video https://youtu.be/Pgx5fNF4Q6M
 * 
 * Written by Ahmad Shamshiri for Robojax Video
 * Date: December 21, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purposes only.
 * 
 */

/* Original Code 
   from https://github.com/adafruit/Adafruit_SSD1306
// https://playground.arduino.cc/Code/NewPing
 * Modified for Robojax video on December 21, 2017
// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------
*/
//// start of SSD1306 display 
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2


#define LOGO16_GLCD_HEIGHT 64 
#define LOGO16_GLCD_WIDTH  128 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//// end of SSD1306 display 

// ---------------------------------------------------------------------------
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.



void setup() {
   Serial.begin(9600);// set serial monitor with 9600 baud
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  
}// setup end


void loop() {
   display.clearDisplay();
  robojaxText("Distance", 3, 0, 2, false);
  String distance  = String(sonar.ping_cm());// get distance and convert it to string for display
  robojaxText(distance +"cm", 3, 20, 3, false);
    display.display();

  delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

}// loop end


/*
 * robojaxText(String text, int x, int y,int size, boolean d)
 * text is the text string to be printed
 * x is the integer x position of text
 * y is the integer y position of text
 * size is the text size, 1, 2, 3 etc
 * d is either true or false.  Use true to display.
 */
void robojaxText(String text, int x, int y,int size, boolean d) {

  display.setTextSize(size);
  display.setTextColor(WHITE);
  display.setCursor(x,y);
  display.println(text);
  if(d){
    display.display();
  }

}

แหล่งข้อมูลและเอกสารอ้างอิง

ไฟล์📁

แผ่นข้อมูล (pdf)

  • SSD1306 display datasheet
    SSD1306 is a single-chip CMOS OLED/PLED driver with controller for organic / polymer light emitting diode dot-matrix graphic display system. It consists of 128 segments and 64commons. This IC is designed for Common Cathode type OLED panel. The SSD1306 embeds with contrast control, display RAM and oscillator, which reduces the number of external components and power consumption. It has 256-step brightness control. Data/Commands are sent from general MCU through the hardware selectable 6800/8000 series compatible Parallel Interface, I2C interface or Serial Peripheral Interface. It is suitable for many compact portable applications, such as mobile phone sub-display, MP3 player and calculator, etc.
    SSD1306_display_datasheet.pdf 1.79 MB