[Elixir Nerves] Hello to LCD display
![[Elixir Nerves] Hello to LCD display](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1612787304443%2FvlAh5fblz.gif&w=3840&q=75)
Search for a command to run...
![[Elixir Nerves] Hello to LCD display](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1612787304443%2FvlAh5fblz.gif&w=3840&q=75)
I wrote a script called phx-docker-compose-new that can be used instead of mix phx.new to generate a new Phoenix application. Building the development environment for a Phoenix application using Docker Compose is convenient because you can set up not...
この記事について Raspberry Pi、TensorFlow、Pythonのいずれにも詳しくない筆者が、物体検出をやって楽しんだ成果の記録です。 TensorFlow公式の物体検出のサンプルプログラムを実行します。 動作環境 ボード Raspberry Pi 4 Model B OS Raspberry Pi OS (32-bit または 64-bit) デスクトップ環境 カメラ Raspberry Pi カメラモジュール v2 Python Python ...
One day I was trying to upgrade one of my Phoenix 1.5 apps to Phoenix 1.6. In that app, I customize Bootstrap styles using SCSS. I want to talk about what I found and stumbled on when setting up SCSS. TIL All explained in Phoenix 1.6 Asset Managemen...
This is written in Japanese. I might convert it to English later, maybe. はじめに Elixirのテストでモックを用意するときに利用するElixirパッケージとして、moxが人気です。Elixir作者のJosé Valimさんが作ったからということもありますが、ただモックを用意するだけではなくElixirアプリの構成をより良くするためのアイデアにまで言及されているので、教科書のようなものと思っています。 一言でいうと「その場...
TIL Access supports keyword lists (Keyword) and maps (Map) out of the box, but not structs. There is a reason why Elixir does not implement Access behaviour for structs. Perfer data.key for accessing predefined atom keys of map or struct. Access be...
When we learn a new programming language, usually the first thing we do is to print the "Hello world" text. It seems like the equivalent in the IoT is to blink an LED. After I was successful in blinking an LED, I got curious about how to change the brightness of an LED. Then I started to think about actually printing the "Hello world" text somehow.
I had tons of electronic parts at hand from the starter kit I purchased when I first started playing with the IoT framework Nerves and Raspberry Pi. So it was a great opportunity for me to learn how to use them. Here are some options I came up with:
Eventually I want to do them all but for now I decided to use a Character LCD Display with I2C interface. Some determiners are:
I googled around and watched tons of Youtube vidoes to learn the basics. Here are some of the resources I found helpful:
There was nothing difficult about the wiring; however, many tutorials just use certain libraries that communicates with an LCD when it comes to the programming. Many are written in C, C++ or Python.
On the other hand, I had trouble finding a good Elixir library for controling an LCD, other than Elixir Circuits. The ExLCD library does deal with LCDs; however as of writing it has not been maintained for many years and depends on old dependencies.
At first I forked ExLCD and attempted to build on it. Then I realized that there is nothing special in communicating with an LCD as I learned more about the Hitachi HD44780 LCD. All I need is to understand what its datasheet says. I decided to write my own from scratch, borrowing ideas from other libraries.
Some key things are:
I did experiments using a playground Nerves app. After all, all I needed was:
I took me a while to be able to show the "Hello" text because I did not do the above-mentioned. It was frustrating when I saw a random text on the display.
Once I was able to show the "Hello" text on the display, I wrote tests, improved the code and refactored incrementally. I realized my experimental app can be split into:

Connect:
Set necessary environment variables. Here is a list of target tags that Nerves supports.
$ export WIFI_SSID=_____ # your WIFI id
$ export WIFI_PSK=______ # your WIFI password
$ export MIX_TARGET=rpi0 # your target board
Install dependencies.
$ mix deps.get
Here are some firmware-related commands.
$ mix help | grep firmware
mix burn # Write a firmware image to an SDCard
mix firmware # Build a firmware bundle
mix firmware.burn # Build a firmware bundle and write it to an SDCard
mix firmware.gen.script # Generates a shell script for pushing firmware updates
mix firmware.image # Create a firmware image file
mix firmware.metadata # Print out metadata for the current firmware
mix firmware.patch # Build a firmware patch
mix firmware.unpack # Unpack a firmware bundle for inspection
mix upload # Uploads firmware to a Nerves device over SSH
Create firmware.
$ mix firmware
Insert the SD card into your host machine.
Burn the firmware to that SD card.
$ mix firmware.burn
Insert the SD card into your target board.
Power your target board on.
Check the connection by pinging.
$ ping nerves.local
SSH into your target board, then interactive Elixir shell will start.
$ ssh nerves.local
Interactive Elixir (1.11.2) - press Ctrl+C to exit (type h() ENTER for help)
Toolshed imported. Run h(Toolshed) for more info.
RingLogger is collecting log messages from Elixir and Linux. To see the
messages, either attach the current IEx session to the logger:
RingLogger.attach
or print the next messages in the log:
RingLogger.next
iex(1)>
Run RingLogger.attach then log messages will be outputted.
Follow the instructions in mnishiguchi/lcd_display README to show "Hello" text.

It was a great experience for me to learn I2C allows me to communicate with a peripheral quite easily just reading the datasheets carefully. Reading the datasheet is super important.
I put together my study note on HD44780 LCD, I/O expanders, I2C interface etc as a separate post.