|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2026 Paul Guyot <pguyot@kallisys.net> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +%%----------------------------------------------------------------------------- |
| 22 | +%% @doc LIS3DH accelerometer example for Pico. |
| 23 | +%% |
| 24 | +%% Reads X, Y, Z acceleration from a LIS3DH connected via I2C and prints |
| 25 | +%% the values every second. |
| 26 | +%% |
| 27 | +%% Default wiring (Pico pin numbers): |
| 28 | +%% SDA -> GP4 (pin 6) |
| 29 | +%% SCL -> GP5 (pin 7) |
| 30 | +%% |
| 31 | +%% These are the default I2C0 pins on the Pico. |
| 32 | +%% @end |
| 33 | +%%----------------------------------------------------------------------------- |
| 34 | +-module(pico_lis3dh). |
| 35 | +-export([start/0]). |
| 36 | + |
| 37 | +%% I2C pins (I2C0 default on Pico) |
| 38 | +-define(SDA_PIN, 4). |
| 39 | +-define(SCL_PIN, 5). |
| 40 | + |
| 41 | +%% LIS3DH I2C address (0x18 when SDO/SA0 is low, 0x19 when high) |
| 42 | +-define(LIS3DH_ADDR, 16#19). |
| 43 | + |
| 44 | +%% LIS3DH registers |
| 45 | +-define(WHO_AM_I, 16#0F). |
| 46 | +-define(CTRL_REG1, 16#20). |
| 47 | +-define(CTRL_REG4, 16#23). |
| 48 | +-define(OUT_X_L, 16#28). |
| 49 | + |
| 50 | +%% Expected WHO_AM_I response |
| 51 | +-define(LIS3DH_ID, 16#33). |
| 52 | + |
| 53 | +start() -> |
| 54 | + I2C = i2c:open([ |
| 55 | + {scl, ?SCL_PIN}, |
| 56 | + {sda, ?SDA_PIN}, |
| 57 | + {clock_speed_hz, 400000}, |
| 58 | + {peripheral, 0} |
| 59 | + ]), |
| 60 | + case check_who_am_i(I2C) of |
| 61 | + ok -> |
| 62 | + configure(I2C), |
| 63 | + loop(I2C); |
| 64 | + {error, Reason} -> |
| 65 | + console:puts("LIS3DH not found: "), |
| 66 | + console:puts(erlang:atom_to_list(Reason)), |
| 67 | + console:puts("\n") |
| 68 | + end. |
| 69 | + |
| 70 | +check_who_am_i(I2C) -> |
| 71 | + case i2c:read_bytes(I2C, ?LIS3DH_ADDR, ?WHO_AM_I, 1) of |
| 72 | + {ok, <<?LIS3DH_ID:8>>} -> |
| 73 | + console:puts("LIS3DH detected\n"), |
| 74 | + ok; |
| 75 | + {ok, <<Other:8>>} -> |
| 76 | + console:puts("Unexpected WHO_AM_I: "), |
| 77 | + console:puts(erlang:integer_to_list(Other, 16)), |
| 78 | + console:puts("\n"), |
| 79 | + {error, unexpected_id}; |
| 80 | + {error, _} = Error -> |
| 81 | + Error |
| 82 | + end. |
| 83 | + |
| 84 | +configure(I2C) -> |
| 85 | + %% CTRL_REG1: 50 Hz ODR, normal mode, X/Y/Z enabled |
| 86 | + %% Bits: ODR=0100 LPen=0 Zen=1 Yen=1 Xen=1 -> 0x47 |
| 87 | + ok = i2c:write_bytes(I2C, ?LIS3DH_ADDR, ?CTRL_REG1, 16#47), |
| 88 | + %% CTRL_REG4: +/- 2g full scale, high resolution |
| 89 | + %% Bits: BDU=1 BLE=0 FS=00 HR=1 ST=00 SIM=0 -> 0x88 |
| 90 | + ok = i2c:write_bytes(I2C, ?LIS3DH_ADDR, ?CTRL_REG4, 16#88). |
| 91 | + |
| 92 | +loop(I2C) -> |
| 93 | + case read_acceleration(I2C) of |
| 94 | + {ok, {X, Y, Z}} -> |
| 95 | + console:puts("X="), |
| 96 | + console:puts(erlang:integer_to_list(X)), |
| 97 | + console:puts(" Y="), |
| 98 | + console:puts(erlang:integer_to_list(Y)), |
| 99 | + console:puts(" Z="), |
| 100 | + console:puts(erlang:integer_to_list(Z)), |
| 101 | + console:puts("\n"); |
| 102 | + {error, Reason} -> |
| 103 | + console:puts("Read error: "), |
| 104 | + console:puts(erlang:atom_to_list(Reason)), |
| 105 | + console:puts("\n") |
| 106 | + end, |
| 107 | + timer:sleep(1000), |
| 108 | + loop(I2C). |
| 109 | + |
| 110 | +read_acceleration(I2C) -> |
| 111 | + %% Read 6 bytes starting at OUT_X_L with auto-increment (bit 7 set) |
| 112 | + case i2c:read_bytes(I2C, ?LIS3DH_ADDR, ?OUT_X_L bor 16#80, 6) of |
| 113 | + {ok, <<XL:8, XH:8/signed, YL:8, YH:8/signed, ZL:8, ZH:8/signed>>} -> |
| 114 | + %% 12-bit left-justified in high-resolution mode: shift right by 4 |
| 115 | + X = ((XH bsl 8) bor XL) bsr 4, |
| 116 | + Y = ((YH bsl 8) bor YL) bsr 4, |
| 117 | + Z = ((ZH bsl 8) bor ZL) bsr 4, |
| 118 | + {ok, {X, Y, Z}}; |
| 119 | + {error, _} = Error -> |
| 120 | + Error |
| 121 | + end. |
0 commit comments