Easiest way to connect a Raspberry Pi with Arduino - USB

In our previous blogs on computer vision using OpenCV, we had a look at a lot of blogs on computer vision. The last blog posted helped us detect a human face and eye using the cam from the laptop. But there is a common problem and question that everyone faces as a next step. How do we connect the computer vision algorithm to take some physical actions from the robot like chasing a RED ball when a robot car sees it? Or the robot moving towards a human face when it detects a human face in real time. The solution for this problem statement at least in hobby grade robotics is to interface the computer vision(input) to mechanics(output).

This can be drawn as a simple flow as -

a)Robot sees the environment around itself (Input) b)Robot detects a RED Ball (Trigger event) c)Robot moves towards the RED Ball(Output/action)

This blog would deal with moving from point b to point c as mentioned above via using an interface. It is no brainer that we can use OpenCV over Python2 in a single board computer for point a. For point b we can use a PiCam or an HD camera connected via USB. For Point c we need to have a robot which has an H-bridge driver connected via a micro-controller like an Arduino.

In terms of interface between a Raspberry Pi and Arduino, we have four options -

Option one - Use a USB cable between Raspberry Pi and Arduino

Option two - Use the I2C

Option three - Use Bluetooth master-slave between both Arduino and Pi

Option four - Use the GPIO

We will use the option one of USB in this blog as this is the most simple way to connect these two devices. Since we talked about integrating computer vision we will use Python on Raspberry side.

Step 1 - Raspberry side work :

Install a python package called as pySerial which can be downloaded using PIP as -

pip install pyserial

If you get any error you would need to install pip first. Now move to terminal and type 3 lines as below -

# This code sends a value 5 to the Arduino Uno connected

>>> import serial # serial is the class you need to import for pyserial >>> ser = serial.Serial('/dev/ttyUSB0', 9600) #Open serial port with baud rate as 9600 >>> ser.write('5') # write a string with value 5

You are done with the Python code and Raspberry Pi side.

Step 2 - Arduino Uno side work :

Now on the Arduino side use a code like -

// Open a serial connection and flash LED when input is received from Python code from Raspberry Pi

void setup(){ // Open serial connection. Serial.begin(9600); pinMode(13, OUTPUT); }

void loop(){ if(Serial.available() > 0){ // if data present, blink digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); digitalWrite(13, HIGH); delay(1000); } }

Compile the code and make sure that the baud rate matches in both cases, in our example it is 9600. Connect the Arduino with it's own power source as power from Pi may not be enough. You may use a power bank for this.

Now go to Pi side and run the python code and you should see the Uno default onboard LED blinking. Yahoo ! You have made the Pi as master to slave Uno.

This interface is not asynchronous but can be made easily synchronous by making the Uno send a character back to Pi and Pi doing a close connection using -

>> ser.close() # close port

Once you are able to control the LED's using Python from Pi side you can start working on the OpenCV code to send characters when the robot camera sees a RED Ball. To calculate the distance from the camera to the object 'triangle similarity' method may be used. Hope this blog provides the first test which you can do to bring the best of both Arduino and Raspberry Pi. A working robot like SID2 used this method as shown in this video.

Attribution : By Evan-Amos (Own work) [Public domain], via Wikimedia Commons

References :



#software #engineeringcollege #hardware