Table of Contents
Raspberry Pi UART:
UART (Universal Asynchronous Receiver / Transmitter) is used, among other things, for communication with RS232 or RS485 interfaces. Often the UART is used for data transmission use with microcontrollers or the PC. The Raspberry Pi performs the UART (Universal Asynchronous Receiver / Transmitter) interface at pins 8 and 10 (TxD and RxD) from the GPIO connector strip out.
In this article I will also explain how to use the UART (Universal Asynchronous Receiver / Transmitter) interface based on the communication between two Raspberry Pi boards. So it is possible to communicate between the two mini computers.
Amazon Purchase Links:
Other Tools and Components:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Activate and clean UART (Universal Asynchronous Receiver / Transmitter):
The UART interface must first be activated after a standard installation of Raspbian and be freed from unnecessary ballast.
Property that the Raspberry Pi booting by default via the UART (Universal Asynchronous Receiver / Transmitter) interface issues. This means that when booting up, a lot of information which are usually not required. Follow these steps for a major cleaning:
- Open the file /boot/cmdline.txt with an editor, e.g. with Nano:
nano / boot / cmdline. txt
- There should be a line there from which you delete the following part:
console = ttyAMA0, 115200 kgdboc = ttyAMA0, 115200
The new line now looks e.g. as follows (all information is in one line!):
dwc_otg. lpm_enable = 0 console = tty1 root = / dev / mmcblk0p6
rootfstype = ext4 elevator = deadline rootwait
- Depending on the configuration, there are further settings behind the line. To let
You this untouched!
Now open the / etc / inittab file in an editor. Check for the last Line of the file is commented out with a # symbol:
T0: 23: respawn: / sbin / getty -L ttyAMA0 115200 vt100
If this is not the case, put a # sign in front of this line. Save and then close the file. If the line has already been commented out, must Don’t make any further changes. The UART interface can now used and no longer outputs data during the boot process.
Connect two Raspberry Pis via UART (Universal Asynchronous Receiver / Transmitter):
The next step is to make a physical connection between the two boards (see Figure 1). Simply connect the TxD pin of the device 1 with the RxD pin of device 2. Connect the RxD pin of device 1 in turn with the TxD pin of device 2. Please also remember the two ground potentials to connect with each other. So create a connection between the GND pins of the devices, e.g. B. Pin 6 of device 1 to pin 6 of device 2. This is often forgotten, and in the subsequent communication, confused characters appear.
Also, keep this in mind when communicating with other microcontrollers or PCs want.
UART (Universal Asynchronous Receiver / Transmitter) in Python:
So that you can integrate UART communication into your Python programs, Let’s now turn to the integration of UART (Universal Asynchronous Receiver / Transmitter) in Python. We continue to use as an example the two connected Raspberry Pi boards. Helps with programming us the python library pyserial. To install this library you will need They in turn use the python setuptools:
sudo apt – get install python – setuptools
sudo apt – get install python3 -pip
Now install pyserial using pip:
sudo pip -3.2 install pyserial
The library can now be loaded into any Python program using import serial will. To communicate between the two Raspberry Pi, create two Python programs:
- py is created on the sending Raspberry Pi and is used for sending of commands.
- py is the listener and receives the data from the other device.
Fill in the sender.py file with the following lines of code:
#! / usr / bin / python3 import serial ser = serial. Serial ("/ dev / ttyAMA0") ser. baud rate = 9600 print ("Send UART data ...") ser. write (b "Hello world! \ n") ser. close ()
This simple program opens the UART (Universal Asynchronous Receiver / Transmitter) interface and sets a baud rate of 9600. The ser.write () function now sends the text Hello World, followed by a newline string. This string shows the second Raspberry Pi, that the end of the message has been reached. ser.close () gives way to the UART interface free to send again. listener.py on the other device has the following content:
#! / usr / bin / python3 import serial ser = serial. Serial ("/ dev / ttyAMA0", timeout = 10) ser. baud rate = 9600 data = ser. readline () print (data. decode ('utf8')) ser. close ()
Here, too, the UART (Universal Asynchronous Receiver / Transmitter) port is selected and set to the same baud rate of 9600. You also define a timeout of 10 seconds. This is necessary because the read function readline () would continue listening endlessly without this timeout. During this time the UART (Universal Asynchronous Receiver / Transmitter) port would be blocked. After the timeout has expired, the UART (Universal Asynchronous Receiver / Transmitter) released again and the program continues from line 9. Now start listener.py on device 1. Within 10 seconds you also need to transmit. py on the second device.
Important pySerial functions:
pySerial offers quite a few more functions that are not available in the previous Programs were used. We want some important functions Explain to you:
- Serial (port, rate, timeout) determines the UART port (/ dev / ttyAMA0), the desired baud rate and the timeout.
- write (“Text”) sends the transferred string to the UART interface.
- read (n) reads out the UART port. If no value is given, only one byte is read out; otherwise n determines the number of bytes to be read. To meet If you do not enter a sufficient number of bytes within the timeout period, the function ends prematurely.
- readline (nmax) reads the UART port for as long as in our example program off until the EOL flag (end-of-line flag) has been transmitted in the form of \ n or until the timeout has been reached. nmax optionally limits the maximum message length. Since version 2.6 of pySerial, it is not possible to set your own EOL flag more so easily possible.
- readlines () reads several lines of text and separates them according to the EOL flag (\ n). This function only ends after a timeout.
The Audio bus I2S:
In addition to audio transmission through the jack socket or via HDMI, the Raspberry Pi also has the ability to send digital audio signals over I2S. The necessary You can find pins for this on the J8 header:
- Pin 12 PCM_CLK
- Pin 35 PCM_FS
- Pin 38 PCM_DIN
- Pin 40 PCM_DOUT
I2S stands for Integrated Interchip Sound and is used to transmit audio data between ICs. You could still find these four pins in the first Raspberry Pi models on the P5 header. Since the B + model, the pins are in the 40-pin GPIO strip J8 has been integrated and is now much more convenient to reach. To convert the audio signals present there into real sound, you need special D / A converter. A dedicated HiFi community has developed here, the ensures crystal-clear audio quality with its diverse audio boards. Via the jack connection this sound quality could not be achieved. To the results of the HiFi community include the following components and projects:
- Crazy Audio HiFiBerry
- Wolfson Audio Card
- Pi DAC
To activate the I2C interface, add it to the device tree. to open To do this, open the /boot/config.txt file and insert the line dtparam = i2s = on there. Some audio boards, such as E.g. the popular HiFiBerry, require additional device tree Overlays. For the HiFiBerry DAC this is e.g. the line dtoverlay = hifiberry-dac, the must be added to the /boot/config.txt file.
For audio-loving users, there was even a dedicated operating system called Volume developed. This supports I2S without any further measures.
The 1-wire bus is a single-wire bus originally developed by the Dallas company has been. The name of the bus comes from the fact that only one data line (DQ) is required, to send and receive data. The data line also supplies the sensor its supply voltage. Here it is necessary to connect the data line via a small pull-up resistor (4.7 k) to be connected to the 3.3 V line. This resistance keeps the data line stable at the 3.3 V level and can be used by the 1-Wire sensor during communication be pulled to ground. A real one-wire connection is in spite of that Name not possible. The ground connection must also always be to the 1-wire Sensor are guided. The 1-Wire bus can theoretically have any number of components to process. So you can easily connect several temperature sensors in parallel, to read values from each individual sensor.
In order to make the 1-Wire-Bus accessible, you need a line here as well in /boot/config.txt to provide the device tree with the necessary information. Open the file with nano /boot/config.txt and add the following line to the End of file:
dtoverlay = w1-gpio, gpiopin = X
For the gpiopin parameter, use the BCM designation of the pin that you are using Want to use your 1-Wire component. It may be that the sensor you are using requires an external pull-up enable pin. If this is the case, insert the following line instead: dtoverlay = w1-gpio-pullup, gpiopin = X, extpullup = Y The value for the parameter gpiopin must now be used again for the GPIO pin used be adjusted. The Y is the placeholder for the BCM designation of the pull-up Enable pins you want to use. After a GPIO pin the pull-up Enable function has been assigned, it can usually not be used for other purposes be used. The last specialty are 1-wire components that go directly over the data line be supplied with electricity. This possibility can also be mapped in the device tree
will. In this case, add a comma and the pullup = on parameter at the end Your line. For the complete commissioning of a 1-wire temperature sensor including connection diagram and loading the modules.
There are also other ways to get access to the 1-Wire bus. So there I2C ICs, which place the 1-wire communication on the I2C bus, or USB adapters, which enable 1-wire communication via USB.