Raspberry Pi - Ultrasone Sensor - OLED

Deze tutorial legt uit hoe u de afstand meet met een ultrasone sensor en deze vervolgens op een OLED-display weergeeft.

Hardware Benodigd

1×Raspberry Pi 5
1×SSD1306 I2C OLED-Display 128x64
1×SSD1306 I2C OLED-Display 128x32
1×Ultrasone Sensor
1×Breadboard (experimenteerprint)
1×Jumperdraden (verbindingskabels)
1×(Aanbevolen) Schroefklem Block Shield voor Raspberry Pi
1×(Aanbevolen) Raspberry Pi Prototyping Basisplaat & Breadboard Kit
1×(Aanbevolen) HDMI-Touchscreen-Monitor voor Raspberry Pi

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 (inclusief pinout, werking, programmeren, enz.), kunnen de volgende tutorials helpen:

Bedradingsschema

Raspberry Pi Ultrasone Sensor OLED bedrading schema

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - Ultrasone Sensor - OLED

Snelle Stappen

  • Zorg dat u Raspbian of een ander Raspberry Pi compatibel besturingssysteem op uw Pi geïnstalleerd heeft.
  • Zorg dat uw Raspberry Pi verbonden is met hetzelfde lokale netwerk als uw pc.
  • Zorg dat uw Raspberry Pi verbinding heeft met internet als u nog bibliotheken moet installeren.
  • Als dit de eerste keer is dat u een Raspberry Pi gebruikt, zie hoe u de Raspberry Pi instelt
  • Verbind uw pc via SSH met de Raspberry Pi door gebruik te maken van de ingebouwde SSH-client op Linux en macOS of PuTTY op Windows. Zie hoe u uw pc via SSH met de Raspberry Pi verbindt.
  • Controleer of u de RPi.GPIO bibliotheek geïnstalleerd heeft. Zo niet, installeer deze dan met het volgende commando:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install Adafruit-SSD1306
  • Maak een Python-scriptbestand aan met de naam ultrasonic_oled.py en voeg de volgende code toe:
# Deze Raspberry Pi code is ontwikkeld door newbiely.nl # Deze Raspberry Pi code wordt zonder enige beperking aan het publiek beschikbaar gesteld. # Voor volledige instructies en schema's, bezoek: # https://newbiely.nl/tutorials/raspberry-pi/raspberry-pi-ultrasonic-sensor-oled import time import RPi.GPIO as GPIO import Adafruit_SSD1306 # GPIO pin configuration for the ultrasonic sensor TRIG_PIN = 16 ECHO_PIN = 20 # OLED display configuration OLED_RESET_PIN = None # Set to GPIO pin if your display has a reset pin OLED = Adafruit_SSD1306.SSD1306_128_64(rst=OLED_RESET_PIN) # Initialize the OLED display OLED.begin() # Clear the display OLED.clear() OLED.display() def measure_distance(): # Trigger the ultrasonic sensor GPIO.output(TRIG_PIN, True) time.sleep(0.00001) GPIO.output(TRIG_PIN, False) pulse_start, pulse_end = 0, 0 # Record the time when the ultrasonic wave is transmitted while GPIO.input(ECHO_PIN) == 0: pulse_start = time.time() # Record the time when the reflected wave is received while GPIO.input(ECHO_PIN) == 1: pulse_end = time.time() # Calculate the time duration of the pulse pulse_duration = pulse_end - pulse_start # Calculate the distance using the speed of sound (343 m/s) distance = pulse_duration * 17150 return round(distance, 2) try: # Set up GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(TRIG_PIN, GPIO.OUT) GPIO.setup(ECHO_PIN, GPIO.IN) while True: # Read distance from ultrasonic sensor distance = measure_distance() # Clear the display OLED.clear() # Display distance on OLED OLED.draw.text((0, 0), "Distance:", font=None, fill=255) OLED.draw.text((0, 16), "{} cm".format(distance), font=None, fill=255) # Display on OLED OLED.display() # Print distance to console (optional) print("Distance: {} cm".format(distance)) # Delay for readability time.sleep(1) except KeyboardInterrupt: print("Program terminated by user.") finally: GPIO.cleanup()
  • Sla het bestand op en start het Python-script door het volgende commando in de terminal uit te voeren:
python3 ultrasonic_oled.py

Het script loopt continu in een oneindige lus totdat u Ctrl + C indrukt in de terminal.

  • Beweeg uw hand voor de sensor.
  • Bekijk het resultaat op het OLED-display en in de Seriële Monitor.

※ Notiz:

Deze code centreert de tekst zowel horizontaal als verticaal op het OLED-display. Voor meer informatie, zie Hoe tekst verticaal/horizontaal centreren op 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.

Bekijk onze video-tutorial voor een visuele begeleiding bij dit project! De video biedt extra inzichten en helpt u stap voor stap.

※ 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!