|
| 1 | +/*! |
| 2 | + * @file CustomI2CPins.ino |
| 3 | + * |
| 4 | + * @brief Example for DYP-R01CW laser ranging sensor with custom I2C pins and clock frequency |
| 5 | + * |
| 6 | + * This sketch demonstrates how to use the DYP-R01CW library with custom I2C pins |
| 7 | + * and a custom I2C clock frequency. This is particularly useful for ESP32/ESP8266 boards |
| 8 | + * where you may want to use different GPIO pins for I2C communication. |
| 9 | + * |
| 10 | + * Custom Configuration: |
| 11 | + * - SDA Pin: 25 |
| 12 | + * - SCL Pin: 26 |
| 13 | + * - I2C Clock Frequency: 10000 Hz (10 kHz) |
| 14 | + * |
| 15 | + * @section hardware Hardware Requirements |
| 16 | + * |
| 17 | + * - ESP32 or ESP8266 board (or other Arduino boards supporting custom I2C pins) |
| 18 | + * - DYP-R01CW / DFRobot SEN0590 laser ranging sensor |
| 19 | + * - I2C connection: |
| 20 | + * - SDA to GPIO 25 |
| 21 | + * - SCL to GPIO 26 |
| 22 | + * - VCC to 5V (or 3.3V depending on sensor version) |
| 23 | + * - GND to GND |
| 24 | + * |
| 25 | + * @section notes Notes |
| 26 | + * |
| 27 | + * - This example uses custom I2C pins which is supported on ESP32 and ESP8266. |
| 28 | + * - For other Arduino boards (e.g., Uno, Mega), custom pins may not be supported, |
| 29 | + * and you should use the default hardware I2C pins. |
| 30 | + * - The custom clock frequency of 10000 Hz (10 kHz) is lower than the typical 100000 Hz (100 kHz). |
| 31 | + * - **Note:** A reduced clock frequency can help improve reliability with long wires or in |
| 32 | + * noisy EMC (Electromagnetic Compatibility) environments by reducing signal integrity issues. |
| 33 | + * |
| 34 | + * @section author Author |
| 35 | + * |
| 36 | + * Written by Matthias Prinke |
| 37 | + * |
| 38 | + * @section license License |
| 39 | + * |
| 40 | + * MIT License |
| 41 | + */ |
| 42 | + |
| 43 | +#include <Wire.h> |
| 44 | +#include <DYP_R01CW.h> |
| 45 | + |
| 46 | +// Custom I2C pins |
| 47 | +#define CUSTOM_SDA_PIN 25 |
| 48 | +#define CUSTOM_SCL_PIN 26 |
| 49 | + |
| 50 | +// Custom I2C clock frequency (10000 Hz = 10 kHz) |
| 51 | +#define CUSTOM_I2C_CLOCK_FREQ 10000 |
| 52 | + |
| 53 | +// Create sensor object with default I2C address (0xE8 in 8-bit format) |
| 54 | +DYP_R01CW sensor; |
| 55 | + |
| 56 | +void setup() { |
| 57 | + // Initialize serial communication |
| 58 | + Serial.begin(115200); |
| 59 | + while (!Serial) { |
| 60 | + ; // Wait for serial port to connect (needed for native USB) |
| 61 | + } |
| 62 | + |
| 63 | + Serial.println("DYP-R01CW Laser Ranging Sensor - Custom I2C Pins Example"); |
| 64 | + Serial.println("=========================================================="); |
| 65 | + Serial.print("Custom SDA Pin: "); |
| 66 | + Serial.println(CUSTOM_SDA_PIN); |
| 67 | + Serial.print("Custom SCL Pin: "); |
| 68 | + Serial.println(CUSTOM_SCL_PIN); |
| 69 | + Serial.print("Custom I2C Clock Frequency: "); |
| 70 | + Serial.print(CUSTOM_I2C_CLOCK_FREQ); |
| 71 | + Serial.println(" Hz"); |
| 72 | + Serial.println(); |
| 73 | + Serial.println("Note: Reduced clock frequency improves reliability with long wires"); |
| 74 | + Serial.println(" or in noisy EMC environments."); |
| 75 | + Serial.println(); |
| 76 | + |
| 77 | +#if defined(ESP32) || defined(ESP8266) |
| 78 | + // Initialize I2C with custom pins (ESP32/ESP8266 specific) |
| 79 | + Wire.begin(CUSTOM_SDA_PIN, CUSTOM_SCL_PIN); |
| 80 | + |
| 81 | + // Set custom I2C clock frequency |
| 82 | + Wire.setClock(CUSTOM_I2C_CLOCK_FREQ); |
| 83 | + |
| 84 | + Serial.println("Custom I2C pins and clock frequency configured for ESP32/ESP8266"); |
| 85 | +#else |
| 86 | + // For other boards, use default I2C pins but set custom clock frequency |
| 87 | + Wire.begin(); |
| 88 | + Wire.setClock(CUSTOM_I2C_CLOCK_FREQ); |
| 89 | + |
| 90 | + Serial.println("WARNING: Custom I2C pins are not supported on this board."); |
| 91 | + Serial.println("Using default hardware I2C pins with custom clock frequency."); |
| 92 | +#endif |
| 93 | + |
| 94 | + Serial.println(); |
| 95 | + |
| 96 | + // Initialize the sensor (pass &Wire to use the already configured Wire instance) |
| 97 | + if (!sensor.begin(&Wire)) { |
| 98 | + Serial.println("ERROR: Could not find DYP-R01CW sensor!"); |
| 99 | + Serial.println("Please check wiring and I2C address."); |
| 100 | + Serial.println("Make sure the sensor is connected to the custom pins:"); |
| 101 | + Serial.print(" SDA: GPIO "); |
| 102 | + Serial.println(CUSTOM_SDA_PIN); |
| 103 | + Serial.print(" SCL: GPIO "); |
| 104 | + Serial.println(CUSTOM_SCL_PIN); |
| 105 | + while (1) { |
| 106 | + delay(1000); // Prevent watchdog issues |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + Serial.println("DYP-R01CW sensor initialized successfully!"); |
| 111 | + |
| 112 | + // Read and display software version |
| 113 | + uint16_t version = sensor.readSoftwareVersion(); |
| 114 | + if (version != 0) { |
| 115 | + Serial.print("Software Version: 0x"); |
| 116 | + Serial.println(version, HEX); |
| 117 | + } else { |
| 118 | + Serial.println("WARNING: Could not read software version"); |
| 119 | + } |
| 120 | + |
| 121 | + Serial.println(); |
| 122 | + delay(1000); |
| 123 | +} |
| 124 | + |
| 125 | +void loop() { |
| 126 | + // Read distance from sensor |
| 127 | + int16_t distance = sensor.readDistance(); |
| 128 | + |
| 129 | + // Check if reading was successful |
| 130 | + if (distance >= 0) { |
| 131 | + Serial.print("Distance: "); |
| 132 | + Serial.print(distance); |
| 133 | + Serial.println(" mm"); |
| 134 | + } else { |
| 135 | + Serial.println("ERROR: Failed to read distance"); |
| 136 | + } |
| 137 | + |
| 138 | + // Wait before next reading |
| 139 | + delay(500); |
| 140 | +} |
0 commit comments