Rpi to Arduino Serial UART Communication Tutorial
Ask QuestionAsked 1 year, 4 months agoActive 24 days agoViewed 2k times21
I’m trying to connect my Arduino to my Raspberry Pi using the GPIO pins. I understand that the Pi uses a 3.3V signal while the Arduino uses a 5V signal so I bought a bi-directional logic level converter to convert them. Here are some shots of my current setup.

I’ve been using this article as my guide: Raspberry Pi Serial (UART) Tutorial. As you can see from my pictures, I have it wired in the same way that they describe.
On the Pi, I’m trying to run the following simple program:
#!/usr/bin/env python
import time
from serial import (
Serial,
PARITY_NONE,
STOPBITS_ONE,
EIGHTBITS,
)
ser = Serial(
port="/dev/serial0",
baudrate=9600,
parity=PARITY_NONE,
stopbits=STOPBITS_ONE,
bytesize=EIGHTBITS,
timeout=1,
)
while 1:
ser.write("Hello\n")
time.sleep(1)
Over on the Arduino side of things, per the guide, I flashed it with the SerialEvent example code that comes as a part of Arduino Studio. With all those components in place, I open up the Serial Monitor in Arduino studio (again, per the guide referenced above), and start the Python program on the Pi.
Then… nothing happens. I’m stumped. Does my wiring look right? Any common pitfalls I might be falling prey to? Thanks in advance.pythonserialarduinouartwiringshareedit follow closeflagedited Aug 6 at 5:06tlfong013,28733 gold badges77 silver badges2121 bronze badgesasked Apr 6 ’19 at 0:59soapergem13111 gold badge11 silver badge88 bronze badges
- 1There are too many unknowns to answer. Break your problem down int simple steps. I suggest you start with a loopback on the Pi, then extend to converter. – Milliways Apr 6 ’19 at 1:11
- 6it looks like you forgot to solder the headers on the logic level converter – jsotola Apr 6 ’19 at 1:50
- 4@David I’ve lost count of the number of people who have promised they properly followed a schematic only to discover the wiring was actually wrong. I prefer clear photos over claimed circuit diagrams. – joan Apr 6 ’19 at 7:08
- 2@David I think a set of schematics (what it’s supposed to be) and some photos (what it actually is) would be the ideal for debugging someone else’s project… – Roger Jones Nov 1 ’19 at 8:55
- 1@RogerJones, you’re right. It makes it easier to know if the actual wiring matches the schematic. – David Nov 1 ’19 at 18:45
add a comment | show 4 more commentsstart a bounty
2 Answers
Then… nothing happens. I’m stumped.
Well, I would be stumped if something did happen.
Let me explain.
First, to write a program to let Rpi and Arduino to talk to each other is a complicated thing for newbies.
My advice is “Eat the Elephant One Bite at a Time!”.
Now let me list the first couple of bites.
- Rpi 3V3 Tx repeatedly writes bytes.
- Rpi 3v3 Tx writes bytes to Tx, pause a bit, Rx reads the written bytes back (loop back test).
- Convert Tx, RX signals from 3V3 to 5V, then repeat the above two steps.
- / to continue, …
I have written the following little test program to do the first two steps. You may like to try it, comment, or ask questions, before I move on to the third step.
# uart_test06 tlfong01 2019apr08hkt1603 ***
# Computer = Rpi3B+
# Linux = $ hostnamectl = raspberrypi Raspbian GNU/Linux 9 (stretch) Linux 4.14.34-v7+ arm
# Python = >>> sys.version = 3.5.3 Jan 19 2017
# Test 1 - repeatWriteBytes() - UART port repeatedly send out bytes.
# Function - Repeat many times sending bytes, pause after each bytes.
# Test 2 - loopBackTest() - UART port send and receive bytes.
# Function - Send one bytes to TX, wait some time (Note 1), then read bytes back from RX.
# Setup - Connet Tx pin to Rx pin to form a loop.
# Note 1
# Bolutek BlueTooth BC04 needs at least 10mS to respond
from time import sleep
import serial
serialPort0 = serial.Serial(port = '/dev/serial0',
baudrate = 9600,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout= 1)
def setSerialPortBaudRate(serialPort, baudrate):
serialPort.baudrate = baudrate
return
def serialPortWriteBytes(serialPort, writeBytes):
serialPort.write(writeBytes)
return
def serialPortReadBytes(serialPort, maxBytesLength):
readBytes = serialPort.read(maxBytesLength)
return readBytes
def serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitTime):
serialPort.flushInput()
serialPort.flushOutput()
serialPort.write(writeBytes)
sleep(waitTime)
readBytes = serialPortReadBytes(serialPort, maxBytesLength)
print(' bytes written = ', writeBytes)
print(' bytes read = ', readBytes)
return readBytes
def repeatWriteBytes(serialPort, writeBytes, pauseTimeBetweenBytes, repeatCount):
print(' Begin repeatWriteOneByte(), ...')
for i in range(repeatCount):
serialPortWriteBytes(serialPort, writeBytes)
sleep(pauseTimeBetweenBytes)
print(' End repeatWriteOneByte().')
return
def serialPortLoopBack(serialPort, writeBytes, maxBytesLength, waitTime):
print(' Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...')
serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitTime)
print(' End serialPortLoopBack(), ...')
return
setSerialPortBaudRate(serialPort0, 9600)
#repeatWriteBytes(serialPort0, b'AT\r\n', 0.01, 200000000)
serialPortLoopBack(serialPort0, b'AT\r\n', 32, 0.030)
''' Sample output tlfong01 2019apr0801
>>>
=== RESTART: /home/pi/Python_Programs/test1193/uart_test02_2019apr0801.py ===
Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...
bytes written = b'AT\r\n'
bytes read = b'AT\r\n'
End serialPortLoopBack(), ...
>>>
'''
# End
All newbies need to understand and explain the following. If Tx is connected to 1k and LED then Ground, multimeter would find Tx ~=3V if not sending bytes out, and ~=2V5 when sending something; LED grows dimmer, but not at all noticeable to my very dull human eyes.

Serial Blink LED Program as debug/testing tool for 3V3 UART Send Bytes
Now I have written a small program for checking if UART can send bytes. This program is useful to check if UART at 3V logic is working OK. Here is the youtube showing led blinking, followed by the blinkLed function.
Youtube video of uart blink LED
# uart_test12 tlfong01 2019apr09hkt1337 ***
# Test 3 - serialBlinkLed() - blink LED many times
# Function - Send b'(/0xff) many times, then '(/0x00) many times
# Setup - Insert (1k + LED) between Tx and Ground
def serialBlinkLED(serialPort, repeatCount):
print(' Begin serialBlinkLed(), ...')
for i in range(repeatCount):
repeatWriteBytes(serialPort0, b'\xff', 0, 200)
repeatWriteBytes(serialPort0, b'\x00', 0, 200)
print(' End serialBlinkLed().')
return
# *** Main ***
setSerialPortBaudRate(serialPort0, 9600)
serialBlinkLED(serialPort0, 100)
# End
I bought a bi-directional logic level converter to convert them.
Yes, I am also testing a 8 channel bidirectional module, based on TSX018E.

UartBlink blink both LEDs connected to 3V Tx and 5V Tx, verifying that the TSX0108E level shifter is working OK. Below is the youtube video (Left side is 3V3 Tx Led, right side 5V Tx Led.
UartBlink blinks both 3V3 Tx Led and 5V Tx Led
Note – OP’s logical level converter picture is not very clear. It appears that Hv pin is not connected.
SparkFun Bi-Directional Logic Level Converter US$2.95
Anyway, I am moving on to do more testing.
I have started reading the tutorial followed by the OP. This tutorial seems a bit technical, and the discussion of PL UART and mini UART is a bit confusing.
Raspberry Pi Serial (UART) Tutorial – Roland Pelayo 2018jul12
Raspberry Pi has two UARTs: PL011 UART and mini UART
PL011 UART has bigger buffer and is more reliable
miniUART has no flow control, baud rate reliant on VPU clock speed, is less reliable
For Rpi with BlueTooth (Rpi3 and RpiZero), PL011 is tied to Bluetooh.
Linux console UART accessible through /dev/serial0.
miniUART accessible through /dev/ttyS0
PL011 UART accessible through /dev/ttyAMA0
Rpi PC UART cable (using PL2303 chip) to connect to Windows PL2303
I found the tutorial OP is reading is not clear. It says Rpi UART can talk to any other UART compatible device, such as Windows using the PC UART cable, and also to Arudino. Because I don’t have Arduino on hand, perhaps I can try Rpi to talk to a Bluetooth serial chip. After terminal and bluetooth, I think I can stop and let OP do the last but not the least job, talking to Arduino. 🙂
Rpi UART to talk to PC Win10 RealTerm, then BlueTooth serial Module
So I am following OP’s tutorial to first let RPi to talk to a serial terminal. The tutorial recommend puTTY, but just now I googled and found SuperTerm seems better, because it has I2C staff which I would use later for other I2C projects. I downloaded SuperTerm and installed it without any problem. I appreciate the tutorial recommending the Prolific COM driver, because some months ago I tried other old drivers but found them not working.

The tutorial recommends the PL2303 driver for the USB/TTL cable. But I often hear that CH3340 is better, more stable. So I plugged in the CH340. So now I have two UARTs, PL2302 at COM32, CH340 at COM5 to do loop back etc.

Now I am testing the CH430 USB to TTL adapter. I think I will first try the loopback thing using RealTerminal.

Now I connected the CH430 adapter’s Tx to Rx, set the RealTerm configuration to 9600-8N1, CH430 Port9, and found monitor displaying echoed characters as I key at the keyboard.
Next step is to write a Rpi python echo program which input characters from PC Winodws CH430 adpater Tx, and eches back at the adapter’s Rx terminal.

Update 2020jan22hkt1448
Appendices
Appendix A – Rpi4B buster Multiple UART alternate function pinout

Appendix B – Rpi4B Multiple UART loopback program
Rpi4B Multiple UART loopback program V1.0
/ to continue, …shareeditdeleteflagedited Jan 22 at 6:55answered Apr 8 ’19 at 8:32tlfong013,28733 gold badges77 silver badges2121 bronze badges
- This is incredible. I’ll be trying out these steps very soon and will let you know how it goes! – soapergem Apr 9 ’19 at 15:20
- Thank you for your nice words. I read the tutorial you recommended a second time and found it not that difficult. The expert there suggest to learn how Rpi can first talk to PC using an USB TTL UART cable and pUTTY, then show you an Arduino program to talk to Rpi. I have no Arduino on hand. I think perhaps instead of Arduino, I can just let Rpi to talk to another serial device, a serial Bluetooth module. If Rpi can talk to serial Bluetooh, of course it can talk to Arduino which can pretend to be a stupid Bluetooth :(. – tlfong01 Apr 10 ’19 at 2:04
Based on your picture, and in addition to what jsotola said about soldering the pins on the level converter, you have not connected the high voltage (HV) and low voltage (LV) pins to the appropriate power source. HV needs to be connected to the Arduino’s 5 volt supply and LV needs to be connected to the Raspberry Pi’s 3.3 volt supply. Without those connections the level converter will not work.shareedit follow flag answered Apr 7 ’19 at 21:29SoreHands11633 bronze badgesadd a comment
.END
Categories: Uncategorized