Open MV Pan and Tilt

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

Hi, we sell a servo shield on our website. Please drop the Arduino from the loop. You don’t need it.

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.

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.

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.

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_signaturesreturn 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


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_codereturn 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()))_

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. (1.72 KB)

Can i maybe have a schematic that shows what pins on the servo shield connects to which pins on the openMV cam

Please see the servo shield product page and click on the specs tab.

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.

You can turn pins off and on on the OpenMV Cam in code. See here:

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?

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.

Here’s the basic code idea:

# 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.

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

How do i set all pins to 0 if nothing is being tracked on the OpenMV i.e theres no rectangle and cross.

Put an else statement after under the

if tags:


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.

See here:

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.

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.

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.