Bridge the Gap: Bring your Software to life with Elixir and Nerves

Peter Ullrich
3 min readJul 31, 2019

--

Writing code is fun, but nothing tops seeing your code moving things in the real world. That’s why I tested out the Nerves library and used $20 of Raspberry Pi utensils to let software wave a flag for me.

A Raspberry Pi Zero W with a 28BYJ-48 Stepper Motor waving a flag

The Hardware

For this project, I used the following hardware:

  1. Raspberry Pi Zero W
  2. 28BYJ-48 5v Stepper Motor with ULN2003 Board
  3. 16GB microSD Card
  4. A grandcentrix flag
  5. Some cables

The Wiring

Here’s an overview of how to connect the ULN2003 to the Raspberry Pi.

Wiring schematic of Raspberry Pi and ULN2003 Board

After you connect the ULN2003 Board to the Pi, connect the Stepper Motor to the ULN2003 and the Pi to your Computer. IMPORTANT: Connect the Pi to your Computer using the USB port and not the PWR port. Otherwise, you will not be able to transfer or execute code from your Computer on the Pi.

Also, make sure to connect the wires properly. The LEDs on the ULN2003 Board should glow red once you start the stepper motor.

Setting up the Project

You can find the full code on GitHub. Here’s how you set up the project from scratch.

If you haven’t already, first install the Nerves dependencies for your operating system

# On macOS, run:
brew update
brew install fwup squashfs coreutils xz
# On Linux, run:
sudo apt install build-essential automake \
autoconf git squashfs-tools ssh-askpass

Then install the Nerves Bootstrap library

mix archive.install hex nerves_bootstrap

Next, create a new Nerves project and install its dependencies with

mix nerves.new move_it
cd move_it

Add the circuits_gpio dependency to your mix.exs file

Install the dependencies with

export MIX_TARGET=rpi0
mix deps.get

You should now have a fully set up project. As a last step, enable the logger by changing the rootfs_overlay/etc/iex.exs like this:

The Code

Open the lib/move_it/move_it.ex file and fill it with the following code:

The code above first opens the GPIO pins with the GPIO.open/2 function and then starts the motor by sequentially enabling and disabling the Pins. This causes the stepper motor to move one step forward each time one of the pins is enabled. You can adjust how many steps the motor will make by defining the countparameter when starting the script with MoveIt.start(count)

Setting up the Raspberry Pi

First, insert the SD card into your computer.

Now, bundle the code into a firmware and copy it to the SD Card with the commands

mix firmware
mix firmware.burn

Nerves “should” be able to find your SD card and will ask you to confirm it. If it doesn’t find your SD card, you can supply the mount point with the -d option like so:

# For example
mix firmware.burn -d /dev/rdisk2

After the copy process is completed, insert the SD Card into your Raspberry Pi and connect the Pi with the USB cable to your computer.

Getting Things moving

Now, the thrilling part comes. Connect your computer to the Raspberry Pi with

ssh nerves.local

This will start an iex terminal on the Raspberry Pi. Now, start the motor with

MoveIt.start(1200)

The motor should turn, you should be filled with awe, and in the distance you will hear the appreciative clapping of Richard Stallman for using open-source software :)

--

--

Peter Ullrich

Peter ist ein sportbegeisterter Elixir-Entwickler bei Studitemps, radfahrender Afrikaliebhaber und leidenschaftlicher Teiler seines Wissens.