Elixir Circuits I2C로 온도 측정 (ADT7410)
17529 단어 RaspberryPi온도 센서ADT7410I2CElixir
1. 소개
Elixir를 사용하여 RaspberryPi의 하드웨어 제어 연습을 시도했습니다.
이번에는 Elixir Circuits.I2C 에서 온도 센서 ADT7410을 제어해 봅니다.
운영 환경
여기서 하드웨어 환경은 다음을 가정합니다.
명령줄
$ uname -a
Linux raspi4 5.4.51-v7l+ #1333 SMP Mon Aug 10 16:51:40 BST 2020 armv7l GNU/Linux
$ iex
Erlang/OTP 22 [erts-10.5.4] [source] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Interactive Elixir (1.10.3) - press Ctrl+C to exit (type h() ENTER for help)
2. 하드웨어
(1) 센서
아날로그 디바이스의 I2C 접속 온도 센서 ADT7410을 사용합니다.
(2) GROVE 프로토 실드
※센서와 RaspberryPi를 직결하는 경우는 읽어 날려 주세요
Raspberry Pi 본체에는 Grove Base HAT 을 사용하고 싶기 때문에, 이번 사용하는 센서를 GROVE 규격에 대응시킵니다.
GROVE - 프로토실드 을 사용하면 작은 범용 기판과 GROVE 커넥터 케이블이 함께 제공되어 쉽게 GROVE 지원 센서를 만들 수 있습니다.
배치·배선 예
ADT7410 단자
GROVE 커넥터 기호
VDD
V
SCL
S0 (SCL)
SDA
S1 (SDA)
GND
G
RaspberryPi에 연결한 후
i2cdetect
를 실행하여 통신할 수 있는지 확인합니다.0x48
0x04
(Grove Base HAT를 사용하는 경우 표시) 명령줄
$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
3. 소프트웨어
(1)아래 준비
프로젝트를 만듭니다.
명령줄$ cd gitwork
$ mix new i2ctemp
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/i2ctemp.ex
* creating test
* creating test/test_helper.exs
* creating test/i2ctemp_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd i2ctemp
mix test
Run "mix help" for more commands.
$ cd i2ctemp/
mix.exs를 편집합니다.
mix.exsdefmodule I2ctemp.MixProject do
use Mix.Project
def project do
[
app: :i2ctemp,
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
#ここの先頭のコメントアウトを外す↓
{:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
#ここを追加↓
{:circuits_i2c, "~> 0.3", override: true}
]
end
end
mix deps.get
를 실행합니다.
명령줄$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
New:
circuits_i2c 0.3.6
dep_from_hexpm 0.3.0
elixir_make 0.6.1
* Getting dep_from_hexpm (Hex package)
* Getting circuits_i2c (Hex package)
* Getting elixir_make (Hex package)
$
(2) 코드
defmodule I2ctemp do
@moduledoc """
Documentation for `I2ctemp`.
"""
#ここから追加↓
alias Circuits.I2C
require Logger
use Bitwise
@doc """
温度の取得
## Examples
iex> I2ctemp.gettemp()
"""
def gettemp() do
# I2Cアドレス
address_adt7410 = 0x48
# I2Cを開く
{:ok, ref} = Circuits.I2C.open("i2c-1")
# 初期化コマンド:13bitモードに設定
Circuits.I2C.write(ref, address_adt7410, <<0x03, 0x00>>)
# 現在温度を読み出す
{_, <<rawdata_h, rawdata_l>>} =
Circuits.I2C.write_read(ref, address_adt7410, <<0x00>>, 2)
#上位ビットと下位ビットの和を求める
data_raw = rawdata_h * 256 + rawdata_l
#3ビットだけシフトする
data_raw = data_raw >>> 3
#摂氏温度に変換する
data = data_raw / 16
#表示
Logger.info(" RAW H: #{rawdata_h * 256}, RAW L: #{rawdata_l}, RAW: #{data_raw}, temp(degree Celsius): #{data}")
end
#ここまで追加↑
@doc """
Hello world.
## Examples
iex> I2ctemp.hello()
:world
"""
def hello do
:world
end
end
코드 안에 비트 연산이 있으므로 Bitwise 을 use로 호출하십시오.
(3) 실행 결과
temp(degree Celsius):
섭씨 온도가 표시됩니다.
(RAW ...는 취득한 원시의 수치를 표시하고 있습니다)
명령줄 $ iex -S mix
Erlang/OTP 22 [erts-10.5.4] [source] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Remember to keep good posture and stay hydrated!
Interactive Elixir (1.10.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> I2ctemp.gettemp()
01:08:08.202 [info] RAW H: 2560, RAW L: 200, RAW: 345, temp(degree Celsius): 21.5625
:ok
iex(2)> I2ctemp.gettemp()
01:08:14.776 [info] RAW H: 3328, RAW L: 64, RAW: 424, temp(degree Celsius): 26.5
:ok
iex(3)> I2ctemp.gettemp()
01:08:22.032 [info] RAW H: 3840, RAW L: 8, RAW: 481, temp(degree Celsius): 30.0625
:ok
iex(4)>
4. 끝에
Elixir Circuits.I2C를 사용하여 온도 센서를 쉽게 제어하는 예제를 소개했습니다.
참고 자료
$ cd gitwork
$ mix new i2ctemp
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/i2ctemp.ex
* creating test
* creating test/test_helper.exs
* creating test/i2ctemp_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd i2ctemp
mix test
Run "mix help" for more commands.
$ cd i2ctemp/
defmodule I2ctemp.MixProject do
use Mix.Project
def project do
[
app: :i2ctemp,
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
#ここの先頭のコメントアウトを外す↓
{:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
#ここを追加↓
{:circuits_i2c, "~> 0.3", override: true}
]
end
end
$ mix deps.get
Resolving Hex dependencies...
Dependency resolution completed:
New:
circuits_i2c 0.3.6
dep_from_hexpm 0.3.0
elixir_make 0.6.1
* Getting dep_from_hexpm (Hex package)
* Getting circuits_i2c (Hex package)
* Getting elixir_make (Hex package)
$
defmodule I2ctemp do
@moduledoc """
Documentation for `I2ctemp`.
"""
#ここから追加↓
alias Circuits.I2C
require Logger
use Bitwise
@doc """
温度の取得
## Examples
iex> I2ctemp.gettemp()
"""
def gettemp() do
# I2Cアドレス
address_adt7410 = 0x48
# I2Cを開く
{:ok, ref} = Circuits.I2C.open("i2c-1")
# 初期化コマンド:13bitモードに設定
Circuits.I2C.write(ref, address_adt7410, <<0x03, 0x00>>)
# 現在温度を読み出す
{_, <<rawdata_h, rawdata_l>>} =
Circuits.I2C.write_read(ref, address_adt7410, <<0x00>>, 2)
#上位ビットと下位ビットの和を求める
data_raw = rawdata_h * 256 + rawdata_l
#3ビットだけシフトする
data_raw = data_raw >>> 3
#摂氏温度に変換する
data = data_raw / 16
#表示
Logger.info(" RAW H: #{rawdata_h * 256}, RAW L: #{rawdata_l}, RAW: #{data_raw}, temp(degree Celsius): #{data}")
end
#ここまで追加↑
@doc """
Hello world.
## Examples
iex> I2ctemp.hello()
:world
"""
def hello do
:world
end
end
$ iex -S mix
Erlang/OTP 22 [erts-10.5.4] [source] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]
Remember to keep good posture and stay hydrated!
Interactive Elixir (1.10.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> I2ctemp.gettemp()
01:08:08.202 [info] RAW H: 2560, RAW L: 200, RAW: 345, temp(degree Celsius): 21.5625
:ok
iex(2)> I2ctemp.gettemp()
01:08:14.776 [info] RAW H: 3328, RAW L: 64, RAW: 424, temp(degree Celsius): 26.5
:ok
iex(3)> I2ctemp.gettemp()
01:08:22.032 [info] RAW H: 3840, RAW L: 8, RAW: 481, temp(degree Celsius): 30.0625
:ok
iex(4)>
Elixir Circuits.I2C를 사용하여 온도 센서를 쉽게 제어하는 예제를 소개했습니다.
참고 자료
Reference
이 문제에 관하여(Elixir Circuits I2C로 온도 측정 (ADT7410)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/myasu/items/76c9a3d16433358c082e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)