# Using UART

**URL:** <https://dietpi.com/forum/t/using-uart/2276>\
**Category:** Troubleshooting\
**Created:** [20 May 2018 04:57 UTC](https://dietpi.com/forum/t/using-uart/2276 "2018-05-20T04:57:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrGlasspoole](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@MrGlasspoole](https://dietpi.com/forum/u/MrGlasspoole)\
**Post date:** [20 May 2018 04:57 UTC](https://dietpi.com/forum/t/using-uart/2276/1 "2018-05-20T04:57:35Z")

</div>

I want to connect a device to the Zero’s UART (GPIO 15,16) but can’t figure it out.

Do i have to set Advanced Options \> Serial console: on or off ?  
Do i have to set enable\_uart to 1 (DietPi/config.txt) ?  
Do i have to set CONFIG\_SERIAL\_CONSOLE\_ENABLE to 1 (DietPi/dietpi.txt) ?  
Do i need to run systemctl disable [getty@tty1.service](mailto:getty@tty1.service) ?

In config.txt it says:

> enabled for 1st run only, if you want to keep this setting, please set CONFIG\_SERIAL\_CONSOLE\_ENABLE=1 in dietpi.txt

To me console means showing booting stuff and the like?

Then here on the bottom it says that the Pi3/Zero uses UART for BT:

> **[Enable serial port on Raspberry Pi – Charles's Blog](http://hallard.me/enable-serial-port-on-raspberry-pi/)**

Is it enough to disable BT in dietpi-config?

Then what about boot/cmdline.txt: [Raspberry Pi Documentation - Configuration](https://www.raspberrypi.org/documentation/configuration/uart.md)  
Do i have to manually remove console=serial0,115200 with DietPi?

And what is the right one to choose from /dev?

---

<div class="post-metadata">

**Author:** ![MrGlasspoole](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@MrGlasspoole](https://dietpi.com/forum/u/MrGlasspoole)\
**Post date:** [3 June 2018 14:07 UTC](https://dietpi.com/forum/t/using-uart/2276/2 "2018-06-03T14:07:51Z")

</div>

Nobody?

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [3 June 2018 15:29 UTC](https://dietpi.com/forum/t/using-uart/2276/3 "2018-06-03T15:29:51Z")

</div>

MrGlasspoole  
I never configured a serial console, UART or something like that, but you will get the important points here: [https://github.com/Fourdee/DietPi/issues/1759](https://github.com/Fourdee/DietPi/issues/1759)

- Enable serial console with **dietpi-config** should already do all steps for you, but to be sure:
- Verify that **/DietPi/config.txt** contains the line **enable\_uart=1**.
- Verify that **/DietPi/dietpi.txt** contains the line **CONFIG\_SERIAL\_CONSOLE\_ENABLE=1**.
- NEVER do **systemctl disable [getty@tty1.service](mailto:getty@tty1.service)** as TTY1 is always needed as fallback login console. You might be left without the possibility to login 😉.
- It seems cmdline does not need to contain anything about that, as UART entry in config.txt seems to replace it (as far as I understand dietpi-config code…). **console=serial0,115200** should be not present within the file, as we disable serial console by default (and remove the line by this).
- Disabling BT seems to be recommended. Jep just do it as well via dietpi-config.
- What you mean by /dev? Which entry in which file?

---

<div class="post-metadata">

**Author:** ![MrGlasspoole](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@MrGlasspoole](https://dietpi.com/forum/u/MrGlasspoole)\
**Post date:** [4 June 2018 01:26 UTC](https://dietpi.com/forum/t/using-uart/2276/4 "2018-06-04T01:26:29Z")

</div>

> [@](#):
>
> What you mean by /dev? Which entry in which file?

I mean what serial to use.  
I tried:

```plaintext
screen /dev/tty0 9600,cs7,parodd,-cstopb
screen /dev/tty11 9600,cs7,parodd,-cstopb
screen /dev/ttyAMA0 9600,cs7,parodd,-cstopb
screen /dev/serial0 9600,cs7,parodd,-cstopb

```

But nothing but a blank screen…

I know the Atmega is sending via serial because connecting it to Windows with a USB-Serial adapter i can see everything in PuTTY.

---

<div class="post-metadata">

**Author:** ![pingouin84k](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/p/da6949/32.png) [@pingouin84k](https://dietpi.com/forum/u/pingouin84k)\
**Post date:** [13 September 2018 07:46 UTC](https://dietpi.com/forum/t/using-uart/2276/5 "2018-09-13T07:46:31Z")

</div>

Hi!

I have the same problem.  
Does anyone have a solution to this?

I am with RPi3.  
I manage to have some data exchange but after a while of random duration my python script (using serial module) crash with an error containing:

```nohighlight
device reports readiness to read but returned no data

```

Please advise!

---

<div class="post-metadata">

**Author:** ![boumboauto](https://dietpi.com/forum/letter_avatar_proxy/v4/letter/b/fbc32d/32.png) [@boumboauto](https://dietpi.com/forum/u/boumboauto)\
**Post date:** [30 May 2019 03:45 UTC](https://dietpi.com/forum/t/using-uart/2276/6 "2019-05-30T03:45:36Z")

</div>

Hi,

I have the same problem here. i understand that there are Two UART in the Rasp3 and that one has its own hardware clock, the other is isug the CPU

The bluetooth adapter is using on of these uarts, the GPIO is using another. It is possible to mix and match which UART is used by which service. For example Bluetooth on AMA0 and some hardware on the GPIOs on S0

It’s very hard to understand how to configure :

- Bluetooth using the hardware UART with it"s own clock
- Serial port configured at 1200 bauds, 7 bits on the GPIO forcing the CPU clock at 250 Mhz

Could someone please explain how to do this on a DietPi ditsto?

Many thanks

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [10 June 2019 00:37 UTC](https://dietpi.com/forum/t/using-uart/2276/7 "2019-06-10T00:37:37Z")

</div>

boumboauto  
You already explained everything correctly:

- By default on RPi3 the hardware UART (aka full UART) on /dev/ttyAMA0 is used by the Bluetooth device.
- But if you disable Bluetooth completely, you can still use it for other tasks.
- Otherwise the software UART (aka mini UART) is available on /dev/ttyS0 which is as well by default configured to be available on the GPIO ports, so being the primary UART, on RPi3.
- You can switch this by some config.txt entry, but lets not make things more complicated.

Important when using UART devices for something else then a serial login console, is that you disable serial console usage. On DietPi do:  
dietpi-config \> Advanced Options \> Serial/UART

- In the “Toggle serial consoles” part disable the serial console on /dev/ttyS0.
- This will disable the serial-getty@ttyS0 service which allows login via serial terminal. And it disables boot messages being printed to this device/console.

And jep, having /dev/ttyS0 enabled forces CPU clock at 250 MHz.

---

<div class="post-metadata">

**Author:** ![jhorv](https://dietpi.com/forum/user_avatar/dietpi.com/jhorv/32/4673_2.png) [@jhorv](https://dietpi.com/forum/u/jhorv)\
**Post date:** [17 July 2019 19:02 UTC](https://dietpi.com/forum/t/using-uart/2276/8 "2019-07-17T19:02:40Z")

</div>

Hi,

I am running DietPi version 6.25.3 and have been running into the same problem. I have done the following:

dietpi-config-\>Advanced Options-\>Serial/UART-\>ttyS0- =\> ON  
save and reboot  
dietpi-config-\>Advanced Options-\>Serial/UART-\>ttyS0- console =\> OFF  
save and reboot

I tried this loopback test connecting GPIO 14 and 15 together:

> #!/usr/bin/env python  
> import time  
> import serial
> 
> ser = serial.Serial(  
> port=’ /dev/ttyS0’, #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0  
> baudrate = 9600,  
> parity=serial.PARITY\_NONE,  
> stopbits=serial.STOPBITS\_ONE,  
> bytesize=serial.EIGHTBITS,  
> timeout=1  
> )  
> counter=0
> 
> while 1:  
> ser.write(“Write counter: %d \n”%(counter))  
> time.sleep(1)  
> counter += 1
> 
> while 2:  
> x=ser.readline()  
> print x

I get no errors when I run this script but unfortunately nothing happens. With ttyS0 there are some limitations so I also set the serial parameters without baudrate, parity, stopbits, and bytesize yet the result was stll the same. I tried using serial0 instead of ttyS0 and then I got a {} error, that is, it couldn’t find the serial port. This is strange since ttyS0 and serial0 are linked just as serial1 is linked with ttyAMA0 (checked with ls -l /dev). So it seems I am still missing something in this configuration. Any ideas what else needs to be done?

---

<div class="post-metadata">

**Author:** ![jhorv](https://dietpi.com/forum/user_avatar/dietpi.com/jhorv/32/4673_2.png) [@jhorv](https://dietpi.com/forum/u/jhorv)\
**Post date:** [18 July 2019 06:16 UTC](https://dietpi.com/forum/t/using-uart/2276/9 "2019-07-18T06:16:05Z")

</div>

A quick update:

I tried using AMA0/serial1 with the console setting in dietp-config set to off. Bluetooth is also off. The output of the loopback script is the same:

> Traceback (most recent call last):  
> File “/usr/local/lib/python3.5/dist-packages/serial/serialposix.py”, line 265, in open  
> self.fd = os.open(self.portstr, os.O\_RDWR | os.O\_NOCTTY | os.O\_NONBLOCK)  
> FileNotFoundError: [Errno 2] No such file or directory: ’ /dev/serial1’
> 
> During handling of the above exception, another exception occurred:
> 
> Traceback (most recent call last):  
> File “./loopback.py”, line 11, in   
> timeout=1  
> File “/usr/local/lib/python3.5/dist-packages/serial/serialutil.py”, line 240, in **init**  
> self.open()  
> File “/usr/local/lib/python3.5/dist-packages/serial/serialposix.py”, line 268, in open  
> raise SerialException(msg.errno, “could not open port {}: {}”.format(self.\_port, msg))  
> serial.serialutil.SerialException: [Errno 2] could not open port /dev/serial1: [Errno 2] No such file or directory: ’ /dev/serial1’

Meanwhile, ls -l /dev shows that it is there.

Finally, dmesg gives the following:

> [1.235831] uart-pl011 20201000.serial: cts\_event\_workaround enabled  
> [1.245609] 20201000.serial: ttyAMA0 at MMIO 0x20201000 (irq = 81, base\_baud = 0) is a PL011 rev2

Any ideas?

---

<div class="post-metadata">

**Author:** ![jhorv](https://dietpi.com/forum/user_avatar/dietpi.com/jhorv/32/4673_2.png) [@jhorv](https://dietpi.com/forum/u/jhorv)\
**Post date:** [1 August 2019 20:00 UTC](https://dietpi.com/forum/t/using-uart/2276/10 "2019-08-01T20:00:12Z")

</div>

I think I found a solution, well kind of 🙄. I was able to finally get ttyS0 working but still was unable to do anything with ttyAMA0. 😢 I guess it has to do with interference from Bluetooth even though I have Bluetooth switched off.

Nevertheless, the problem with ttys0 appears to be with permissions. When I do this:

```nohighlight
chmod 666 /dev/ttyS0

```

I am then able to access ttyS0 no problem and my loopback script works. Also I was able to connect my ESP8266 module and send/receive data via Node-Red. Actually, it was through Node-Red that I discovered the solution because it clearly gave me a “permission denied” error. Python simply said that it “could not open port” and “/dev/ttyS0 [Errno 2] No such file or directory” which is quite vague and misleading.

Anyway, my problem now is that the permission settings for the UART are not permanent, that is, it has to be reset if there is a reboot or shutdown. Anyone know how to set the permission for the UART at boot time so it will always be available?

---

<div class="post-metadata">

**Author:** ![MichaIng](https://dietpi.com/forum/user_avatar/dietpi.com/michaing/32/7_2.png) [@MichaIng](https://dietpi.com/forum/u/MichaIng)\
**Post date:** [23 October 2019 17:16 UTC](https://dietpi.com/forum/t/using-uart/2276/11 "2019-10-23T17:16:10Z")

</div>

jhorv  
Sorry for missing reply.

Instead of making the tty world-writeable, please add the nodered user to the tty group: usermod -aG tty nodered

Instead of only disabling Bluetooth, might might be required to switch Bluetooth serial device usage by adding dtoverlay=miniuart-bt to /DietPi/config.txt. AFAIK otherwise ttyAMA0 does not listen to the GPIO ports.  
**EDIT: Ah nope, disable-bt should be sufficient to allow ttyAMA0 listening to GPIO ports.**

---

<div class="post-metadata">

**Author:** ![jhorv](https://dietpi.com/forum/user_avatar/dietpi.com/jhorv/32/4673_2.png) [@jhorv](https://dietpi.com/forum/u/jhorv)\
**Post date:** [10 November 2019 09:53 UTC](https://dietpi.com/forum/t/using-uart/2276/12 "2019-11-10T09:53:35Z")

</div>

Many thanks for the hint, better late than never. 😃

I had been thinking of adding chmod a+rw /dev/ttyS0 to the /etc/rc.local file but that’s absolutely not secure and your suggestion is obviously the best way to do it.

Meanwhile, after I updated DietPi from v6.25.3 to v6.26.3 I have been able to now access ttyAMA0 through node-red and python. Bluetooth is still disabled but since I don’t use it (yet) that’s not much of a concern for the time being.

Thanks again!
