Skip to content

Commit

Permalink
Implement RCWL-9620 I2C ultrasonic distance sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
vickash committed Oct 8, 2023
1 parent 52db645 commit c681117
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion HARDWARE.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Polling and reading follow a call and response pattern.
| Name | Status | Interface | Component Class | Notes |
| :--------------- | :------: | :-------- | :--------------- |------ |
| HC-SR04 | :heart: | Digi In/Out | `Sensor::HCSR04` | Ultrasonic, 20-4000mm
| RCWL-9620 | :heart: | I2C | `Sensor::RCWL9260` | Ultrasonic, 20-4500mm
| RCWL-9620 | :green_heart: | I2C | `Sensor::RCWL9260` | Ultrasonic, 20-4500mm
| VL53L0X | :heart: | I2C | `Sensor::VL53L0X` | Laser, 30 - 1000mm
| GP2Y0E03 | :heart: | I2C | `Sensor::GP2Y0E03` | Infrared, 40 - 500mm

Expand Down
15 changes: 15 additions & 0 deletions examples/sensor/rcwl9620.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Example using an RCWL-9620 sensor over I2C to measure distance.
#
require 'bundler/setup'
require 'denko'

board = Denko::Board.new(Denko::Connection::Serial.new)
bus = Denko::I2C::Bus.new(board: board, pin: :SDA)
sensor = Denko::Sensor::RCWL9620.new(bus: bus) # address: 0x57 default

sensor.poll(1) do |distance|
puts "Distance is #{distance} mm"
end

sleep
1 change: 1 addition & 0 deletions lib/denko/sensor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ module Sensor
autoload :AHT20, "#{__dir__}/sensor/aht"
autoload :SHT3X, "#{__dir__}/sensor/sht3x"
autoload :QMP6988, "#{__dir__}/sensor/qmp6988"
autoload :RCWL9620, "#{__dir__}/sensor/rcwl9620"
end
end
34 changes: 34 additions & 0 deletions lib/denko/sensor/rcwl9620.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Denko
module Sensor
class RCWL9620
include I2C::Peripheral
include Behaviors::Poller

def before_initialize(options={})
@i2c_address = 0x57
super(options)
end

def _read
i2c_write(0x01)
sleep(0.120)
i2c_read(nil, 3)
end

def pre_callback_filter(bytes)
# Data is in micrometers, 3 bytes, big-endian.
um = (bytes[0] << 16) + (bytes[1] << 8) + bytes[2]
mm = um / 1000.0

# Limit output between 20 and 4500mm.
if mm > 4500.0
return 4500.0
elsif mm < 20.0
return 20.0
else
return mm
end
end
end
end
end

0 comments on commit c681117

Please sign in to comment.