john77
Posts: 75
Joined: Tue Nov 26, 2019 11:04 am

How to use Bluetooth and UART both.

Sun Jan 12, 2020 7:06 am

To use UART I have to add

Code: Select all

dtoverlay=pi3-miniuart-bt
Then I open a device

Code: Select all

uart_fd = open("/dev/serial0", O_RDWR | O_NOCTTY | O_NDELAY);
and work with UART.
But in this case a Bluetooth is not available.
So how to configure RPi 4 to work with both modules?
Last edited by john77 on Sun Jan 12, 2020 1:22 pm, edited 1 time in total.

dgordon42
Posts: 802
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: Hoe to use Bluetooth and UART both.

Sun Jan 12, 2020 11:56 am

It's not necessary to use

Code: Select all

dtoverlay=pi3-miniuart-bt
to use the UART's on a Pi, just add

Code: Select all

enable_uart=1
to the '/boot/config.txt' file and reboot instead. The miniUART will now be available as a serial port on the GPIO pin, and it's clocking, etc., will be set up as necessary. The miniUART is sufficient for most serial port applications.

If you do need to use the full UART, then you will need to use "pi3-miniuart-bt", but you should follow the procedure in the '/boot/overlays/README' file. You will need to set values for 'core_freq' and 'core_freq_min' in '/boot/config.txt'. The values depend on your Pi model and may be found here.

Also, you should note that the DT Parameter 'pi3-miniuart-bt' has now been changed to just 'miniuart-bt' but the old style remains for backward compatibility.

The Serial Port will still appear at '/dev/serial0', and Bluetooth will still operate using either of the above procedures.

Hope this helps,
Dave.

john77
Posts: 75
Joined: Tue Nov 26, 2019 11:04 am

Re: Hoe to use Bluetooth and UART both.

Sun Jan 12, 2020 12:07 pm

dgordon42 wrote:
Sun Jan 12, 2020 11:56 am
It's not necessary to use

Code: Select all

dtoverlay=pi3-miniuart-bt
to use the UART's on a Pi, just add

Code: Select all

enable_uart=1
to the '/boot/config.txt' file and reboot instead. The miniUART will now be available as a serial port on the GPIO pin, and it's clocking, etc., will be set up as necessary. The miniUART is sufficient for most serial port applications.

If you do need to use the full UART, then you will need to use "pi3-miniuart-bt", but you should follow the procedure in the '/boot/overlays/README' file. You will need to set values for 'core_freq' and 'core_freq_min' in '/boot/config.txt'. The values depend on your Pi model and may be found here.

Also, you should note that the DT Parameter 'pi3-miniuart-bt' has now been changed to just 'miniuart-bt' but the old style remains for backward compatibility.

The Serial Port will still appear at '/dev/serial0', and Bluetooth will still operate using either of the above procedures.

Hope this helps,
Dave.
without the line - pi3-miniuart-bt - UART prints gibberish.

So if I understand it right I should set
pi3-miniuart-bt
core_freq=
core_freq_min=
And then UART and Bluetooth both available? And UART stays on GPIO 14-15?

But when I add pi3-miniuart-bt - the bluetooth icon vanished from the tool panel.

dgordon42
Posts: 802
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: How to use Bluetooth and UART both.

Sun Jan 12, 2020 6:22 pm

john77 wrote:without the line - pi3-miniuart-bt - UART prints gibberish.
Not sure why you are experiencing this.

So far any work I've done with the serial ports has been on an original Pi and a Pi 3B, so I have just tried this out on a Pi 4B4G running an up to date Raspbian Buster to see what happens.

To test, I use the following Python3 script:

Code: Select all

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# serial_port_loopback3.py
# Will also work on Python2.
# Serial port testing on a RaspberryPi 

from __future__ import print_function
import serial

test_string = "Testing 1 2 3 4".encode('utf-8')
#test_string = b"Testing 1 2 3 4" ### Will also work

port_list = ["/dev/serial0"]

for port in port_list:

    try:
        serialPort = serial.Serial(port, 115200, timeout = 2)
        serialPort.flushOutput()
        serialPort.flushInput() # Syntax may change in new version of python3-serial
        print("Opened port", port, "for testing:")
        bytes_sent = serialPort.write(test_string)
        print ("Sent", bytes_sent, "bytes")
        loopback = serialPort.read(bytes_sent)
        if loopback == test_string:
            print ("Received", len(loopback), "valid bytes, Serial port", port, "working \n")
        else:
            print ("Received incorrect data", loopback, "over Serial port", port, "loopback\n")
        serialPort.close()
    except:
        #except IOError:
        print ("Failed at", port, "\n")

# That's it!
Next, I added the following line to the bottom of the original '/boot/config.txt' file.

Code: Select all

enable_uart=1
I power down The Pi, and connect GPIO physical pins 8 and 10 together to form a serial port loopback.
After powering up again, I run the Python script above, and the port pass the test. In this configuration the Serial Port is using '/dev/ttyS0', and Bluetooth (and WiFi) appear unaffected. The Bluetooth icon is present on the top of the GUI, but I have no Bluetooth device to test it's function.

Next, I add the following 3 lines to the end of '/boot/config.txt':

Code: Select all

core_freq=500
core_freq_min=500
dtoverlay=miniuart-bt
and reboot.

The Python loopback test script still works, although this time it's going through '/dev/ttyAMA0', and I still have my Bluetooth icon on my GUI. Bluetooth is now using '/dev/ttyS0'.

Try the loopback test and see how you get on.

Sometimes if you are seeing gibberish on a serial port, it helps to reduce the baud to 9600 for troubleshooting.

I have noticed that the Pi runs 5 to 7 degrees hotter at idle with the increased 'core_freq_min' value, one reason not to change the Bluetooth configuration if at all possible.

Hope this helps,
Dave.

john77
Posts: 75
Joined: Tue Nov 26, 2019 11:04 am

Re: How to use Bluetooth and UART both.

Mon Jan 13, 2020 10:06 am

Thank you Dave for detailed explanation. It works good.

User avatar
rin67630
Posts: 1158
Joined: Fri Mar 04, 2016 10:15 am

Re: How to use Bluetooth and UART both.

Mon Jan 13, 2020 10:31 am

IMHO the RPi4 has many UARTS so you can leave /dev/ttyAMAO to Bluetooth and just add another full UART as e.g. /dev/ttyAMA1.
That will be better than using the very limited mini UART that was the only solution possible on a RPi3.

john77
Posts: 75
Joined: Tue Nov 26, 2019 11:04 am

Re: How to use Bluetooth and UART both.

Mon Jan 13, 2020 10:34 am

rin67630 wrote:
Mon Jan 13, 2020 10:31 am
IMHO the RPi4 has many UARTS so you can leave /dev/ttyAMAO to Bluetooth and just add another full UART as e.g. /dev/ttyAMA1.
That will be better than using the very limited mini UART that was the only solution possible on a RPi3.
Thank you. I'll consider this solution. As we are limited on GPIOs I have to spare every pin for other functionalities.

User avatar
rin67630
Posts: 1158
Joined: Fri Mar 04, 2016 10:15 am

Re: How to use Bluetooth and UART both.

Mon Jan 13, 2020 1:08 pm

john77 wrote:
Mon Jan 13, 2020 10:34 am
rin67630 wrote:
Mon Jan 13, 2020 10:31 am
IMHO the RPi4 has many UARTS so you can leave /dev/ttyAMAO to Bluetooth and just add another full UART as e.g. /dev/ttyAMA1.
That will be better than using the very limited mini UART that was the only solution possible on a RPi3.
Thank you. I'll consider this solution. As we are limited on GPIOs I have to spare every pin for other functionalities.
Pls have a look here:
https://www.raspberrypi.org/forums/view ... 1#p1590882
Adding "enable_uart=1" to config.txt on a Pi4 enables ttyS0 (UART1) on GPIOs 14 & 15 (Alt5), leaving UART0 driving the Bluetooth interface on 30-33 (Alt3).
Apparently it looks like, if you give Bluetooth the UART0, then the mini uart (barely usable) must be on TX-RX @GPIO 14-15.
The new UARTs 2-5 can only be mapped to other pins in ALT-4 mode. If you are short on pins and are not happy with the mini uart, then the solution could have been to disable the mini-UART and use GPIO 14-15 for another purpose.
If you don't need the HAT EEPROM feature, you might use UART2 (/dev/ttyAMA1) at GPIO 0 and 1 in ALT4 mode.

john77
Posts: 75
Joined: Tue Nov 26, 2019 11:04 am

Re: How to use Bluetooth and UART both.

Tue Jan 14, 2020 11:22 am

rin67630 wrote:
Mon Jan 13, 2020 1:08 pm
Apparently it looks like, if you give Bluetooth the UART0, then the mini uart (barely usable) must be on TX-RX @GPIO 14-15.
The new UARTs 2-5 can only be mapped to other pins in ALT-4 mode. If you are short on pins and are not happy with the mini uart, then the solution could have been to disable the mini-UART and use GPIO 14-15 for another purpose.
If you don't need the HAT EEPROM feature, you might use UART2 (/dev/ttyAMA1) at GPIO 0 and 1 in ALT4 mode.
So right now, with dtoverlay=miniuart-bt I use a mini-uart? Actually it works quite well with a baud rate 115200. Another option - to use GPIO 0-1 and free GPIO 14-15 for other purpose?

dgordon42
Posts: 802
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: How to use Bluetooth and UART both.

Tue Jan 14, 2020 1:32 pm

john77 wrote:So right now, with dtoverlay=miniuart-bt I use a mini-uart?
Incorrect, with "dtoverlay=miniuart-bt", Bluetooth uses the miniUART 'ttyS0' internally and you use the full UART 'ttyAMA0' as the Serial Port on GPIO pins 14 and 15 (physical pins 8 and 10). The clue is in the name.

See the details in my posts above and the file '/boot/overlays/README' on your Pi.

The beauty of calling the Serial Port '/dev/serial0' in code is that all Pi models will automatically connect GPIO pins 14 & 15 to the correct UART regardless of the Pi's configuration. If you choose to use UART1 or above, your code will nor run on any Pi model released prior to the Model 4B, and as far as I know the 'serial0' alias won't work either.

Dave.

john77
Posts: 75
Joined: Tue Nov 26, 2019 11:04 am

Re: How to use Bluetooth and UART both.

Tue Jan 14, 2020 1:46 pm

dgordon42 wrote:
Tue Jan 14, 2020 1:32 pm
john77 wrote:So right now, with dtoverlay=miniuart-bt I use a mini-uart?
Incorrect, with "dtoverlay=miniuart-bt", Bluetooth uses the miniUART 'ttyS0' internally and you use the full UART 'ttyAMA0' as the Serial Port on GPIO pins 14 and 15 (physical pins 8 and 10). The clue is in the name.

Dave.
OK. But what's the problem to use miniUART with Blutooth?
So far I open a device and discover remote devices.

Code: Select all

int BT_Open(void)
{
    bt_dev_id = hci_get_route(nullptr);
    bt_sock = hci_open_dev(bt_dev_id);

    if (bt_dev_id < 0 || bt_sock < 0)
    {
        printf ("Bluetooth initialization failed fail\n");
        return -1;
    }
    else
       printf ("Bluetooth device id = %d, socket = %d\n", bt_dev_id, bt_sock);

    return 0;
}

void BT_Scan(void)
{
    int flags = IREQ_CACHE_FLUSH;
    int max_resp = 2;
    int len=8, i;
    char addr[19] = { 0 };
    char name[248] = { 0 };

    inquiry_info *inq_info = (inquiry_info *) malloc (max_resp * sizeof(inquiry_info));

    int num_rsp = hci_inquiry(bt_dev_id, len, max_resp, nullptr, &inq_info, flags);
    if (num_rsp < 0)
    {
        printf ("Inquiry error\n");
        free(inq_info);
        return;
    }

    for (i=0; i<num_rsp; i++)
    {
        ba2str(&(inq_info+i)->bdaddr, addr);
        memset(name, 0 , sizeof(name));
        if (hci_read_remote_name(bt_sock, &(inq_info+i)->bdaddr, sizeof(name), name, 0) < 0)
            strcpy (name, "[unknown]");
        printf ("%s  %s\n", addr, name);
    }

    free(inq_info);
}
And it works good. I haven't tried to send actual data. Could be a problem?

oldblackcrow
Posts: 2
Joined: Tue Sep 29, 2020 1:07 pm

Re: How to use Bluetooth and UART both.

Tue Oct 06, 2020 8:16 pm

This all seems fine and dandy, but if I'm using Python or CircuitPython, could I just use a 'uartread'?

I want to display gas sensor data sent through Bluetooth. I'm using a 'uartwrite' in the Python code to send that data to an android app. Can't I, then, use a similar Python script in the Pi Zero to read the data using the 'uartread'?

Return to “General discussion”