2018-08-22 22:05:28 +02:00
.. _uart:
UART Bus
========
2018-11-14 22:12:27 +01:00
.. seo ::
:description: Instructions for setting up a UART serial bus on ESPs
2018-11-19 18:32:16 +01:00
:image: uart.png
2018-11-14 22:12:27 +01:00
:keywords: UART, serial bus
2018-08-22 22:05:28 +02:00
UART is a common serial protocol for a lot of devices. For example, when uploading a binary to your ESP
you have probably used UART to access the chip. UART (or for Arduino often also called Serial) usually
consists of 2 pins:
- **TX** : This line is used to send data to the device at the other end.
- **RX** : This line is used to receive data from the device at the other end.
Please note that these the naming of these two pins depends on the chosen perspective and can be ambiguous. For example,
while the ESP might send (`` TX `` ) on pin A and receive (`` RX `` ) data on pin B, from the other devices
perspective these two pins are switched (i.e. *it* sends on pin B and receives on pin A). So you might
need to try with the two pins switched if it doesn't work immediately.
2019-02-16 23:25:23 +01:00
Additionally, each UART bus can operate at different speeds (baud rates), so ESPHome needs to know what speed to
2018-08-22 22:05:28 +02:00
receive/send data at using the `` baud_rate `` option. The most common baud rates are 9600 and 115200.
In some cases only **TX** or **RX** exists as the device at the other end only accepts data or sends data.
.. note ::
On the ESP32, this component uses the hardware UART units and is thus very accurate. On the ESP8266 however,
2019-02-16 23:25:23 +01:00
ESPHome has to use a software implementation as there are no other hardware UART units available other than the
2018-08-22 22:05:28 +02:00
ones used for logging. Therefore the UART data on the ESP8266 can have occasional data glitches especially with
higher baud rates..
2021-08-01 10:56:18 +02:00
.. note ::
2021-08-18 04:47:08 +02:00
From ESPHome 2021.8 the `` ESP8266SoftwareSerial `` UART `` write_byte `` function had the parity bit fixed to be correct
for the data being sent. This could cause unexpected issues if you are using the Software Serial and have devices that
2021-08-01 10:56:18 +02:00
explicity check the parity. Most likely you will need to flip the `` parity `` flag in YAML.
2018-11-19 18:32:16 +01:00
.. code-block :: yaml
2018-08-22 22:05:28 +02:00
# Example configuration entry
uart:
2021-08-01 10:56:18 +02:00
tx_pin: 1
rx_pin: 3
2018-08-22 22:05:28 +02:00
baud_rate: 9600
Configuration variables:
2018-08-24 22:44:01 +02:00
------------------------
2018-08-22 22:05:28 +02:00
- **baud_rate** (**Required** , int): The baud rate of the UART bus.
- **tx_pin** (*Optional* , :ref: `config-pin` ): The pin to send data to from the ESP's perspective.
- **rx_pin** (*Optional* , :ref: `config-pin` ): The pin to receive data on from the ESP's perspective.
2020-06-10 02:22:16 +02:00
- **rx_buffer_size** (*Optional* , int): The size of the buffer used for receiving UART messages. Increase if you use integration that needs to read big payloads from UART. Defaults to `` 256 `` .
2020-05-24 23:59:26 +02:00
- **data_bits** (*Optional* , int): The number of data bits used on the UART bus. Options: 5 to 8. Defaults to 8.
- **parity** (*Optional* ): The parity used on the UART bus. Options: `` NONE `` , `` EVEN `` , `` ODD `` . Defaults to `` NONE `` .
2019-11-02 19:10:08 +01:00
- **stop_bits** (*Optional* , int): The number of stop bits to send. Options: 1, 2. Defaults to 1.
2018-08-22 22:05:28 +02:00
- **id** (*Optional* , :ref: `config-id` ): Manually specify the ID for this UART hub if you need multiple UART hubs.
2021-03-06 14:25:25 +01:00
ESP32 options:
2021-07-28 23:56:11 +02:00
- **invert** (*Optional* , boolean): Invert the logic levels of the RX and TX pins. Options: `` true `` or `` false `` . Defaults to `` false `` .
2021-03-06 14:25:25 +01:00
2019-02-13 11:20:34 +01:00
.. _uart-hardware_uarts:
Hardware UARTs
--------------
2020-05-10 21:27:59 +02:00
Whenever possible, ESPHome will use the hardware UART unit on the processor for fast and accurate communication.
When the hardware UARTs are all occupied, ESPHome will fall back to a software implementation that may not
2019-02-13 11:20:34 +01:00
be accurate at higher baud rates.
`` UART0 `` is (by default) used by the :doc: `logger component </components/logger>` , using `` tx_pin: GPIO1 `` and
`` rx_pin: GPIO3 `` . If you configure a UART that overlaps with these pins, you can share the hardware with the
logger and leave others available. If you have configured the logger to use a different hardware UART, the pins
used for hardware sharing change accordingly.
The ESP32 has three UARTs. Any pair of GPIO pins can be used, as long as they support the proper output/input modes.
The ESP8266 has two UARTs; the second of which is TX-only. Only a limited set of pins can be used. `` UART0 `` may
use either `` tx_pin: GPIO1 `` and `` rx_pin: GPIO3 `` , or `` tx_pin: GPIO15 `` and `` rx_pin: GPIO13 `` . `` UART1 `` must
use `` tx_pin: GPIO2 `` . Any other combination of pins will result in use of a software UART.
2019-05-29 19:28:30 +02:00
.. _uart-write_action:
`` uart.write `` Action
---------------------
This :ref: `Action <config-action>` sends a defined UART signal to the given UART bus.
.. code-block :: yaml
on_...:
- uart.write: 'Hello World'
2019-06-26 21:49:43 +02:00
# For escape characters, you must use double quotes!
2020-12-21 01:55:44 +01:00
- uart.write: "Hello World\r\n"
2019-06-26 21:49:43 +02:00
2019-05-29 19:28:30 +02:00
# Raw data
- uart.write: [0x00, 0x20, 0x42]
# Templated, return type is std::vector<uint8_t>
- uart.write: !lambda
return {0x00, 0x20, 0x42};
2020-09-17 03:39:15 +02:00
# in case you need to specify the uart id
- uart.write:
id: my_second_uart
data: 'other data'
2018-08-22 22:05:28 +02:00
See Also
--------
2019-02-13 11:20:34 +01:00
- :doc: `/components/logger`
2019-05-12 22:44:59 +02:00
- :apiref: `uart/uart.h`
2019-02-07 13:54:45 +01:00
- :ghedit: `Edit`