Devices & Components
Arduino® Nano ESP32 with headers
Benewake TFmini Plus Laser Level Distance Sensor Waterproof IP65 Lidar Range Finder UART Anti-dust 12m 1000Hz UART for Arduino Raspberry Pi STM32
Power: USB-C Cable
Capacitor 1000 uF
BTF-LIGHTING WS2812B IC RGB 5050SMD Pure Gold Individual Addressable LED Strip 3.28FT 60LED
BTF-LIGHTING 5 Pcs/Pack 20AWG 3 Pin JST SM Male Female Plug LED Connector 6.56FT/2m Extension Cable for WS2812B
22 AWG Wire
400 points solderless breadboard
Resistor 330 ohm
Hardware & Tools
Wire Stripper & Cutter, 22-10 AWG / 0.64-2.6mm Capacity Single & Stranded Wires
Double sided tape
velcro
Software & Tools
Arduino IDE
Project description
Code
Automatic Garage Parking Assistant
cpp
The variable measurements are project-specific.
1#include <TFMPlus.h> 2#include <HardwareSerial.h> 3#include <Adafruit_NeoPixel.h> 4 5// TFMini Plus Setup 6TFMPlus tfmP; 7HardwareSerial tfSerial(1); // Using UART1 on Nano ESP32 8const int RX_PIN = 43; // White wire on TFMini Plus -> GPIO43 (RX0 on board) 9const int TX_PIN = 44; // Green wire on TFMini Plus -> GPIO44 (TX1 on board) 10 11// WS2812B LED Setup 12#define LED_PIN 5 // D2 on Nano ESP32 = GPIO5 (signal pin that goes to the resistor and then the LED strip) 13#define NUM_LEDS 60 // Number of LEDs on WS2812B strip 14#define BRIGHTNESS 40 15Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800); 16 17// Distance Thresholds for Different Color Stages 18const int MIN_DISTANCE = 167.64; // 66 in - perfect sport (red) 19const int MID_DISTANCE = 210.82; // 83 in - moderate range (orange) 20const int MAX_DISTANCE = 254; // 100 in - (off) 21 22// Timing and parking detection 23unsigned long lastReadTime = 0; 24const unsigned long readInterval = 250; // read sensor every 250ms 25 26// Variables for parked detection (checks if system should stop) 27int16_t lastDist = -1; // last measured distance 28unsigned long lastChangeTime = 0; // last time distance changed 29const unsigned long parkedTimeout = 3000; // 3 seconds parked timeout 30 31void setup() { 32 Serial.begin(115200); 33 delay(500); 34 35 // Start TFMini Plus sensor 36 tfSerial.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN); 37 delay(500); 38 39 if (tfmP.begin(&tfSerial)) { 40 Serial.println("TFMini Plus connected!"); 41 tfmP.sendCommand(SET_FRAME_RATE, FRAME_100); 42 tfmP.sendCommand(SAVE_SETTINGS, 0); 43 } else { 44 Serial.println("TFMini Plus connection failed."); 45 } 46 47 // Initialize LED strip 48 strip.begin(); 49 strip.setBrightness(BRIGHTNESS); 50 strip.show(); // Turn off all LEDs initially 51} 52 53void loop() { 54 if (millis() - lastReadTime >= readInterval) { 55 lastReadTime = millis(); 56 57 int16_t dist; 58 59 if (tfmP.getData(dist)) { 60 Serial.print("Distance: "); // displays to Serial Monitor for feedback 61 Serial.print(dist); 62 Serial.println(" cm"); 63 64 // LED Color Logic 65 if (dist > MAX_DISTANCE) { 66 setAllLeds(strip.Color(0, 0, 0)); // off 67 } 68 else if (dist < MIN_DISTANCE && dist != 0) { // Distance cannot equal 0 for the red stage since 0 is returned when the sensor detects the windshield 69 // Significant change detection, helps turn off LEDs after short time (keeps system automatic) 70 int delta = abs(dist - lastDist); 71 const int MIN_CHANGE = 3; // cm sensitivity threshold 72 73 if (delta >= MIN_CHANGE) { 74 lastChangeTime = millis(); // Only reset timer on real movement 75 setAllLeds(strip.Color(255, 0, 0)); // red 76 } 77 else if (millis() - lastChangeTime < parkedTimeout) { 78 setAllLeds(strip.Color(255, 0, 0)); // stays red 79 } 80 else { 81 setAllLeds(strip.Color(0, 0, 0)); // Turn off if car stops moving (truly parked) 82 } 83 } 84 else if (dist < MID_DISTANCE || dist == 0) { // When sensor detects the glass of the windshield (when the LED should be yellow), the distance returned is 0 85 setAllLeds(strip.Color(255, 165, 0)); // orange 86 } 87 else { 88 setAllLeds(strip.Color(0, 255, 0)); // green 89 } 90 91 lastDist = dist; // update distance after all logic for iteration 92 } 93 else { 94 Serial.print("Error: "); 95 Serial.println(tfmP.status); 96 setAllLeds(strip.Color(0, 0, 0)); // Turn off on error 97 } 98 } 99} 100 101void setAllLeds(uint32_t color) { 102 for (int i = 0; i < NUM_LEDS; i++) { 103 strip.setPixelColor(i, color); 104 } 105 strip.show(); 106}
Downloadable files
Adafruit NeoPixel Library (in Arduino IDE)
Download Adafruit NeoPixel Library by Adafruit
Adafruit_NeoPixel.cpp
TFMPlus Library (in Arduino IDE)
Download TFMPlus driver by Bud Ryerson
file.None
Comments
Only logged in users can leave comments