Garage Distance Sensor
Introduction
Looking for a Qwiic and easy way to set up perfect parking? Look no further than the SparkFun Qwiic Ultrasonic Distance Sensor! This nifty device takes your garage parking game to the next level, offering a high-tech solution to prevent those dreaded wall bumps!
If you are looking for the full Hookup Guide for the SparkFun Ultrasonic Distance Sensor - TCT40 (Qwiic), click the button bellow. This guide only covers a simple project to get you started quickly, while the full Hookup Guide goes over every detail of the sensor.
Hardware Needed
To follow this experiment, you will need the following materials. While this is a simple project we wanted to make sure that you have everything you need to get started before we get to the code. For this simple project we chose the RedBoard Qwiic but you could choose from many of our development boards such as the Qwiic Pro Micro as well.
Software Setup
Installing the Required Libraries
- Install the SparkFun Qwiic Ultrasonic Arduino Library: Open the Arduino IDE and navigate to Sketch > Include Library > Manage Libraries. In the Library Manager, search for "SparkFun Qwiic Ultrasonic Arduino Library" and install the latest version from SparkFun, or by downloading it from the GitHub Repository.
- Install the SparkFun Qwiic OLED Arduino Library: Open the Arduino IDE and navigate to Sketch > Include Library > Manage Libraries. In the Library Manager, search for "SparkFun Qwiic OLED Arduino Library" and install the latest version from SparkFun, or by downloading it from the GitHub Repository.
- Install the SparkFun Qwiic Twist Arduino Library: Open the Arduino IDE and navigate to Sketch > Include Library > Manage Libraries. In the Library Manager, search for "SparkFun Qwiic Twist Arduino Library" and install the latest version from SparkFun, or by downloading it from the GitHub Repository.
Read Measurements with a Serial Monitor
Now that we've installed the Arduino library, it's time to upload our first sketch to make sure everything is working properly and you are able to read basic measurements with your Serial Monitor in the Arduino IDE.
For this example you will need the following items SparkFun RedBoard Qwiic, a SparkFun Ultrasonic Distance Sensor, a SparkFun Qwiic OLED - (1.3in., 128x64), a SparkFun Qwiic Twist, two 100mm Qwiic Cables, a 500mm Qwiic Cable , and a USB Micro-B Cable.
Using the Qwiic system, assembling the hardware is simple and straightforward. Start by connecting the SparkFun RedBoard Qwiic to the SparkFun Qwiic OLED (1.3in., 128x64) using one of the 100mm Qwiic cables. Next, connect the SparkFun Qwiic Twist to the OLED using the second 100mm Qwiic cable. Finally, plug one end of the 500mm Qwiic cable into the remaining port on the Qwiic Twist, and connect the other end to the SparkFun Ultrasonic Distance Sensor. Remember to insert all Qwiic cables in the correct orientation. To power up the system, connect the RedBoard to your computer using the USB Micro-B cable. With these connections in place, you're all set to start your project!
You can copy and paste the code below into a shiny new Arduino sketch:
#include <Wire.h>
#include <SparkFun_Qwiic_OLED.h>
#include "SparkFun_Qwiic_Twist_Arduino_Library.h"
#include "SparkFun_Qwiic_Ultrasonic_Arduino_Library.h"
// Create instances
QwiicMicroOLED myOLED;
TWIST twist;
QwiicUltrasonic myUltrasonic;
// Variables for distance settings
float minDistance = 50; // Initial min distance in mm
float maxDistance = 250; // Initial max distance in mm
bool settingMin = true; // True = setting min, False = setting max
bool inRange = false; // Whether current distance is within range
// Device address for ultrasonic sensor
uint8_t deviceAddress = kQwiicUltrasonicDefaultAddress; // 0x2F
void setup() {
Serial.begin(115200);
Serial.println("Starting Distance Sensor with OLED and Twist");
Wire.begin();
// Initialize the OLED
while (myOLED.begin() == false) {
Serial.println("OLED not connected, check wiring!");
delay(1000);
}
// Initialize the Twist encoder
if (twist.begin() == false) {
Serial.println("Twist not detected. Freezing...");
while (true);
}
// Initialize the Ultrasonic sensor
while(myUltrasonic.begin(deviceAddress) == false) {
Serial.println("Ultrasonic sensor not connected, check wiring!");
delay(1000);
}
// Reset twist count and LED
twist.setCount(50); // Start at 50mm
twist.setColor(0, 0, 0); // LED off initially
}
float getDistance() {
uint16_t distance = 0;
myUltrasonic.triggerAndRead(distance);
return distance; // Returns distance in mm
}
void loop() {
// Get distance measurement
float currentDistance = getDistance();
// Check for twist button press to toggle between setting min and max
if (twist.isPressed()) {
settingMin = !settingMin; // Toggle between min and max setting
twist.setCount(settingMin ? int(minDistance) : int(maxDistance));
delay(200); // Debounce
}
// Update min or max distance based on twist
int count = twist.getCount();
if (settingMin) {
minDistance = count;
if (minDistance >= maxDistance) minDistance = maxDistance - 10;
} else {
maxDistance = count;
if (maxDistance <= minDistance) maxDistance = minDistance + 10;
}
// Check if current distance is within range
inRange = (currentDistance >= minDistance && currentDistance <= maxDistance);
// Update LED based on whether distance is in range
if (inRange) {
twist.setColor(0, 255, 0); // Green when in range
} else {
twist.setColor(0, 0, 0); // Off when out of range
}
// Clear the display
myOLED.erase();
// Display current mode and settings
String modeStr = settingMin ? "Set Min" : "Set Max";
myOLED.text(0, 0, modeStr, 1);
// Display distance
String distStr = String(int(currentDistance)) + " mm";
int x0 = (myOLED.getWidth() - myOLED.getStringWidth(distStr)) / 2;
myOLED.text(x0, 16, distStr, 1);
// Display min and max
String rangeStr = String(int(minDistance)) + "-" + String(int(maxDistance)) + "mm";
x0 = (myOLED.getWidth() - myOLED.getStringWidth(rangeStr)) / 2;
myOLED.text(x0, 32, rangeStr, 1);
// Update the display
myOLED.display();
delay(100);
}
How To Change Distance Parameters
Customizing your parking distance is easy with the SparkFun Qwiic Twist. Here's how to adjust the minimum and maximum distances for the green LED indicator:
- Initial Setup:
- The default minimum distance is set to 50mm.
- The default maximum distance is set to 250mm.
- Adjusting Parameters:
- Use the Qwiic Twist to change either the minimum or maximum distance.
- Rotate the Twist clockwise to increase the distance, or counterclockwise to decrease it.
- Switching Between Min and Max:
- Click the Twist once to toggle between adjusting the minimum and maximum distances.
- The OLED display will indicate which parameter you're currently modifying.
- Green LED Behavior:
- The green LED will illuminate when your vehicle is within the set range.
- Once you go past the minimum distance, the LED will turn off, signaling you to stop.
- Fine-Tuning:
- Experiment with different min/max values to find your ideal parking position.
- Remember, the minimum distance is your stopping point to avoid contact with the wall.
By adjusting these parameters, you can create a custom "sweet spot" for parking your vehicle, ensuring you park at the perfect distance every time. The intuitive twist-and-click interface makes it easy to modify your settings as needed.
Conclusion
Ready to take your garage parking to the next level? If you want to go further and explore a more in-depth implementation, we highly recommend checking out Rob's detailed video tutorial on creating a comprehensive stop-and-go light system for your garage.
You can find the full video tutorial at: SparkFun News - Garage Parking Tutorial
Rob's video will provide advanced techniques and creative approaches to enhance your distance sensing setup, potentially unlocking even more innovative ways to improve your parking precision.