What hardware to choose for my project?

Good Morning, I'm new to Arduino and I'm attempting to communicate with RS232, RS485 and Canbus although I'm struggling with selecting the best hardware for the job.

  1. I've seen the UNO R4 has onboard canbus but it needs an external receiver, plus extra adaptors for rs485 and rs232.

  2. The MKR unit has shields for both canbus and RS485 so I'll just need an adapter for TLL to RS232.

  3. I'm wondering if any boards have all 3 communications types built in? or if there's a simpler/neater way of doing this without lots of different adapters.

  4. I'm assuming If I used multiple adapters, I would need multiple communication pinouts/channels for each communication type. I wouldn't be able to use one TTL pinout for all 3 adapters etc... If that makes sense.
    For example the GIGA R1 WIFI has 4 UART ports and 1 Canbus port which requires an external tranceiver. I could put RS232 and RS485 on 2 of the UART ports with adapters and my canbus on the canbus ports with a external transceiver. (I couldn't put both rs232 and rs485 on the same UART port even if I was to communication on the port individually).

Hardware wise, seems like it will be six of one half dozen of the other, chose whatever you like best. However you should also consider what libraries are available to do what you want. Many of the tried and true libraries for RS485, CAN and Serial were developed for the AVR processors and you will find many examples/tutorials for them.

consider the ESP32 which has three serial ports for RS232/RS485 and has a builtin Canbus interface - see Two-Wire Automotive Interface (TWAI)
just requires an external CAN transciver - see what-is-can-bus-how-to-use-can-interface-with-esp32-and-arduino
the Arduino IDE has support libraries for the ESP32

1 Like
  1. So I can use Arduino IDE on the ESP32?
  1. It has three serial ports but I'm guessing I'll need an adapter for both RS232 and RS485 as they run on different + inverted voltages, unlike TTL which utilizes 0-5v high and low signal.
  1. Also, what is the exact idea of libraries? I just wrote a quick serial communication program with 0 prior experience of programming which wasn't too hard. So I'm not so sure what the libraries are actually used for.

the Arduino IDE supports the ESP32

ESP32 and RS232 loopback test (pins 2 and 3 of 9pin D-type connector linked)

ESP32 and RS485

couple of ESP32s communicating over canbus

  1. using a MCP2515
  2. using the ESP32 two-wire interface to drive a cjmcu-1051 CAN transciver

connected to other devices over canbus

there are libraries for the two-wire interface and other facilities, e.g. WiFi, Bluetooth, ADC, DAC, timbers, RMT, etc

  1. So I will need some Communication adapters, no board has these all built-in I assume?
    Guessing it'll go like this.

ESP32 CANBUS PINS > CANBUS Tranceiver > CANBUS Device
ESP32 UART 1 PINS > TTL TO RS232 ADAPTER > RS232 Device
ESP UART 2 PINS > TTL TO RS485 ADAPTER > RS485 Device

  1. What is the MCP2515 used for? The documentation just suggests it needs a Canbus Transceiver.

  2. What are the libraries used for though? Why would I use a library? Note - I have no understanding of these, I'm a complete novice.

the MCP2515 board in the photo has a canbus transciver on the PCB - it is being driven by the MCP_CAN_lib library
the use of the ESP32 two-wire interface eliminates the MCP2515 and all that is required is a canbus transciver

you may need to explicitly include a library such as for the two-wire, WiFi, BLE, DAC, etc
however many support libraries are included automatically, e.g. to drive the GPIO, Serial IO, etc

apart from custom PCBs never seen a board with all builtin - maybe worth having a look at LILYGO boards
make sure you test each module in a separate program before attempting to implement the complete project

1 Like

Right makes sense, I just need a board with at least 2 UART ports and 1 can-bus port. Again.. assuming most boards don't have an in-built transceiver so I'll need a external transceiver for the can-bus? and obviously the RS communication converters too?

I've just seen that i can use the digital pins as a serial communication port.

Maybe I can grab the Arduino R4 Wifi which has 1 UART and 1 Can-bus then use 2 digital pins?

You can use software serial on digital pins, but it has many limitations for max baudrate, serial parameters and rx/tx simultaneously. Ideally you reserve one UART for uploading code and serial monitor.
Esp32 would be better choice.

yes

never use a Arduino R4 Wifi so cannot advise

Okay thanks for your help, much appreciated.

One more thing that I guess isn't related to the hardware is...

I've noticed some people use the likes of Serial.read, Serial.print etc...

Others use RS485.read, RS485.print etc... Is there a way to define this?

Guessing it does the same thing but it's easier to sort through and read.

Serial.print() prints to the IDE Serial monitor via the USB port
RS485.print() is printing to a hardware or software serial port defined as RS485
e.g. in the following Serial1 is a ESP32 hardware serial port

// ESP32  Serial1 test - for loopback test connect pins RXD1 and TXD1

#define RXD1 16 // can map Serial1 and Serial2 to many ESP32 GPIO pins
#define TXD1 17 // check pin usage https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

// for RS232 shield connect
// ESP32 RXD1 to TTL/RS232 Rx
// ESP32 TXD1 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on ESP32 5V on UNO ect
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial.printf("\n\nESP32 serial1  test RXD1 pin %d TXD1 pin %d\n", RXD1, TXD1);
  Serial.printf(" loopback test connect pin %d to pin %d\n", RXD1, TXD1);
  Serial.printf("RS232: ESP32 pin %d RXD1 to TTL/RS232 Rx and pin %d TXD1 to TTL/RS232 Tx\n", RXD1, TXD1);
  Serial.printf("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  // read from Serial1, send to Serial
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }
  // read from Serial, send to Serial1
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

couple of test runs serial monitor output

ESP32 connected to a Mega

SP32 serial1  test Rx pin 16 Tx pin 17
   for loopback test connect pin 16 to pin 17
loopback test 1
loopback test 2 abcdefghijk 000000000000000
hello from mega
test 2 from mega
test 3 from mega 1234567890

------------------------------------------------------------

Arduino Mega Serial1 test -  for loopback test connect pin 18 to pin 19
RS232: Mega pin 18 TXD to TTL/RS232 Tx and pin 19 RXD to TTL/RS232 Rx
RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx

loopback test1
loopback test 2 1234567890 llllllllllll
hello from ESP32
test 2 from esp32
test 3 from ESP32 12345678900987654321

Again makes perfect sense.

  1. How do I define a serial port to RS485/RS232 ETC...?
  1. Can I use this to print to an LCD?

Something like this? I'm still learning the code.

you connect the external RS232 or RS485 modules to specified pins - say RS232 to GPIO16/GPIO17 and RS485 to GPIO18/GPIO19
you program opens two hardware serial ports Serial1 and Serial2 vso

#define RXD1 16 // can map Serial1 and Serial2 to many ESP32 GPIO pins
#define TXD1 17
#define RXD2 18
#define TXD2 19

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);

bytes read/written from/to Serial1 communicates with the RS232 module
bytes read/written from/to Serial2 communicates with the RS485 module

depends on the LCD and the library supporting it
what LCD are you using?

avoid using screen images if possible - they are difficult to read and waste space
copy the TEXT and post using code tags </>

Not decided on hardware yet! But this is a quick example/reference of what I'm getting at.

int RX = 0; // RX Pinout
int TX = 1; // TX Pinout
int BUTTON = 7 // Command Button

Screen(2, 3, 4, 5, 6); //Screen Pinout


void setup() {
  pinMode(RX, INPUT);
  pinMode(TX, OUTPUT);
  pinMode(BUTTON, INPUT);

  Serial.begin(9600); //RS485 Baudrate
  Serial.println("H PV"); // Enters Toilet to Maintenance Mode
}

void loop() {
    Serial.read(RX); // Reads incoming traffic
    Screen.println(RX); //prints incoming traffic to display
    delay(500);
}
{
  if(digitalread(BUTTON) == HIGH);
  Serial.print("T 1");
}

some graphics libraries support println() statements
for example, running the graphicstest example from the Adafruit_ILI9341 library
using code such as

unsigned long testText() {
  tft.fillScreen(ILI9341_BLACK);
  unsigned long start = micros();
  tft.setCursor(0, 0);
  tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(1);
  tft.println("Hello World!");
  tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
  tft.println(1234.56);
  tft.setTextColor(ILI9341_RED);    tft.setTextSize(3);
  tft.println(0xDEADBEEF, HEX);
  tft.println();
tft.println();
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(5);
  tft.println("Groop");
.........

displays (using an ESP32S3)
image

Where did you find this code, and what processor is it for? I ask, because there are numerous deficiencies/redundancies.
Did this come from ChatGPT, or some other AI, perchance? If so, please always identify when using these sources for code, as it will generally require more effort to explain to you what's wrong with it.

I made the code as an example, i have no clue what I'm doing...