Serial communication 3 Arduino with Raspberry

Hello. I read that the difference between a serial communication (uart) and IC2 is that IC2 is only one way, that is, it only sends or receives and not both at the same time.

I am interested in doing serial communication with the RX TX pins, not USB. I have already done it with USB and it works.

This page shows an IC2 example: Raspberry Pi to Arduino I2C Communication - The Geek Pub
It shows how to identify each Arduino. That is, assign a name.

My question is: in case it is uart, how do I identify each Arduino?

I'm not an expert with this technology so I don't know if you have to assign a name in each Arduino as with IC2, or a name is assigned by default.

This page (Raspberry Pi UART Communication using Python and C | Raspberry Pi) says that I should run the following code to search for UART devices:

ls -l /dev

I want to do something like the following in Python (it works with 1 USB, I haven't tried it with RX and TX). Who helps me? I don't want to burn anything:

import serial
import time

print("Loading")
ser0 = serial.Serial("/dev/ttyUSB0", 115200, timeout=0)
ser1 = serial.Serial("/dev/ttyUSB1", 115200, timeout=0)
ser2 = serial.Serial("/dev/ttyUSB2", 115200, timeout=0)

time.sleep(1)

def read_data(name):
    time.sleep(0.2)
    data = str(name.readlines())
    print(data)

read_data(ser0)
time.sleep(1)
read_data(ser1)
time.sleep(1)
read_data(ser2)

For identification and addressing of one of multiple stations a special protocol is required. Such a protocol is implemented in I2C and can be implemented on top of UART in RS-485 as MODBUS demonstrates.

In no case you can have full duplex communication on a network with more than 2 nodes. An Arduino Mega has 4 UARTs and could communicate full duplex with 4 other stations. Dunno how many UARTs your RasPi has.

1 Like

UART not working like IC2? That is, if you connect several devices to the same PIN (RX, TX), you can use several buses.

I have the idea that this is how it works, so I ask how to identify each Arduino.

If you mean I2C, which is a bus, multi device system, then its most unlike Serial which is two devices only, TX to RX.

1 Like

No. UART uses RX and TX, I2C uses SCL and SDA.
UART is a point-to-point connection, I2C and SPI are multi-station bus systems.

1 Like

1- That is, with the two I2C pins (SDA, SCL) I can use many devices and with the two UART pins (RX, TX) I can only use one device, right?


2- Another question, in the case of I2C, how do I send and receive signals? I do it with serial from my computer, when I press a key my arduinos respond.

import serial
import time

print("Loading")
ser = serial.Serial("/dev/ttyUSB0", 115200, timeout=0)
time.sleep(1)

x = input("Enter data: ")

if x == "a":
    ser.write(b'1\n')
    data = ser.readlines()
    print(data)

As you can see, I am sending signals to the serial monitor and at the same time reading what it receives. Can I do that with I2C? How?

Note: I can use USB for serial communication since the USB ports are numbered, but I think it would be better to use pins directly (besides learning something new doesn't hurt).

Between two Arduinos its possible with one setup as master I2C device and the other as slave.

Should be possible for the Raspberry Pi to be master or slave, but perhaps ask in the Raspberry Pi forums ..............

1 Like

For some examples do a Google search on;

Arduino I2C master slave communications

1 Like
  1. Right
  2. Use the Wire library and its examples

Sorry, I can't see what your (Python?) code does.

Every I2C node can act as a master and initiate a conversation. For the rest of the time it behaves as a slave.

1 Like

Do you know how to do it with Python? That is, to read the console with python I use the code:

data = ser.readlines()
print(data)

To send data to the console Arduino yo Raspberry via Python I use the code:
ser.write(b'1\n')

But that's with Serial. According to this tutorial (https://www.thegeekpub.com/18263/raspberry-pi-to-arduino-i2c-communication/) with I2C you use:

I2Cbus.write_i2c_block_data(SlaveAddress, 0x00, BytesToSend)

But, it only shows how to send the data not how to receive it. I am reading the library documentation: smbus2 · PyPI

... says to use:
read_i2c_block_data

Has anyone here used it? Is it the same as serial or is it different? It is not clear to me if I should print the data in Arduino with serial or what I should do.

Everything has been explained before. Except for Python code and libraries which are unknown in this forum.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.