Circuiits.UART problems in RPI0


I have been doing tests with Circuits.UART without result and without receiving any communication in ttyS0 or ttyAMA0

It seems that in Raspberry Pi Z the default port associated with GPIO 14 and 15 serial communication is ttyS0 and what worked for me on raspbian was to disable the console service of the ttyS0 port by stopping the service with sudo systemctl stop serial-getty@ttyS0.service and remove the line console = serial0,115200 from cmdline.txt

I have also seen in Nerves that in “config.txt” it is overlaying ttyAMA0 on ttyS0 with dtoverlay = pi3-miniuart-bt so that the GPIO serial port is ttyAMA0.

According to the documentation that I have read to use serial communication over GPIO without problems it is necessary to deactivate the console through raspi-config .

Configuring The GPIO Serial Port On Raspbian Jessie and Stretch Including Pi 3 and 4 - Spell Foundry)

I have tried to modify cmdline.txt and config.txt removing the overlap dtoverlay = pi3-miniuart-bt and console=serial0,115200 editing the files directly on my MicroSD but it seems to affect how ssh communications are being mapped so I have not been able to log back in with ssh. If I undo the modifications by ssh circuits@ip or via USB ssh circuits@nerves.local I can log back into the system.

So I don’t know where to continue testing.

I think the key is to disable the serial port console, is there any way in nerves to stop this service or run sudo systemctl stop serial-getty@ttyAMA0.service for the ttyAMA0 port?

I have serial comm working over ttyAMA0 with nerves rpi0 out of the box - so give that a try…
afaik there shouldn’t be any need to change things around…

Hi @outlog. Thanks for the encouragement, I have used the example methods of the official documentation, without result. I am trying to send AT commands to a NB-IoT module connected via GPIO serial pins. But my little experience with Elixir and Nerves is making it very difficult to debug what is happening with the communication. Some sample use of the ttyAMA0 or some method to debug the port will be of great help.

check the wiring first and foremost - also ensure you have solid connection on the wires eg not flimsy breadboarding etc.
(also check that the wires are crossed correctly eg. nerves rx is connected to device tx and vice versa - of course except for those boards that try to be helpful by flipping the rx/tx - sic!)

start out in active mode… ssh to the nerves system and try:

{:ok, uart_pid} = Circuits.UART.start_link, "ttyAMA0", speed: 9600,active: true)

change the speed to whatever the device is communicating…

enter flush in the console to see received messages (since you are in active mode)

then send a command to the device eg:
Circuits.UART.write(uart_pid, "\xfe\x04\x00\x03\x00\x01\xd5\xc5" )
(obviously change the command to something relevant to device…

and do a flush again to see received messages…

find the datasheet for the device - and especially also find multiple arduino/python libs and find one where the code and commands are obvious and understandable…

I’ve attached code below that I use for a Senseair S8 (co2 sensor)… not cleaned up at all and far from perfect - sorry - I see it even has other irrelevant code for talking with a renogy solar controller lol…

but hopefully it can inspire/encourage you… do pls post more about the device etc. and any code that materializes…

wip code example senseair S8:
It’s started in application.ex in the supervision tree eg:

    children = [
       {Fw.Senseair, %{name: "sensor_1", tty: "ttyAMA0", speed: 9600, blynk: "blynk", virtual_pin: 5}},

the module itself…

defmodule Fw.Senseair do
  @moduledoc """
  Documentation for Senseair.
  use GenServer, restart: :permanent #, max_restarts: :infinity

  require Logger

  defstruct name: 0,
            uart_pid: 0,
            co2: []
  @name "sensor_1"

  @tty "ttyAMA0"
  @speed 9600


  @s8_co2            "\xfe\x04\x00\x03\x00\x01\xd5\xc5" 
  # returns <<254, 4, 2, 1, 147, 236, 217>>
  # 1 * 256 + 147 => 403 ppm CO2

  @s8_fwver          "\xfe\x04\x00\x1c\x00\x01\xe4\x03" 
  # returns <<254, 4, 2, 1, 79, 237, 64>>
  # "Firmware: %d.%d", buf[3], buf[4]); eg: 1.79

  @cmd_read_CO22      "\xFE\x44\x00\x08\x02\x9F\x25" # returns  <<254, 68, 2, 1, 147, 236, 217>> calc same as s8_co2
  @s8_id_hi          "\xfe\x04\x00\x1d\x00\x01\xb5\xc3" # returns <<254, 4, 2, 7, 36, 175, 15>>
  @s8_id_lo         "\xfe\x04\x00\x1e\x00\x01\x45\xc3"  # returns <<254, 4, 2, 84, 44, 146, 57>>

  @cmd_read_RH       "\xFE\x44\x00\x14\x02\x97\xE5"    # returns  <<254, 68, 2, 6, 7, 250, 134>> (256 * 6 + 7 ) / 100 => ~15% (not real, internal?) 
  @cmd_init          "\xFE\x41\x00\x60\x01\x35\xE8\x53" # NOT TRIED
  # special command 0x7C - param: 0x6 - CO2 background calibration 0x7 - CO2 zero/nitrogen calibration
  @reset_cali_ack          "\xFE\x06\x00\x00\x00\x00\x9D\xC5"
  @start_cali       "\xFE\x06\x00\x01\x7C\x06\x6C\xC7" #NOT TRIED
  # wait at least 2 secs - read_cali_ack should return 1 (20 in hex)
  @read_cali_ack          "\xFE\x03\x00\x00\x00\x01\x90\x05" # returns  <<254, 3, 2, 0, 0, 172, 80>>

  @read_abc_duration  "\xFE\x03\x00\x1F\x00\x01\xA1\xC3" # returns <<254, 3, 2, 0, 180, 172, 39>> (256*0+180) => 180 hours /24 =>  7,5 days
  # set abc not included see hardware(fw) issue

  @cmd_read_Temp     "\xFE\x44\x00\x12\x02\x94\x45" #returns <<254, 68, 2, 0, 0, 184, 228>>  (256*0+0) ZERO
  @cmd_read_det_Temp  "\xFE\x44\x00\x0A\x02\x9E\x45" # returns <<254, 68, 2, 10, 89, 126, 126>> (10*256+89)/100 => 26.49 TOO HIGH internal temp?
  @cmd_read_Temp2     "\xfe\x04\x00\x04\x00\x01\x64\x04" # returns <<254, 132, 2, 242, 241>> error?

#iex(19)> data = "\xFE\x44\x00\x08\x02\x9F\x25"
#<<254, 68, 0, 8, 2, 159, 37>>
#iex(20)> data_size = byte_size(data) - 2
#iex(21)> <<head::binary-size(data_size), rest::binary>> = data
#<<254, 68, 0, 8, 2, 159, 37>>
#iex(22)> calc_crc = CRC.crc(:crc_16_modbus, head)
#iex(23)> << big, small >> = rest
#<<159, 37>>
#iex(24)> rec_crc = big + small*256
#iex(25)> head
#<<254, 68, 0, 8, 2>>
#iex(26)> rem(9631, 256)
#iex(27)> div(9631, 256)

  # Public Interface

  def start_link(args = %{name: name, tty: tty, speed: speed}, opts \\ []) do
    registry_name = via_tuple(name)
    GenServer.start_link(__MODULE__, {args, name}, name: registry_name)

  defp via_tuple(name) do
    {:via, Registry, {:uart_registry, name}}

  def read_co2() do
    #change to?, {:read_co2, @target}), "read_co2")

  def test() do, "read_co22")

  def read_raw(data) do, {"read_raw", data})

  # GenServer Callbacks

  def init({%{tty: tty, speed: speed}, name}) do
     # stop other pids on same port
     |> Enum.filter(fn {_, port} -> port == tty end) 
     |> Enum.each(fn { pid, _ } -> Circuits.UART.stop(pid) end)

     {:ok, uart_pid} = Circuits.UART.start_link
     #rx_framing_timeout: 200, tty, speed: speed,active: false)
     {:ok, %__MODULE__{ name: name, uart_pid: uart_pid }}

  #host testing
  def handle_call({:read_co2, target}, _from, state) when target == :host do
    co2 = 400

    data = %{co2: co2, time:!("Europe/Copenhagen")}
    {:reply, {:ok, co2}, Map.put(state, :co2, [data | state.co2]) }

  def handle_call({:read_co2, target}, _from, state) do
    Circuits.UART.write(state.uart_pid, @s8_co2)
    result = case, 1000) do
      {:ok, read_values} -> read_values
      _ -> nil
    if correct_crc?(result) do
      co2 = case result do
        <<254, 4, 2,  big, small, _crc1, _crc2>> ->
          co2 = big *256 + small
        _ -> nil
      {:reply, {:ok, co2}, state}
      {:reply, {:error, "unable"}, state}

  def handle_call("read_co22", _from, state) do
    if == :host do

    # copy/paste string from manual
    #"01 03 000C 0008 840F" 
    #|> String.replace(" ", "") 
    #|> String.codepoints 
    #|> Enum.chunk_every(2) 
    #|> Enum.reduce(<<>>, fn x, acc -> acc <> Base.decode16!(x) end)

    #raw = "\x01\x03\x01\x01\x00\x01\xD4\x36"  #voltage battery
    #raw = "\x01\x03\x00\x14\x00\x04\x04\x0D" # giver <<1, 3, 8, 0, 1, 3, 0, 0, 0, 5, 0, 134, 116>>
    raw = "\x01\x03\x00\x0C\x00\x08\x84\x0F" # model name

    Circuits.UART.write(state.uart_pid, raw)
    result = case, 1000) do
      {:ok, read_values} -> read_values
      _ -> nil

    result2 = case, 1000) do
      {:ok, ""} -> result
      {:ok, read_values} -> result <> read_values
      _ -> <<>>

    result3 = case, 1000) do
      {:ok, ""} -> result2
      {:ok, read_values} -> result2 <> read_values
      _ -> <<>>
    if correct_crc?(result3) do
      co2 = case result3 do
        <<1, 3, 2, 0, voltage, _crc1, _crc2>> ->
        <<1, 3, x, tail :: binary>> -> # model name
           <<head::binary-size(x), rest::binary-size(2)>> = tail
        _ -> nil
      {:reply, {:ok, co2}, state}
      {:reply, {:error, "unable"}, state}

  def handle_call({"read_raw", data}, _from, state) do
    Circuits.UART.write(state.uart_pid, data)

    read =, 1000)
    #result = case read do
    #  {:ok, read_values} -> read_values
    #  _ -> nil
    #if correct_crc?(result) do
    #  {:reply, {:ok, result}, state}
    #  {:reply, {:error, "raw_unable"}, state}
    {:reply, {:ok, "wrote_raw"}, state}

  # handle any unknow calls
  def handle_call(msg, _from, state) do"Unknown handle_cast msg: #{inspect msg}")
    {:reply, "unknown call message", state}

  # for active mode NOT USED HERE
  def handle_info({:circuits_uart, _tty, data}, state) do"Got data on serial:#{inspect data}")
    {:noreply, state}

  defp correct_crc?(data) when data == nil or data == "" do

  defp correct_crc?(data) do
    data_size = byte_size(data) - 2
    <<head::binary-size(data_size), rest::binary>> = data 
    << big, small >> = rest
    CRC.crc(:crc_16_modbus, head) == big + small * 256


Hello @outlog , thank you very much for your help and encouragement.

I have tried to use your example, I got no results, through serial GPIO. I have also tried to emulate the same setup that works for me in python ttyS0 as miniUART modifying in Nerves cmdline.txt and config.txt to see if I could leave the original configuration and deactivate the serial console, which is what the manufacturer indicates. But also without result in Nerves. I’ll try to get a logic analyzer to see if I get something on serial GPIO pins. And try to see what encoding is using nerves in the sending and receiving of text strings.

Although I will also try instead of porting from python to Elixir the entire library. I will try to do the setup by USB port and microusb cable. And try using ppp protocol, with Vintage Net Mobile as a Custom Modem. Which I think will be more compatible with the entire Nerves and Nerves Hub environment for the distribution of Firmware, which is the final goal. Although I’m still trying to put all the pieces of the puzzle together and see which is the best setup.

The example code in python for mqtt communication and the results of the tests is:

The result is the same as nerves default setup, in active mode the same, flush does’t give any result:

Python sample code:

import RPi.GPIO as GPIO
import serial
import time

ser = serial.Serial('/dev/ttyS0',9600)

power_key = 4
rec_buff = ''
Message = 'SomePubSub'

def power_on(power_key):
    print('SIM7080X is starting:')
    print('SIM7080X is ready')

def power_down(power_key):
    print('SIM7080X is loging off:')
    print('Good bye')
def send_at(command,back,timeout):
    rec_buff = ''
    if ser.inWaiting():
        rec_buff =
    if rec_buff != '':
        if back not in rec_buff.decode():
            print(command + ' back:\t' + rec_buff.decode())
            return 0
            return 1
        print(command + ' no response')

        print('wait for signal')
    send_at('AT+CGREG?','+CGREG: 0,1',0.5)
    send_at('AT+CACID=0', 'OK',1)
    print('send message successfully!')
    send_at('AT+CNACT=0,0', 'OK', 1)
    if ser != None:

if ser != None:

The python code works!!

I will post if I have any result with the logic analyzer or by ppp setup and Vintage Net Mobile.

my guess would be that you are not doing the powering “on” off the device on gpio 4 which the python code does - don’t know the specs but assume you need to replicate the def power_on(power_key): with

eg something like: (check if 4 is correct pin - they sometimes have different
{:ok, gpio4} =, :output)

and then a oneline

Circuits.GPIO.write(gpio4, 1); :timer.sleep(2000); Circuits.GPIO.write(gpio4, 0);

depending on specs, maybe use pullup/down eg Circuits.GPIO.set_pull_mode(gpio, pull_mode)

keep us posted on any progress…

It would have been fun and easy to solve if was this, justo to power on. I’m afraid it will be something more complex. With the logic analyzer the only thing I get after having turned on the board with Circuits.GPIO or with the physical button on the board. They are unreadable frames sent every so often without any specific action since I have a trigger that launches the recording when there is movement in the Tx or Rx and I only get start and stop bit missing! even without performing any action on the port. However in python the port is stable and only receives data when I receive or send data, and AT commands are correctly decoded in ASCII.

Logic Analyzer in Nerves. Channels setup Tx GPIO pin on blue in Phyton and Nerves tests.

And the example in python decoding every AT command, and launching the trigger when send the command.

I’ll post more test of the results.

1 Like