Garage Distance Sensor
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); }