Curious about smart parking systems engineering? Click here to learn more !

How to Connect your Raspberry Pi and Arduino Together

Posted by Momen 25/05/2017 0 Comment(s)

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Our Raspberry Pi training series:

1. Introduction to Raspberry Pi

2. Analog Inputs/Output In Raspberry Pi

3. Wireless Communication in Raspberry Pi

4. How to connect a new Raspberry Pi to your Laptop without a screen

5. Communication between Raspberry Pi and Arduino

- See more at: https://www.zeroohm.com/blog/introduction-to-raspberry-pi/#sthash.QoQQtBo9.dpuf

Welcome to Zeroohm Raspberry Pi Training series. In this series of Raspberry Pi Training, Zeroohm will focus its efforts on helping the community learn more about Raspberry Pi fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

Our Raspberry Pi training series:

1. Introduction to Raspberry Pi

2. Analog Inputs/Output In Raspberry Pi

3. Wireless Communication in Raspberry Pi

4. How to connect a new Raspberry Pi to your Laptop without a screen

5. Communication between Raspberry Pi and Arduino

 

Do You have a Raspberry Pi and Adruino ? And you want to do a special project were you make your Raspberry Pi and Arduino Communicate with each other and you have no idea what to do ? what about  ?

Will then Don't Worry, in This tutorial I'm going to show you the simplest way to have a data communication between a Raspberry Pi and any model of Arduino Boards.

Overview:

There are many ways to do a communication between Raspberry Pi and Arduino such as, using the GPIO I2C Serial pins or by using the UART pins, but the simplest and easiest way is using a USB cable , with this method you can use your Raspberry Pi to develop the main application and connect to the Internet, you can use your Arduino board device to interact with several sensors and with a simple USB cable you can power your Arduino board and exchange data with your Raspberry Pi .

First of all here’s the list of materials needed :

  • Raspberry Pi .
  • Arduino .
  • USB cable (depending on the Arduino board that you have).

 

After this tutorial, you will learn how to send data from Raspberry Pi to Arduino , and from Arduino to Pi. But Before you start if you don't have a screen to connect you Raspberry Pi then click here and follow steps .

Implementation:

Step 1: make sure you have installed pySerial on your Raspberry Pi by using the following command in the terminal:

sudo apt-get install python-serial

This Library will give you the ability to read from and write to the serial port with Python Programming language.

Step 2 : plug the USB to the Arduino and Raspberry Pi, then check the connection between Raspberry Pi and Arduino by typing the following command in Pi terminal:

ls /dev/tty*

The result should be :

 /dev/ttyACM0

That mean the connection is fine and the devices are ready to communicate with each other.

 

cennection between Raspberry Pi and Arduino

Step 3 : In the terminal type this command to download Arduino IDE to the Raspberry Pi :

Sudo apt-get install arduino

 install Arduino IDE to Raspberry Pi

This process can take a while.

Once the download finishs you will have everything to start communicate between Raspberry Pi and Arduino

 

  • Sending data from The Raspberry Pi to the Arduino.

Step 4 : open Arduino IDE,  and write the following code :

const int led = 13;

int usbRead = 0;

void setup() {

     Serial.begin(9600);

      pinMode(led, OUTPUT);

     digitalWrite(led, LOW);

}

void loop () {

    if(Serial.available() ) {

        usbRead = Serial.read(), "0"  ; // when you send an integer over USB from Raspberry Pi to Arduino it will be in ASCI form and here will converted back to the integer

       Serial.println(usbRead);

       if(usbRead == 1 ) {

           blink();

           usbRead = 0;

       }

    }

}

void blink() {

     for(int x = 0; x <3 ; x ++ ) {

         digitalWrite(led, HIGH ) ;

         delay(1000);

         digitalWrite(led, LOW);

         delay(1000);

    }

}

Step 5 : open python 2 (IDLE) by going to the Applications menu >  programming > python 2 ( IDLE ), and write the following code:

#!/usr/bin/env python

import serial

usbCom = serial.Serial('/dev/ttyACM0',9600)

usbCom.open()

usbCom.write( ' 1 ' )

Step 6 : open Arduino IDE in the Raspberry Pi and click to serial monitor , then a new window will open type 1 and click send , now observe the LED on Arduino it will blink three times , that mean the Arduino receive bits from Raspberry Pi three time each bit after one second .

Raspberry Pi communicats Arduino

 

  • Sending data from Arduino to Raspberry

The Arduino Micro-controller sends 1s and 0s  (bits) that contain necessary information one by one, or Serially as group of bits together that turn into bytes (composed of 8 bits) , however in this tutorial you will send one bit at time .

Step 7: open Arduino IDE in the Raspberry Pi and write the following code :

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

void loop(){
      Serial.println(“Hello Raspberry”);
      delay(1000);
}

Step 8 : In order to get serial communication data and print it in terminal you have to get something could “ grap serial “  , type this command in Raspberry Pi terminal :

wget https://raw.githubusercontent.com/tbird20d/grabserial/master/grabserial grabserial

communication between Raspberry Pi and Arduino 1

Step 9 : type this command in the terminal

python grabserial

Then the Raspberry Pi will receive “ Hello Raspberry “ each second.

Arduino communication Raspberry Pi

 

Congratulation, you successfully done the communication from Arduino to Raspberry Pi. Now in your Project you might require to write a python code that receives the serial data not in the terminal, to do so check This Tutorial were an xbee module uses the USB port to send and receive serial date with the Raspberry Pi by using the "pySerial" Library in the python code. You can easily integrate and use the same commands to receive data and send data between your Raspberry Pi and your Arduino Board.

Leave a Comment