Connecting OpenMV to Machine CAM switch

Hello there,

I am working on a simple project of detecting a colour and sending stop command to the machine. I have used the library and some example files to detect the colour (Red) and modified to turn on the LEDs based on the condition. Now, I need to send signal to the real machine which is operating with Cam switches. I want to know how to connect the openMV to the switch? Using Pin0 for output is enough or is it possible to receive the currect state of the switch and then send signal to stop the machine? I tried to learn from the library and documentations but I am confused on application. Can someone help me to configure this? Any help is appreciated. Thank you

Yes, see the pyb.Pin class.

Hello Mr. Kwagyeman,

I have used a simple program to turn off a machine using OpenMV cam H7. I used the following code from the example files found in the forum. The idea is that to stop the machine if the camera sense more pixels of red colour in the frame. In our production process, the colour will reveal slowly and we can sense the pixels and set a limit to send the stop signal.

import sensor, image, time, pyb
from pyb import LED
from pyb import Pin

red_led   = LED(1)
green_led = LED(2)

p = pyb.Pin('P0', pyb.Pin.OUT_PP,pyb.Pin.PULL_NONE)

def led_control(x):
    if   (x&1)==0:
    elif (x&1)==1: red_led.on()
    if   (x&2)==0:
    elif (x&2)==2: green_led.on()

threshold = (30, 100, 20, 127, 20, 127),  # generic_red_thresholds (code 1)

sensor.skip_frames(time = 2000)
clock = time.clock()

    img = sensor.snapshot()
    blob_list = img.find_blobs(threshold, pixels_threshold=200, area_threshold=200)

    red_bool = 0
    green_bool = 0

    for b in blob_list:
        br = b.rect()
        l = (b.w() + b.h())
        lm = l/2
        if b.code() & (lm < 140): # No stop - Started seeing colour
            red_bool = 0
            green_bool = 2
            img.draw_rectangle(b.rect(), color=(255,0,0), thickness=2)
            img.draw_cross(,, color=(255,0,0), thickness=2)
        elif b.code() & (lm > 140): # Stop - Colour intensity reached
            green_bool = 0
            red_bool = 1

    led_control(red_bool | green_bool)

I used a 3VDC (3v to 24v relay to connect the OpenMV H7 to the machine’s switch.
I had an issue in sending the 3v completely as output. When I measure the output voltage, it shows 1.75 V and sometimes 1.5V.

And suddenly the openMV is not working and there is a message to the laptop saying that there is a power surge on the USB port. I think something damaged the circuit. When I plug it in again, it heats up quickly and doesn’t work.

Is there any solution for this or I need to buy another product?

If I am successful with this project, I will buy 36 to 40 more openMV and use it for all the machines in our production line. Kindly guide me through this project. Thank you.


How did you connect the relay? It would likely need a transistor circuit of some description between the pin and the relay (and a protection diode)


Unfortunately I didn’t use any protection between the relay and the openMV. I connected the P0 pin and 3.3V to the relay. Could you tell me what type of transistor, should I use and how to connect the relay, OpenMV and the transistor. I apologies for these dumb questions but I am a noob in these electrical stuffs.

Your help is very much appreciated. Thank you.


It’s like this:

If you need a PCB made up, please let me know and I will quote for it. I have one with a latching relay and a lot of other stuff already working.

The orange component is a latching relay I use to turn on/off a 4G router.


Thank you for the support. I will try using the transistor and flywheel diode as mentioned in the diagram.