fhunleth
Circuits GPIO, I2C and SPI - Use Elixir to control hardware and read sensors
Elixir Circuits is a set of libraries for interacting with hardware. We previously announced Circuits.UART, and now we’re ready to announce Circuits.GPIO, Circuits.I2C, and Circuits.SPI. Here are examples of devices that you can control with the libraries:
- Circuits.GPIO - buttons, switches, and lights
- Circuits.I2C - accelerometers, gyroscopes, compasses, some thermometers, displays, lighting and motor controllers and more
- Circuits.SPI - analog to digital converters, small displays, and custom programs running on FPGAs
These libraries work on Raspberry Pis with Raspbian, other embedded Linux devices and the official Nerves platforms. The libraries also include test backends for compilation and limited testing on development machines.
The Circuits GPIO, I2C, and SPI libraries can be thought of as Elixir ALE 2.0. During the development of Elixir ALE 2.0, we decided to break apart the library based on hardware interface. The APIs are similar to Elixir ALE, but different enough that if you are currently using ALE, we recommend that you review the porting guides in the documentation. All users of Elixir ALE are highly encouraged to update their projects to the appropriate Circuits libraries.
Changes from Elixir ALE 1.0 include:
- Circuits GPIO, I2C, and SPI now use NIFs. The performance improvement is noticeable - especially for GPIOs. Yes, there’s a trade off in stability. Based on our experience with ALE, we felt we could achieve a similar level of stability with NIFs and support use cases that were limited by ALE’s performance.
- Various API improvements and conveniences like supporting iodata when writing to the I2C and SPI buses and more configuration in open calls to support “glitch-free” initialization
- Support for internal pull-ups and pull-downs on GPIOs on Raspberry Pis. This saves you from having to connect a resister to buttons
- More user-friendly support for finding devices on I2C buses
- Timestamped GPIO interrupt messages to improve pulse measurement precision
Check out https://elixir-circuits.github.io/ for more information.
We really enjoy using the Elixir programming language with hardware and we hope that you will too.
Happy hardware hacking!
Frank Hunleth
Mark Sebald
Matt Ludwigs
Most Liked
fhunleth
We just released circuits_gpio v0.4.1 which has important updates for Raspbian. All Circuits projects should work well on Raspbian now.
While many of us primarily work with Nerves, we want Circuits to have a great out-of-box experience with Raspbian and other Linux distros running on Raspberry Pis, Beaglebones, etc. If you’re using one of these, please let us know (via GitHub issue) if anything doesn’t work or is confusing.
fhunleth
Circuits.GPIO 2.1.3 is now available that fixes a serious issue that caused interrupt messages to stop being sent. Please update when you can. I went ahead and marked the hex packages for versions prior to 2.1.3 as invalid since this issue can be quite hard to track down. Thanks to @akoutmos for providing me a Nerves Livebook that reproduced it and @gus who also was able to reproduce and confirm the fix in a different context.
The bug affects anyone using two or more GPIOs in interrupt mode. If you’re not using interrupts or use only one, then upgrading is less important. In practice, the issue occurred when a GenServer using one GPIO in interrupt mode crashes and restarts. Another open GPIO in interrupt mode could stop getting interrupt messages. Internally, the ordering of events matters, so it was possible to get inconsistent outcomes.
fhunleth
circuits_gpio v0.4.2 is available now. It has an important update that makes GPIO pullup and pulldown settings work with the Raspberry Pi 4. This feature is frequently used by Raspberry Pi hats that have buttons on them.
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









