3

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.

Pi to Arduino

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.

tlfong01
  • 4,384
  • 3
  • 9
  • 23
soapergem
  • 141
  • 1
  • 2
  • 8
  • 1
    There 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 06 '19 at 01:11
  • On top of the previous suggestion, please avoid using pictures to show your circuit and instead use a circuit schematic to illustrate your connections. It makes it easier for people that try to answer your question, and makes for a nicer post in the future when people look at it for reference. – David Apr 06 '19 at 01:37
  • 6
    it looks like you forgot to solder the headers on the logic level converter – jsotola Apr 06 '19 at 01: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 06 '19 at 07:08
  • Have you connected a Pi ground? The black wire (normally ground) appears to be connected to GPIO4. – joan Apr 06 '19 at 07:12
  • @ jsotola yes, pins appear not soldered. This is strange. – tlfong01 Apr 09 '19 at 12:12
  • I was unaware that anything needed to be soldered the the logic-level converter. For a simple test like this I assumed simply resting the logic-level converters on the header pins connected to the breadboard would be sufficient. Furthermore I also confirmed by using a multimeter that current flows from one wire to the other. – soapergem Apr 09 '19 at 15:23
  • 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 01 '19 at 08:55
  • 1
    @RogerJones, you're right. It makes it easier to know if the actual wiring matches the schematic. – David Nov 01 '19 at 18:45

2 Answers2

2

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.

SoreHands
  • 151
  • 3
1

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.

  1. Rpi 3V3 Tx repeatedly writes bytes.
  2. Rpi 3v3 Tx writes bytes to Tx, pause a bit, Rx reads the written bytes back (loop back test).
  3. Convert Tx, RX signals from 3V3 to 5V, then repeat the above two steps.
  4. / 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.

Tx sending bytes

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.

enter image description here

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.

realterm

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.

usb/ttl cable

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

CH430 USB TTL Adapter

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.

real term loopback test

Update 2020jan22hkt1448

Appendices

Appendix A - Rpi4B buster Multiple UART alternate function pinout

Rpi4B Multiple UART pinouts

Appendix B - Rpi4B Multiple UART loopback program

Rpi4B Multiple UART loopback program V1.0

/ to continue, ...

tlfong01
  • 4,384
  • 3
  • 9
  • 23
  • This is incredible. I'll be trying out these steps very soon and will let you know how it goes! – soapergem Apr 09 '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 02:04