-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RCWL-9620 I2C ultrasonic distance sensor
- Loading branch information
Showing
4 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |