Arduino Nano - Ultrasone Sensor - OLED

Deze handleiding legt uit hoe u met Arduino Nano de afstand van een ultrasone sensor kunt meten en deze weergeven op een OLED-display.

Over OLED en Ultrasone Sensor

Als u niet bekend bent met OLED en Ultrasone Sensor (inclusief pinout, functionaliteit en programmeren), kunnen de volgende tutorials u van de benodigde informatie voorzien:

Bedradingsschema

Arduino Nano Ultrasone Sensor OLED bedrading schema

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Ultrasone Sensor - OLED

/* * Deze Arduino Nano code is ontwikkeld door newbiely.nl * Deze Arduino Nano code wordt zonder enige beperking aan het publiek beschikbaar gesteld. * Voor volledige instructies en schema's, bezoek: * https://newbiely.nl/tutorials/arduino-nano/arduino-nano-ultrasonic-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define TRIG_PIN 12 // The Arduino Nano pin connected to TRIG pin of ultrasonic sensor #define ECHO_PIN 11 // The Arduino Nano pin connected to ECHO pin of ultrasonic sensor #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C String distance_str; 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 distance_str.reserve(10); // to avoid fragmenting memory when using String } void loop() { // Produce a 10-microsecond pulse to the TRIG pin. digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Measure the pulse duration from the 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"); distance_str = String(distance_cm, 2); // two decimal places distance_str += " cm"; Serial.println(distance_str); // print the temperature in Celsius to Serial Monitor oled_display_center(distance_str); // display temperature on OLED } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Snelle Stappen

  • Klik op het Libraries icoon in de linkerzijbalk van de Arduino IDE.
  • Zoek naar “SSD1306” en vind de SSD1306 bibliotheek van Adafruit.
  • Druk op de Installeren knop om de installatie te voltooien.
Arduino Nano OLED bibliotheek
  • U wordt gevraagd om aanvullende bibliotheekafhankelijkheden te installeren.
  • Om ze allemaal in één keer te installeren, klikt u op de Alles installeren knop.
Arduino Nano Adafruit GFX sensor bibliotheek
  • Kopieer de code en open deze met de Arduino IDE.
  • Klik op de Uploaden knop in de Arduino IDE om de code te compileren en naar de Arduino Nano te uploaden.
  • Beweeg uw hand voor de sensor.
  • Controleer het resultaat op het OLED-display en in de Serial Monitor.

※ Notiz:

Deze code centreert de tekst horizontaal en verticaal op een OLED-display. Voor meer informatie, zie How to vertical/horizontal center on OLED.

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!