I want to connect OpenCV (Python) with arduino Uno when it detects a tennis ball.

OpenCV code:

import numpy as np
import cv2
import serial
face_cascade = cv2.CascadeClassifier('tennisballdetect.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:

# Module -2: Trigger Pyserial
if detect > 0:

     ser = serial.Serial('COM1', 9600)

   ser = serial.Serial('COM1', 9600)

My Arduino Code:

#include <Servo.h> 
int servoPin = 3;
Servo Servo1;
char incomingBit;    // for incoming serial data

void setup() {
// We need to attach the servo to the used pin number 
// Servo1.attach(servoPin);
pinMode(servoPin, OUTPUT);      // sets the digital pin as output
Serial.begin(9600);    // opens serial port, sets data rate to 9600 bps
void loop() {
if (Serial.available() > 0) {       // Serial.available(), which detects if 
any data has been sent to the 
                                   //serial port
 //  Serial.read() will read the incoming character and store it in 
    incomingBit = Serial.read();
    Serial.print("I received:  ");
    //if the value of incomingBit is ‘Y’, we need to turn the LED ‘on’, else we turn it ‘off’. So, 
    //in a nutshell,if detecting faces from a given image was successful, this will turn on the LED

            if(incomingBit == 'Y' || incomingBit == 'y') {

             // Make servo go to 0 degrees 
 // Make servo go to 90 degrees 
 // Make servo go to 180 degrees 
            else {
              digitalWrite(servoPin, LOW); 

Servo is correctly connected to Arduino and it is moving without serial communication. openCV is also working and detecting tennisball.

But when trying to connect OpenCV to arduino, servo is not moving. Any kind of help is appreciated.

  • Put a serial.print incomingBit. Also serial.read returns a byte. Could be that your parse is failing. – Sigma Apr 21 '17 at 21:56
  • Can you be a bit more specific? I am new to this and this is not very clear to me.Sorry for that. – sayem48 Apr 22 '17 at 17:25
  • Put Serial.println(incomingBit) after serial.read() to see if you get an incoming command – Sigma Apr 22 '17 at 21:42
  • You need to debug the code. Is the message sent through the COM(serial communication), if so, is it recived on the other end? Anyway put ´Serial.print("I received: "); Serial.println(incomingBit);´ to se if anything is recived on the other end. Then get back to me! =) – Sigma Apr 23 '17 at 11:03
  • The "detect" value in Python code is >0 but still when I upload my arduino code , I get no value at arduino IDE. Is there any way to solve it? @sigma – sayem48 Apr 24 '17 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.