Skip to content

Commit 6675cc7

Browse files
authored
Merge pull request #18 from matthias-bs/copilot/add-i2c-sensor-example
Add CustomI2CPins example with configurable GPIO and clock frequency
2 parents 29f0edf + 17d2ae5 commit 6675cc7

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,62 @@ void loop() {
104104
}
105105
```
106106

107+
### Custom I2C Pins Example
108+
109+
For ESP32/ESP8266 boards, you can use custom GPIO pins for I2C communication and set a custom clock frequency:
110+
111+
```cpp
112+
#include <Wire.h>
113+
#include <DYP_R01CW.h>
114+
115+
// Custom I2C pins
116+
#define CUSTOM_SDA_PIN 25
117+
#define CUSTOM_SCL_PIN 26
118+
119+
// Custom I2C clock frequency (10000 Hz = 10 kHz)
120+
#define CUSTOM_I2C_CLOCK_FREQ 10000
121+
122+
DYP_R01CW sensor;
123+
124+
void setup() {
125+
Serial.begin(115200);
126+
127+
// Initialize I2C with custom pins (ESP32/ESP8266 specific)
128+
Wire.begin(CUSTOM_SDA_PIN, CUSTOM_SCL_PIN);
129+
130+
// Set custom I2C clock frequency
131+
Wire.setClock(CUSTOM_I2C_CLOCK_FREQ);
132+
133+
// Initialize the sensor (pass &Wire to use the configured Wire instance)
134+
if (!sensor.begin(&Wire)) {
135+
Serial.println("ERROR: Could not find sensor!");
136+
while (1) {
137+
delay(1000); // Prevent watchdog issues
138+
}
139+
}
140+
141+
Serial.println("Sensor initialized!");
142+
}
143+
144+
void loop() {
145+
int16_t distance = sensor.readDistance();
146+
147+
if (distance >= 0) {
148+
Serial.print("Distance: ");
149+
Serial.print(distance);
150+
Serial.println(" mm");
151+
} else {
152+
Serial.println("ERROR: Failed to read distance");
153+
}
154+
155+
delay(500);
156+
}
157+
```
158+
159+
**Note:** Custom I2C pins are supported on ESP32 and ESP8266. For other Arduino boards (e.g., Uno, Mega), use the default hardware I2C pins.
160+
161+
**Tip:** A reduced clock frequency (like 10 kHz shown above) can help improve reliability when using long wires or in noisy EMC (Electromagnetic Compatibility) environments by reducing signal integrity issues.
162+
107163
## API Reference
108164

109165
### Constructor
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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

Comments
 (0)