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.
For this project, I used the following hardware:
- Raspberry Pi Zero W
- 28BYJ-48 5v Stepper Motor with ULN2003 Board
- 16GB microSD Card
- A grandcentrix flag
- Some cables
Here’s an overview of how to connect the ULN2003 to the Raspberry Pi.
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 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
Add the circuits_gpio dependency to your
Install the dependencies with
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:
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
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
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
This will start an
iex terminal on the Raspberry Pi. Now, start the motor with
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 :)