Communicating with Arduino using C/C++

Hello Everyone

This post is on “How to communicate with Arduino using C/C++”.

I basically have an array of integers from my C/C++ code which i need to send to an Arduino. I need to communicate between C/C++ and Arduino

Before we actually see how this is done, we should know a little about how are actually devices handled in Linux.
Under Linux and UNIX each and every hardware device is treated as a file. They are special files located in the /dev folder in your system which allow us to access the hardware directly.

Like any other normal file in our computer we can read/write to these device files(which means reading or writing to the device itself). That is pretty much how the OS also does it on a primitive level. It opens the file associated with the device it has to use and then reads or writes to it accordingly.

Arduino boards are also handled in the same way. We will read/write to the device file of our Arduino.

First, we need to identify the device file for our arduino.

  • Go to the tools menu and look for Serial Port submenu.
  • There it will show you the list of device files(Serial Ports). For Arduino Duemilanove it is /dev/ttyUSB0(file name can change if it is already in use), for Arduino UNO it will be /dev/ttyACM0. Select a file from this menu which will be used for communication.

Now, there are 2 parts to this problem, the C code and the Arduino code.

  • C code will send data
  • Arduino code will receive data.

C code

int main()
{
    int data[] = {10,5,13};  //Random data we want to send
    FILE *file;
    file = fopen("/dev/ttyUSB0","w");  //Opening device file
    int i = 0;
    for(i = 0 ; i < 3 ; i++)
    {
        fprintf(file,"%d",data[i]); //Writing to the file
        fprintf(file,"%c",','); //To separate digits
        sleep(1);
    }
    fclose(file);
}

Explanation.

  • We have an array of integers and we want to send this array to our arduino. First we open our arduino device file which is /dev/ttyUSB0.
    int data[] = {10,5,13};  //Random data we want to send
    FILE *file;
    file = fopen("/dev/ttyUSB0","w");  //Opening device file
  • Next we write each element of the array to the file using a simple for loop, after writing each element we add a comma(‘,‘) to separate elements in our file.
    int i = 0;
    for(i = 0 ; i < 3 ; i++)
    {
        fprintf(file,"%d",data[i]); //Writing to the file
        fprintf(file,"%c",','); //To separate digits
        sleep(1);
    }
    fclose(file);

Next thing is to read this data through an arduino.

Arduino Code

void setup()
{
    Serial.begin(9600);
}

int calc()
{
    int num=0,x=0;

    for(x;x<=j;x++)
          num=num+(buff[x]-48)*pow(10,j-x);

    return num;
}

int input,num;
int buff[5];
int j=-1;

void loop()
{
    if(Serial.available()>0)
    {
        input=Serial.read();

        if(input==',')
        {
            num=calc();
            j=-1;
            Serial.println(num);
        }
        else
        {
            j++;
            buff[j]=input;
        }
    }
}

Explanation

  • First we setup the Serial port in void setup()(line 3).
  • After setting it up, we have to start reading, now in our C code we wrote integer values to our file
    fprintf(file,"%d",data[i]);
    

    But our arduino will read them as characters, so we need to read characters and convert them back to our original number.

  • So in void loop() we keep on reading data(which is nothing but our integers converted to characters) and store them in an array(buff) until we read a comma (‘,‘) . comma was used to separate digits(line 10 of our C code).
    input=Serial.read();
    
    if(input==',')
    {
        num=calc();
        j=-1;
        Serial.println(num);
    }
    else
    {
        j++;
        buff[j]=input;
    }
    

    As soon as we read a comma we call calc() where we convert the read characters back to the integer.

  • In calc() we subtract 48 from the ascii value of the character, multiply it with its place value raised to 10 and return the final integer num.
    for(x;x<=j;x++)
        num=num+(buff[x]-48)*pow(10,j-x);
    
  • Repeat the process until all the data has been read.

Finally, we were able to send an array from C/C++ to our arduino.

This was a very basic way of communicating, many better techniques can also be used.