Erlang Actor Library for Embedded -- An embedded framework from Erlang Solutions
C Erlang Makefile
Switch branches/tags
Clone or download
knewter Merge pull request #29 from mdsebald/patch-1
Fixed read() and write_read() specs
Latest commit 37827e2 Mar 3, 2017
Failed to load latest commit information.
c_src Mega-patch to pull in various updates from forks Apr 4, 2015
examples Update Makefile for examples to use valid path for ebin folder of ALE Apr 21, 2015
src Merge pull request #29 from mdsebald/patch-1 Mar 3, 2017
.gitignore Mega-patch to pull in various updates from forks Apr 4, 2015
.travis.yml Enable Travis CI support Apr 5, 2015
Makefile Add support for build system. Apr 21, 2015 Fixed copyright specs to so that make docs works May 13, 2015 Add support for build system. Apr 21, 2015
rebar.config Mega-patch to pull in various updates from forks Apr 4, 2015
rebar.lock Mega-patch to pull in various updates from forks Apr 4, 2015

Erlang/ALE -- Erlang Actor Library for Embedded

Build Status

Erlang/ALE provides high level abstractions for interfacing to hardware peripherals through I2C, SPI, and GPIOs on embedded platforms.

Getting started

Erlang/ALE supports both rebar3 and build tools. If you're natively compiling Erlang/ALE, everything should work like any other Erlang library. Normally, you would include erlang_ale as a dependency in your rebar.config or Makefile file. If you just want to try it out, do the following:

git clone

Steps for rebar3 build tool:
	rebar3 compile
	rebar3 shell

Steps for build tool:
	make shell

If you're cross-compiling, you'll need to setup your environment so that the right C compiler is called. See the Makefile for the variables that will need to be overridden.


It is possible generate HTML documentation from the self documented source.

cd erlang_ale
make docs

The HTML files can be seen in the erlang_ale/doc folder. Open the index.html file, and read.


The following examples were tested on a Raspberry Pi that was connected to an Erlang Embedded Demo Board. There's nothing special about either the demo board or the Raspberry Pi, so these should work similarly on other embedded Linux platforms.


A GPIO is just a wire that you can use as an input or an output. It can only be one of two values, 0 or 1. A 1 corresponds to a logic high voltage like 3.3 V and a 0 corresponds to 0 V. The actual voltage depends on the hardware.

Here's an example setup:

GPIO schematic

To turn on the LED that's connected to the net labelled PI_GPIO18, you can run the following:

1> {ok, Gpio18} = gpio:start_link(18, output).
{ok, <0.96.0>}

2> gpio:write(Gpio18, 1).

Input works similarly:

3> {ok, Gpio17} = gpio:start_link(17, input).
{ok, <0.97.0>}

4> gpio:read(Gpio17).

%% Push the button down

5> gpio:read(Gpio17).

If you'd like to get a message when the button is pressed or released, register a process using the register_int/1 or register_int/2. Then enable interrupts:

6> gpio:register_int(Gpio17).

7> gpio:set_int(Gpio17, both).

%% Press the button a few times

8> flush().
{gpio_interrupt, 17, rising}
{gpio_interrupt, 17, falling}
{gpio_interrupt, 17, rising}
{gpio_interrupt, 17, falling}


A SPI bus is a common multi-wire bus used to connect components on a circuit board. A clock line drives the timing of sending bits between components. Bits on the MOSI line go from the master (usually the processor running Linux) to the slave, and bits on the MISO line go the other direction. Bits transfer both directions simultaneously. However, much of the time, the protocol used across the SPI bus has a request followed by a response and in these cases, bits going the "wrong" direction are ignored.

The following shows an example ADC that reads from either a temperature sensor on CH0 or a potentiometer on CH1.

SPI schematic

The protocol for talking to the ADC is described in the MCP3203 Datasheet. Sending a 0x64 first reads the temperature and sending a 0x74 reads the potentiometer.

NOTE: Before you try using SPI, make sure that the proper drivers are loaded. For example, on Raspian be sure to run raspi-config to enable SPI.

1> {ok, MySpi} = spi:start_link("spidev0.0", []).
{ok, <0.124.0>}

%% Read the potentiometer

%% Use binary pattern matching to pull out the ADC counts (low 12 bits)
2> <<_:4, Counts:12>> = spi:transfer(MySpi, <<16#74, 16#00>>).
<<1, 197>>

3> Counts.

%% Convert counts to volts (1023 = 3.3 V)
4> Volts = Counts / 1023 * 3.3.


An I2C bus is similar to a SPI bus in function, but uses one less wire. It supports addressing hardware components and bidirectional use of the data line. Be aware that I2C requires some care when hooking up such as making sure to attach the proper pullup resisters to SCL and SDA. If you using a Raspberry Pi, see the clock-stretching bug.

The following shows a bus IO expander connected via I2C to the processor.

I2C schematic

The protocol for talking to the IO expander is described in the MCP23008 Datasheet. Here's a simple example of using it.

NOTE: Before trying the example below, make sure that I2C support has been enabled and that the i2c-dev module has been inserted.

%% On the Raspberry Pi, the IO expander is connected to I2C bus 1 (i2c-1).
%% Its 7-bit address is 0x20. (see datasheet)
1> {ok, IoExpander} = i2c:start_link("i2c-1", 16#20).
{ok, <0.102.0>}

%% By default, all 8 GPIOs are set to inputs. Set the 4 high bits to outputs
%% so that we can toggle the LEDs. (Write 0x0f to register 0x00)
2> i2c:write(IoExpander, <<16#00, 16#0f>>).

%% Turn on the LED attached to bit 4 on the expander. (Write 0x10 to register
%% 0x09)
3> i2c:write(IoExpander, <<16#09, 16#10>>).

%% Read all 11 of the expander's registers to see that the bit 0 switch is
%% the only one on and that the bit 4 LED is on.
4> i2c:write(IoExpander, <<16#00>>).  % Set the next register to be read to 0

5> i2c:read(IoExpander, 11).
<<15, 0, 0, 0, 0, 0, 0, 0, 0, 17, 16>>

%% The 17 in register 9 says that bits 0 and bit 4 are high
%% We could have just read register 9.

6> i2c:write(IoExpander, <<9>>).

7> i2c:read(IoExpander, 1).

%% A simpler way to write the register and read its contents.
8> i2c:write_read(IoExpander, <<9>>, 1).


  1. Where did PWM support go?

The original Erlang/ALE implementation supported PWM on the Raspberry Pi. The implementation was platform-specific and not maintained. After a year of bit rot, it was removed.