DYP-R01CW / DFRobot SEN0590 Laser Ranging Sensor Library for Arduino
An Arduino library for interfacing with the DYP-R01CW (DFRobot SEN0590) laser ranging sensor via I2C (Wire interface). This library provides a simple API for reading distance measurements from the sensor.
- Simple I2C interface using Arduino Wire library
- Distance measurement in millimeters
- Connection checking functionality
- Configurable I2C address support
- Compatible with all Arduino boards that support the Wire library
- Open the Arduino IDE
- Go to Sketch → Include Library → Manage Libraries (or Tools → Manage Libraries)
- Search for "DYP-R01CW"
- Click "Install"
- Download this repository as a ZIP file
- In the Arduino IDE, go to Sketch → Include Library → Add .ZIP Library
- Select the downloaded ZIP file
- The library will be installed and ready to use
Add the following to your platformio.ini:
lib_deps =
https://github.com/matthias-bs/DYP-R01CWThe DYP-R01CW sensor has the following electrical specifications:
| Parameter | Value | Notes |
|---|---|---|
| Supply Voltage | 3.3V - 5.0V | Compatible with both 3.3V and 5V logic |
| Communication Rate | 1 - 100 kbit/s | I2C bus speed |
| Recommended Pull-up Resistors | 3 - 10 kΩ | For I2C SDA and SCL lines |
Connect the DYP-R01CW sensor to your Arduino board:
| Sensor Pin | Arduino Pin |
|---|---|
| VCC | Supply voltage (3.3V - 5.0V) |
| GND | GND |
| SDA | SDA (A4 on Uno, 20 on Mega) |
| SCL | SCL (A5 on Uno, 21 on Mega) |
#include <Wire.h>
#include <DYP_R01CW.h>
// Create sensor object with default I2C address (0xE8 in 8-bit format)
DYP_R01CW sensor;
void setup() {
Serial.begin(115200);
// Initialize the sensor
if (!sensor.begin()) {
Serial.println("ERROR: Could not find sensor!");
while (1);
}
Serial.println("Sensor initialized!");
}
void loop() {
// Read distance from sensor
int16_t distance = sensor.readDistance();
if (distance >= 0) {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" mm");
} else {
Serial.println("ERROR: Failed to read distance");
}
delay(500);
}For ESP32/ESP8266 boards, you can use custom GPIO pins for I2C communication and set a custom clock frequency:
#include <Wire.h>
#include <DYP_R01CW.h>
// Custom I2C pins
#define CUSTOM_SDA_PIN 25
#define CUSTOM_SCL_PIN 26
// Custom I2C clock frequency (10000 Hz = 10 kHz)
#define CUSTOM_I2C_CLOCK_FREQ 10000
DYP_R01CW sensor;
void setup() {
Serial.begin(115200);
// Initialize I2C with custom pins (ESP32/ESP8266 specific)
Wire.begin(CUSTOM_SDA_PIN, CUSTOM_SCL_PIN);
// Set custom I2C clock frequency
Wire.setClock(CUSTOM_I2C_CLOCK_FREQ);
// Initialize the sensor (pass &Wire to use the configured Wire instance)
if (!sensor.begin(&Wire)) {
Serial.println("ERROR: Could not find sensor!");
while (1) {
delay(1000); // Prevent watchdog issues
}
}
Serial.println("Sensor initialized!");
}
void loop() {
int16_t distance = sensor.readDistance();
if (distance >= 0) {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" mm");
} else {
Serial.println("ERROR: Failed to read distance");
}
delay(500);
}Note: Custom I2C pins are supported on ESP32 and ESP8266. For other Arduino boards (e.g., Uno, Mega), use the default hardware I2C pins.
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.
DYP_R01CW(uint8_t addr = DYP_R01CW_DEFAULT_ADDR)Creates a new DYP_R01CW sensor object.
addr: I2C address of the sensor in 8-bit format (default: 0xE8)
bool begin(TwoWire *wire = &Wire)Initializes the sensor and I2C communication.
wire: Pointer to TwoWire object (default: &Wire)- Returns:
trueif initialization successful,falseotherwise
int16_t readDistance()Reads distance measurement from the sensor.
- Returns: Distance in millimeters, or -1 if read failed
bool isConnected()Checks if sensor is connected and responding.
- Returns:
trueif sensor is connected,falseotherwise
uint16_t readSoftwareVersion()Reads the software version number from the sensor.
- Returns: Software version number as a 16-bit value (two bytes from register 0x00), or 0 if read failed
bool setAddress(uint8_t newAddr)Sets the I2C address of the sensor by writing to the slave address register (0x05).
newAddr: New I2C address in 8-bit format - must be one of the 20 supported addresses- Supported addresses (8-bit): 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE, 0xF8, 0xFA, 0xFC, 0xFE
- Even addresses from 0xD0-0xFE, excluding reserved range 0xF0-0xF6
- Returns:
trueif address was set successfully,falseotherwise - Important: The
newAddrparameter uses 8-bit I2C address format (includes R/W bit) - Note: The sensor's default 8-bit address is 0xE8
- Note: The new address takes effect immediately and the object's internal address is updated automatically. You can continue using the same sensor object without creating a new one.
- Note: The sensor stores the new address persistently.
Example:
// Change sensor address from default 0xE8 (8-bit) to 0xD4 (8-bit)
DYP_R01CW sensor; // Uses default address 0xE8
sensor.begin();
if (sensor.setAddress(0xD4)) {
Serial.println("Address changed successfully!");
// No need to create a new object - the same object now uses the new address
int16_t distance = sensor.readDistance(); // Works with new address
} else {
Serial.println("Failed to change address");
}void setDistanceOffset(int16_t offset)Sets the distance offset to be applied to all distance readings.
offset: Offset in millimeters to add to distance readings (can be positive or negative)- Note: The default offset is 0 mm
- Use case: Calibrate the sensor or compensate for mounting distance
Example:
DYP_R01CW sensor;
sensor.begin();
// Apply a positive offset (sensor reads 10mm shorter than actual)
sensor.setDistanceOffset(10);
// Apply a negative offset (sensor reads 5mm longer than actual)
sensor.setDistanceOffset(-5);
// Reset to no offset
sensor.setDistanceOffset(0);int16_t getDistanceOffset()Gets the current distance offset.
- Returns: Current offset in millimeters
Example:
DYP_R01CW sensor;
sensor.begin();
sensor.setDistanceOffset(10);
int16_t currentOffset = sensor.getDistanceOffset(); // Returns 10
Serial.print("Current offset: ");
Serial.print(currentOffset);
Serial.println(" mm");- DYP-R01CW Product Page - Official product page from DYP with technical specifications and product details
- DFRobot SEN0590 Wiki - DFRobot's wiki documentation for the SEN0590 laser ranging sensor (compatible module)
- ESPHome Integration - ESPHome custom component for the SEN0590/DYP-R01CW sensor by sredfern
MIT License - see LICENSE file for details
Matthias Prinke
Contributions are welcome! Please feel free to submit a Pull Request.