Arduino - Ultrasone Sensor - OLED

In deze handleiding gaan we leren hoe u de afstand van een ultrasone sensor leest en deze weergeeft op een OLED.

Arduino Ultrasone Sensor OLED

Hardware Vereist

1×Arduino Uno R3
1×USB 2.0 kabel type A/B
1×SSD1306 I2C OLED-Display 128x64
1×SSD1306 I2C OLED-Display 128x32
1×Ultrasone Sensor
1×Breadboard (experimenteerprint)
1×Jumper draden
1×(Aanbevolen) Schroefklem Block Shield voor Arduino Uno
1×(Aanbevolen) Breadboard-Shield voor Arduino Uno
1×(Aanbevolen) Behuizing voor Arduino Uno
1×(Aanbevolen) Prototyping Basisplaat & Breadboard Kit voor Arduino Uno

Of u kunt de volgende kits kopen:

1×DIYables Sensorkit (30 sensoren/displays)
1×DIYables Sensorkit (18 sensoren/displays)
Openbaarmaking: Sommige van de links in deze sectie zijn Amazon-affiliate links. We kunnen een commissie ontvangen voor aankopen die via deze links worden gedaan, zonder extra kosten voor u. We waarderen uw steun.

Over OLED en Ultrasone Sensor

Als u niet bekend bent met OLED en ultrasone sensor (pinout, werking, programmeren, enz.), leer er dan meer over in de volgende tutorials:

Aansluitschema

Arduino Ultrasone Sensor OLED Aansluitschema

This image is created using Fritzing. Click to enlarge image

Arduino Code - Ultrasone Sensor - OLED

/* * Deze Arduino code is ontwikkeld door newbiely.nl * Deze Arduino code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino/arduino-ultrasonic-sensor-oled */ #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 TRIG_PIN = 9; // Arduino pin connected to Ultrasonic Sensor's TRIG pin const int ECHO_PIN = 8; // Arduino pin connected to Ultrasonic Sensor's ECHO pin Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C String tempString; void setup() { 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(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin long duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance float distance_cm = 0.017 * duration_us; // print the value to Serial Monitor Serial.print("distance: "); Serial.print(distance_cm); Serial.println(" cm"); tempString = String(distance_cm, 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(); }

Snelle Stappen

  • Open de Arduino IDE op uw pc.
  • Navigeer naar het Libraries icoon in de linkerzijbalk van de Arduino IDE.
  • Zoek op “SSD1306”, vind vervolgens de SSD1306 bibliotheek van Adafruit.
  • Klik op de Installeren knop om de bibliotheek te installeren.
Arduino OLED bibliotheek
  • U krijgt een melding om enkele andere bibliotheek-afhankelijkheden te installeren
  • Klik op de Alles Installeren knop om alle afhankelijkheden te installeren.
Arduino Adafruit GFX sensor bibliotheek
  • Kopieer bovenstaande code en open deze met Arduino IDE
  • Klik op de Uploaden knop in de Arduino IDE om de code naar de Arduino te uploaden
  • Beweeg uw hand voor de sensor
  • Bekijk het resultaat op het OLED-display en de Seriële Monitor

※ Notiz:

De bovenstaande code centreert de tekst automatisch horizontaal en verticaal op het OLED-display. Zie Hoe tekst verticaal/horizontaal centreren op OLED voor meer details.

Video Tutorial

We overwegen het maken van videotutorials. Als u videotutorials belangrijk vindt, abonneer u dan op ons YouTube-kanaal om ons te motiveren de video's te maken.

※ ONZE BERICHTEN

  • U bent welkom om de link naar deze tutorial te delen. Gebruik onze inhoud echter niet op andere websites. We hebben veel moeite en tijd gestoken in het maken van de inhoud, respecteer alstublieft ons werk!