Look for in our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Serial communication between Raspberry and M-Duino/Ardbox


Introduction

In this post it will be shown how to communicate using the serial between a MDuino/Ardbox and a Raspberry Pi. It actually exists many ways on communicating between this two devices, but today we will focus on see how to do it using the Serial.


Requirements

Ethernet or 20 I/Os PLC:      Ethernet PLC     20 I/Os PLC    

Panel PC:                                Panel PC          

 Industrial Shields boards:   Industrial Shields Boards



Description

Serial communication is actually one of the easiest way to communicate. It only uses two wires to achieve that, Rx & Tx. One wire is to send(Tx) and the other is for receiving(Rx). To stablish a communication Rx of device 1 must be connected to Tx of device 2 and vice versa. This is done automatically if the wire of the Serial is the USB wire. 

Now it will be seen a really easy example that allows to communicate a Raspberry and a Arduino Board using Serial.



Example

To start with we will configure the Arduino board. The code of this is really simple and intuitive:

void setup()
{
   Serial.begin(9600); //Starting serial communication
}
 
void loop()
{
  if (Serial.available()) {
    byte dataread = Serial.read();
    if(dataread == 'H')
    {
      Serial.println("Hello"); // send the data
    }
  }
}

This sketch reads from the Serial and if it is read an "H" character it answers wih a "Hello".

 

Now we will see the most interesting part. This part is how to communicate using the Serial of the Raspberry. USB in summary is a Serial Communication, so if we connect the typical USB cable between them the connection is already stablished. Now we need a tool that can interact with it. Actually there are a lot of tools that does that, from codes made in Python to some softwares that are purposed for that. In this case we will see a software called Minicom.


By default the Raspberry has Minicom already installed, but if we don't, the first thing to do is to install it. For install it you need to write the following command line:

$ sudo apt-get install minicom


Once it is installed you just need to run it using one of the following commands dependind on the name of the Arduino Board detected by Linux. 

$ sudo minicom --device /dev/ttyUSB0
$ sudo minicom --device /dev/ttyACM0


The interface that shows is really easy and its behaviour is similar to a terminal.


With this software it is really easy to interact now with the Arduino board. You just need to type everything you want and type ENTER. This will send a message to the Arduino. If the Arduino sends us a message it will also be displayed on the same Terminal.