Page 1 of 1

Open MV Pan and Tilt

Posted: Thu Nov 16, 2017 4:39 am
by Ebo
Hi im currently a mechatronics student.

One of our projects was to design a pan and tilt device (Relatively old) using an OpenMV Cam and Arduino uno. The motors in the current pan and tilt uses voltage to trigger certain directions it has to move, it has a separate wire for each of the directions it can move. Our project was to interface the openMV cam so that it can track either IR light or colours. Any help for possible schematics or code i could use to actuate the pins of the arduino would help alot.

Kind regards,
Sleepy engineering Student

Re: Open MV Pan and Tilt

Posted: Thu Nov 16, 2017 11:58 am
by kwagyeman
Hi, we sell a servo shield on our website. Please drop the Arduino from the loop. You don't need it.

Re: Open MV Pan and Tilt

Posted: Sun Nov 19, 2017 7:17 pm
by Ebo
We were given an object we have to innovate, its an old pan and tilt device. It uses basic DC motors to change direction in x and y direction. We have to interface that with the openMV Cam.

Re: Open MV Pan and Tilt

Posted: Sun Nov 19, 2017 8:04 pm
by kwagyeman
Ah, I see. So, for these DC motors... Can you tell me how you go about holding the pan and tilt device in one position with them? Do you have some type of feedback on the motor position to close a loop? If so, then you'll want an Arduino for the motor control.

Re: Open MV Pan and Tilt

Posted: Sun Nov 19, 2017 9:02 pm
by Ebo
As of right now, its just a normal dc motor. We were thinking of using an arduino chip because we arent allowed to use a development board. Only problem is reading the values for x and y in the face detection or colour detection program and outputing it to the motors. is there any way of maybe getting an output voltage on one of the pins of the OpenMV Cam? The voltage could tell the motor when to go high and low. I tried looking online for solutions, but ive only gotten servo motor examples. I have until the 27th to develope this systems and its tedious not having any guide towards solving the problem.

Re: Open MV Pan and Tilt

Posted: Mon Nov 20, 2017 12:28 pm
by Ebo
Theres something wrong with the program. Its not outputting to my servo (Pin 7); any help???

# Pixy UART Emulation Script
# This script allows your OpenMV Cam to emulate the Pixy (CMUcam5) in UART mode.
# Note that you need to setup the lab color thresholds below for your application.
# P4 = TXD
# P5 = RXD
# P7 = Servo 1
# P8 = Servo 2

# Pixy Parameters ############################################################

color_code_mode = 1 # 0 == Disabled, 1 == Enabled, 2 == Color Codes Only, 3 == Mixed

max_blocks = 1000
max_blocks_per_signature = 1000
min_block_area = 20

uart_baudrate = 19200

# Pan Servo
s0_lower_limit = 1000 # Servo pulse width lower limit in microseconds.
s0_upper_limit = 2000 # Servo pulse width upper limit in microseconds.

# Tilt Servo
s1_lower_limit = 1000 # Servo pulse width lower limit in microseconds.
s1_upper_limit = 2000 # Servo pulse width upper limit in microseconds.

analog_out_enable = False # P6 -> Analog Out (0v - 3.3v).
analog_out_mode = 0 # 0 == x position of largest blob - 1 == y position of largest blob

# Parameter 0 - L Min.
# Parameter 1 - L Max.
# Parameter 2 - A Min.
# Parameter 3 - A Max.
# Parameter 4 - B Min.
# Parameter 5 - B Max.
# Parameter 6 - Is Color Code Threshold? (True/False).
# Parameter 7 - Enable Threshold? (True/False).
lab_color_thresholds = [(0, 100, 40, 127, -128, 127, True, True), # Generic Red Threshold
(0, 100, -128, -10, -128, 127, True, True), # Generic Green Threshold
(0, 0, 0, 0, 0, 0, False, False),
(0, 0, 0, 0, 0, 0, False, False),
(0, 0, 0, 0, 0, 0, False, False),
(0, 0, 0, 0, 0, 0, False, False),
(0, 0, 0, 0, 0, 0, False, False)]

fb_pixels_threshold = 500 # minimum number of pixels that must be in a blob
fb_merge_margin = 5 # how close pixel wise blobs can be before merging


e_lab_color_thresholds = [] # enabled thresholds
e_lab_color_code = [] # enabled color code
e_lab_color_signatures = [] # original enabled threshold indexes
for i in range(len(lab_color_thresholds)):
if lab_color_thresholds[7]:
e_lab_color_signatures.append(i + 1)

import image, math, pyb, sensor, struct, time

# Camera Setup

sensor.skip_frames(time = 2000)

# LED Setup

red_led = pyb.LED(1)
green_led = pyb.LED(2)
blue_led = pyb.LED(3)

# DAC Setup

dac = pyb.DAC("P6") if analog_out_enable else None

if dac:

# Servo Setup

min_s0_limit = min(s0_lower_limit, s0_upper_limit)
max_s0_limit = max(s0_lower_limit, s0_upper_limit)
min_s1_limit = min(s1_lower_limit, s1_upper_limit)
max_s1_limit = max(s1_lower_limit, s1_upper_limit)

s0_pan = pyb.Servo(1) # P7
s1_tilt = pyb.Servo(2) # P8

s0_pan.pulse_width(int((max_s0_limit - min_s0_limit) // 2)) # center
s1_tilt.pulse_width(int((max_s1_limit - min_s1_limit) // 2)) # center

s0_pan_conversion_factor = (max_s0_limit - min_s0_limit) / 1000
s1_tilt_conversion_factor = (max_s1_limit - min_s1_limit) / 1000

def s0_pan_position(value):
s0_pan.pulse_width(round(s0_lower_limit + (max(min(value, 1000), 0) * s0_pan_conversion_factor)))

def s1_tilt_position(value):
s1_tilt.pulse_width(round(s1_lower_limit + (max(min(value, 1000), 0) * s1_tilt_conversion_factor)))

# Link Setup

uart = pyb.UART(3, uart_baudrate, timeout_char = 1000)

def write(data):

def available():
return uart.any()

def read_byte():
return uart.readchar()

# Helper Stuff

def checksum(data):
checksum = 0
for i in range(0, len(data), 2):
checksum += ((data[i+1] & 0xFF) << 8) | ((data[i+0] & 0xFF) << 0)
return checksum & 0xFFFF

def get_normal_signature(code):
for i in range(len(e_lab_color_signatures)):
if code & (1 << i):
return e_lab_color_signatures
return 0

def to_normal_object_block_format(blob):
temp = struct.pack("<hhhhh", get_normal_signature(blob.code()),,, blob.w(), blob.h())
return struct.pack("<hh10s", 0xAA55, checksum(temp), temp)

def get_color_code_signature(code):
color_code_list = []
for i in range(len(e_lab_color_signatures)):
if code & (1 << i):
octal = 0
color_code_list_len = len(color_code_list) - 1
for i in range(color_code_list_len + 1):
octal += color_code_list << (3 * (color_code_list_len - i))
return octal

def to_color_code_object_block_format(blob):
angle = int((blob.rotation() * 180) // math.pi)
temp = struct.pack("<hhhhhh", get_color_code_signature(blob.code()),,, blob.w(), blob.h(), angle)
return struct.pack("<hh12s", 0xAA56, checksum(temp), temp)

def get_signature(blob, bits):
return get_normal_signature(blob.code()) if (bits == 1) else get_color_code_signature(blob.code())

def to_object_block_format(blob, bits):
return to_normal_object_block_format(blob) if (bits == 1) else to_color_code_object_block_format(blob)

# FSM Code

fsm_state = 0
last_byte = 0


def parse_byte(byte):
global fsm_state
global last_byte

if fsm_state == FSM_STATE_NONE:
if byte == 0x00: fsm_state = FSM_STATE_ZERO
else: fsm_state = FSM_STATE_NONE

elif fsm_state == FSM_STATE_ZERO:
if byte == 0xFF: fsm_state = FSM_STATE_SERVO_CONTROL_0
elif byte == 0xFE: fsm_state = FSM_STATE_CAMERA_CONTROL
elif byte == 0xFD: fsm_state = FSM_STATE_LED_CONTROL_0
else: fsm_state = FSM_STATE_NONE

elif fsm_state == FSM_STATE_SERVO_CONTROL_0:

elif fsm_state == FSM_STATE_SERVO_CONTROL_1:
s0_pan_position(((byte & 0xFF) << 8) | ((last_byte & 0xFF) << 0))

elif fsm_state == FSM_STATE_SERVO_CONTROL_2:

elif fsm_state == FSM_STATE_SERVO_CONTROL_3:
fsm_state = FSM_STATE_NONE
s1_tilt_position(((byte & 0xFF) << 8) | ((last_byte & 0xFF) << 0))

elif fsm_state == FSM_STATE_CAMERA_CONTROL:
fsm_state = FSM_STATE_NONE
# Ignore...

elif fsm_state == FSM_STATE_LED_CONTROL_0:
if byte & 0x80: red_led.on()

elif fsm_state == FSM_STATE_LED_CONTROL_1:
if byte & 0x80: green_led.on()

elif fsm_state == FSM_STATE_LED_CONTROL_2:
fsm_state = FSM_STATE_NONE
if byte & 0x80: blue_led.on()

last_byte = byte

# Main Loop

pri_color_code_mode = color_code_mode % 4

def bits_set(code):
count = 0
for i in range(7):
count += 1 if (code & (1 << i)) else 0
return count

def color_code(code):
for i in range(len(e_lab_color_code)):
if code & (1 << i):
return e_lab_color_code
return False

def fb_merge_cb(blob0, blob1):
if not pri_color_code_mode:
return blob0.code() == blob1.code()
return True if (blob0.code() == blob1.code()) else (color_code(blob0.code()) and color_code(blob1.code()))

def blob_filter(blob):
if(pri_color_code_mode == 0):
return True
elif(pri_color_code_mode == 1): # color codes with two or more colors or regular
return (bits_set(blob.code()) > 1) or (not color_code(blob.code()))
elif(pri_color_code_mode == 2): # only color codes with two or more colors
return (bits_set(blob.code()) > 1)
elif(pri_color_code_mode == 3):
return True

clock = time.clock()
img = sensor.snapshot()
blobs = list(filter(blob_filter, img.find_blobs(e_lab_color_thresholds, area_threshold = min_block_area, pixels_threshold = fb_pixels_threshold, merge = True, margin = fb_merge_margin, merge_cb = fb_merge_cb)))

# Transmit Blobs #

if blobs and (max_blocks > 0) and (max_blocks_per_signature > 0): # new frame
dat_buf = struct.pack("<h", 0xAA55)
sig_map = {}
first_b = False

for blob in sorted(blobs, key = lambda x: x.area(), reverse = True)[0:max_blocks]:
bits = bits_set(blob.code())
sign = get_signature(blob, bits)

if not sign in sig_map:
sig_map[sign] = 1
sig_map[sign] += 1

if sig_map[sign] <= max_blocks_per_signature:
dat_buf += to_object_block_format(blob, bits)

if dac and not first_b:
x_scale = 255 / (img.width()-1)
y_scale = 255 / (img.height()-1)
dac.write(round((blob.y() * y_scale) if analog_out_mode else (blob.x() * x_scale)))
first_b = True

dat_buf += struct.pack("<h", 0x0000)
write(dat_buf) # write all data in one packet...

else: # nothing found
write(struct.pack("<h", 0x0000))

if dac:

# Parse Commands #

for i in range(available()):

num_blobs = min(len(blobs), max_blocks)
print("%d blob(s) found - FPS %f" % (num_blobs, clock.fps()))

Re: Open MV Pan and Tilt

Posted: Mon Nov 20, 2017 1:06 pm
by kwagyeman
Hi, there are multiple examples on the forums of how to send face detection results out through the serial port. This is very easy. Here's a script to do it attached:

Re the previous comment, you need to setup the color tracking thresholds. I was just giving you some example code.

Re: Open MV Pan and Tilt

Posted: Mon Nov 20, 2017 2:41 pm
by Ebo
Can i maybe have a schematic that shows what pins on the servo shield connects to which pins on the openMV cam

Re: Open MV Pan and Tilt

Posted: Mon Nov 20, 2017 11:15 pm
by kwagyeman
Please see the servo shield product page and click on the specs tab.

Re: Open MV Pan and Tilt

Posted: Sat Nov 25, 2017 8:11 am
by Ebo
Thank you, looked at it.. I have another question; How do i send a 3.3V signal to a relay that's going to be used to actuate the pan and tilt(pan and tilt moves when 24VAC is sent to one of the 6 pins). I want to send a signal to the relay that triggers the pan and tilt device to move. Each pin moves the pan and tilt differently i.e Green(Up) White(Down) Red(Left) and Blue(Right). So depending on whats being tracked it'll move the device accordingly. We're also thinking about using AprilTags since it can track further without inteferences from light etc.
Some help on this would be appreciated.

Re: Open MV Pan and Tilt

Posted: Sat Nov 25, 2017 11:02 am
by kwagyeman
You can turn pins off and on on the OpenMV Cam in code. See here: ... s-and-gpio

You just pulse the pin high per frame until you don't need to anymore.

Do you know how to write a P control loop? And how to drive a transistor with the 3.3V on the I/O pin to control the relay?

Re: Open MV Pan and Tilt

Posted: Sat Nov 25, 2017 12:36 pm
by Ebo
Thank you so much.. do you maybe know what 4 pins i can use on the OpenMV Cam? Also how do i make the pins HIGH for framerate? Can you maybe give me an example.. Apriltag tracking is a success. Now i need to trigger the corresponding pin.

Re: Open MV Pan and Tilt

Posted: Sat Nov 25, 2017 2:14 pm
by kwagyeman
Here's the basic code idea:

Code: Select all

# AprilTags Example
# This example shows the power of the OpenMV Cam to detect April Tags
# on the OpenMV Cam M7. The M4 versions cannot detect April Tags.

import sensor, image, time, math, pyb

sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()

# Note! Unlike find_qrcodes the find_apriltags method does not need lens correction on the image to work.

# The apriltag code supports up to 6 tag families which can be processed at the same time.
# Returned tag objects will have their tag family and id within the tag family.

tag_families = 0
tag_families |= image.TAG16H5 # comment out to disable this family
tag_families |= image.TAG25H7 # comment out to disable this family
tag_families |= image.TAG25H9 # comment out to disable this family
tag_families |= image.TAG36H10 # comment out to disable this family
tag_families |= image.TAG36H11 # comment out to disable this family (default family)
tag_families |= image.ARTOOLKIT # comment out to disable this family

# What's the difference between tag families? Well, for example, the TAG16H5 family is effectively
# a 4x4 square tag. So, this means it can be seen at a longer distance than a TAG36H11 tag which
# is a 6x6 square tag. However, the lower H value (H5 versus H11) means that the false positve
# rate for the 4x4 tag is much, much, much, higher than the 6x6 tag. So, unless you have a
# reason to use the other tags families just use TAG36H11 which is the default family.

def family_name(tag):
    if( == image.TAG16H5):
        return "TAG16H5"
    if( == image.TAG25H7):
        return "TAG25H7"
    if( == image.TAG25H9):
        return "TAG25H9"
    if( == image.TAG36H10):
        return "TAG36H10"
    if( == image.TAG36H11):
        return "TAG36H11"
    if( == image.ARTOOLKIT):
        return "ARTOOLKIT"


h_drive_forward  = pyb.Pin('P0', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
h_drive_backward = pyb.Pin('P1', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
v_drive_forward  = pyb.Pin('P2', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)
v_drive_backward = pyb.Pin('P3', pyb.Pin.OUT_PP, pyb.Pin.PULL_NONE)

    img = sensor.snapshot()
    tags = img.find_apriltags(families=tag_families) # defaults to TAG36H11 without "families".
    for tag in tags:
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(,, color = (0, 255, 0))
        print_args = (family_name(tag),, (180 * tag.rotation()) / math.pi)
        print("Tag Family %s, Tag ID %d, rotation %f (degrees)" % print_args)
    if tags:
        close_tag = min(tags, key = lambda x: x.z_translation())
        if < ((img.width()//2) - H_DEAD_ZONE):
        elif > ((img.width()//2) + H_DEAD_ZONE):
        if < ((img.height()//2) - V_DEAD_ZONE):
        elif > ((img.height()//2) + V_DEAD_ZONE):

It works by driving 4 i/o pins to control the relays. It will drive the relays until the closest tag is centered in the image.

Re: Open MV Pan and Tilt

Posted: Sat Nov 25, 2017 3:37 pm
by Ebo
Wow didn't even think of getting a position using the difference in edge to the rectangle. Some clever thinking and then setting the pin HIGH according to those measurements. Thank you. Im going to try and connect a few Relays to pins. The Pan and Tilt will use the 24VAC from Transformer to move the motors accordingly. Im going to attemp some PID control to feedback information to the OpenMV Cam.

Happy Engineering Student

Re: Open MV Pan and Tilt

Posted: Sun Nov 26, 2017 12:02 am
by Ebo
How do i set all pins to 0 if nothing is being tracked on the OpenMV i.e theres no rectangle and cross.

Re: Open MV Pan and Tilt

Posted: Sun Nov 26, 2017 11:51 pm
by kwagyeman
Put an else statement after under the

Code: Select all

if tags:

Code: Select all


Re: Open MV Pan and Tilt

Posted: Wed Nov 29, 2017 7:01 pm
by Ebo
It works perfectly. I got 3.3V relays.. the open mv cam outputs 3.3 V which is correct, the relay should trigger, but the current is too small (0.6A). I need it at atleast 0.8A to make the relays trigger. I have to build a circuit that can boost the current of the input that goes to the 3.3V relay. Researching Transistors to maybe draw current from a source.Or even draw the circuit to ground and source it from there. Transistors seem like the best option. What do you think?

Sleepless Engineering Student.

Re: Open MV Pan and Tilt

Posted: Thu Nov 30, 2017 12:55 am
by kwagyeman
See here: ... BqfDA_OA6M:

Very standard circuit. The diode is there to dealy with something called back EMF which is produced when the relay turns off. Coils of wire tend to dump a massive negative voltage onto the power rails when they switch off so the diode absorbs that.

Re: Open MV Pan and Tilt

Posted: Thu Nov 30, 2017 5:55 pm
by Ebo
Ive seen those examples online. Thats where i got the idea for Transistors. But does this example solve the problem of Current? I see the emitter of the Transistor goes straight to ground. and voltage is being dropped before the transistor. Since V = IR, wont I also become smaller because of the resistors? Just curious about how it switches a 3.3V

Stay Awake Engineering Student.

Re: Open MV Pan and Tilt

Posted: Thu Nov 30, 2017 6:13 pm
by kwagyeman
The OpenMV Cam io pins can only provide 25 ma each, with a total of 150ma across all pins. Each of those transistors can do 500 ma and above.

Google the word amplifier.

Re: Open MV Pan and Tilt

Posted: Fri Dec 01, 2017 12:27 pm
by Ebo
Okay Im going to have to Amplify the signal being sent from the OpenMV Cam (3.3V and 0.05A). Hoepfully I'll be able to get it to the desired 3.3V and 0.08A. Thanks. This is the last part of the project, then it should work 100%. Will upload a video as soon as its done.

Kind regards,
Sleepy Engineering Student

Re: Open MV Pan and Tilt

Posted: Fri Dec 01, 2017 8:08 pm
by Ebo ... t-Using-B/

I used the above circuit. With a 1k Resistor.. But for some reason when i rebuild the circuit. The relay switches once and not again, Im using a 7V battery as the VSS and 3.3 as the input at v1. I have the 2N2222 Transistor connected correctly, but im not sure why it only triggers the relay once and not again when i move the apriltag infront of it, because i am getting 3.3 at the output.

Some help would go a real long way since this is the last problem we're having.

Kind regards,
Sleepy Engineering Student

Re: Open MV Pan and Tilt

Posted: Fri Dec 01, 2017 9:05 pm
by kwagyeman
Um, can you measure the voltage on the transistor base? The pin that connects to the OpenMV Cam? Make sure that the OpenMV Cam's IOs are working properly.

Also, can you draw me a circuit? By hand and scanned or via paint is fine.

Re: Open MV Pan and Tilt

Posted: Sat Dec 02, 2017 1:58 am
by Ebo
Got it triggering.. Turns out I cant source from a 9V Battery. I need to use a constant DC voltage from a power supply.. I attached the circuit Im using.

Re: Open MV Pan and Tilt

Posted: Sat Dec 02, 2017 1:39 pm
by kwagyeman
Okay, great.

Re: Open MV Pan and Tilt

Posted: Sat Dec 02, 2017 4:47 pm
by Ebo
Thank you so much for the assistance on the project. Will upload a video to youtube soon, if Im allowed to put OpenMV on the web. Full credit will be given to OpenMV. In the final testing stages and handling the fact that our circuit is drawing abit too much current.

Kind regards,

Seeing light at the end of the tunnel Engineering Student

Re: Open MV Pan and Tilt

Posted: Sun Dec 03, 2017 11:59 am
by Ebo
Circuit is working, only problem we're having is the circuit is drawing too much current. ANY Ideas to how this can be solved? Our transformer gets hot and starts smoking, the rating on the transformer is 4A at 24VAC.

Kind regards,
Sleepy Engineering Student.

Re: Open MV Pan and Tilt

Posted: Sun Dec 03, 2017 2:02 pm
by kwagyeman
I don't know why your professor is letting you play with 24VAC and 4A power rails. Seems kinda in the dangerous zone.

Um, if the system maintains position one moved then just increase the dead zone value I put in the code so it's not drawing power trying to keep the camera centered on something.

Re: Open MV Pan and Tilt

Posted: Sun Dec 03, 2017 5:51 pm
by Ebo
Okay i'll give that a go.. When the pan and tilt gets power, it moves.. but as soon as i hook up a normal closed Relay, it gets the transformer overheating. When i connect it in Normally Open, it does nothing, because i cant switch the coil of the relay, so the pan and tilt moves on its own, counter clockwise. Didnt know it might be a programming problem, thought maybe it was a hardware malfunction. What do you think would be an ideal deadzone?

Thank you very much for the help with this project, its giving me sleepless nights.

Sleepy Engineering Student.

Re: Open MV Pan and Tilt

Posted: Sun Dec 03, 2017 6:35 pm
by kwagyeman
You have to test until you find a dead zone value that works without needed a lot of power. The only way to find this is to try different values.

Re: Open MV Pan and Tilt

Posted: Sun Dec 03, 2017 7:34 pm
by Ebo
Okay i'll play around with the value. But I havent connected my OpenMV Cam to the pan and tilt yet. Im running the Pan and Tilt without the camera. First testing relays, but thats when the transformer gets hot. As soon as i can get the relays running in NC i'll hook up the OpenMV Cam.

,Sleepy Engineering Student