Use DYP-A01 ultrasonic distance sensor in Elixir.
# List available serial ports
iex> Circuits.UART.enumerate
%{
"ttyAMA0" => %{},
"ttyS0" => %{},
"ttyUSB0" => %{
description: "CP2102 USB to UART Bridge Controller",
manufacturer: "Silicon Labs",
product_id: 60000,
serial_number: "0001",
vendor_id: 4292
}
}
# Start a gen server for interacting with a DYP-A01 sensor on port ttyAMA0
iex> {:ok, pid} = DYPA01.start_link(port_name: "ttyAMA0")
{:ok, #PID<0.1407.0>}
# Measure the current distance
iex> DYPA01.measure(pid)
{:ok, %DYPA01.Measurement{distance_mm: 1680, timestamp_ms: 321793}}
This library assumes the "UART auto output" mode, which is enabled when the sensor's "RX" cable is unused. For example, when connecting the sensor to Raspberry Pi's GPIO pins, use only the following three pins.
Cable number | Cable name | Raspberry Pi |
---|---|---|
1 | VCC | GPIO 4 (5V) |
2 | GND | GPIO 6 (GND) |
3 | RX | |
4 | TX | GPIO 8 (RX) |