# Using the pan tilt output pins

**URL:** https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592
**Category:** General
**Created:** [October 16, 2023, 1:08am UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592 "2023-10-16T01:08:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![myraspberrypi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@myraspberrypi](https://forum.sunfounder.com/u/myraspberrypi)
#### Post date: [October 16, 2023, 1:08am UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/1 "2023-10-16T01:08:50Z")

</div>

Great lesion 58 went really smooth. When I tried to turn on some LEDs using the THRU pins on the PAN/TILD. to indicate which direction it was moving.  
The camera locks up. Error says try setting BOARD or BCM. I have set it up both ways and used different pins. But still get the error.  
Seems I may be interfering with the servo signals. But I am not using 12 or 13 outputs to light my LEDs.  
Any helpful suggestions would be appreciated . In terminal mode LEDS work fine . But when running you tracking example using Thonny it  
hangs the camera up and I get the ( try to set mode BCM or BOARD. And I have tried both. Error pops up when i turn on output. Up till then  
it all works fine.

---

<div class="post-metadata">

### Author: ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)
#### Post date: [October 16, 2023, 6:57am UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/2 "2023-10-16T06:57:31Z")

</div>

Can you provide us with the code you wrote so we can test it?

---

<div class="post-metadata">

### Author: ![myraspberrypi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@myraspberrypi](https://forum.sunfounder.com/u/myraspberrypi)
#### Post date: [October 17, 2023, 11:37pm UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/3 "2023-10-17T23:37:53Z")

</div>

Thank you for your response. See attached .  
If I put # on line 109 the camera runs great.  
If I remove # and try to turn on output. Program locks up.

Also Line 9 I do not know if camera pan/tilt feed thru are BCM or BOARD I think it should be BOARD.  
Tried both but program still hangs up due to line 109.

I am trying to turn on 4 LED’s to simulate output in all 4 directions PAN +/- and Tilt +/-

Any help is greatly appreciated  
Mike

(Attachment openCV-10.py is missing)

---

<div class="post-metadata">

### Author: ![myraspberrypi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@myraspberrypi](https://forum.sunfounder.com/u/myraspberrypi)
#### Post date: [October 17, 2023, 11:48pm UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/4 "2023-10-17T23:48:42Z")

</div>

import cv2  
from picamera2 import Picamera2  
import time  
import numpy as np  
from servo import Servo  
#picam2 = Picamera2()  
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)  
GPIO.setup(17,GPIO.OUT)

pan=Servo(pin=13)  
tilt=Servo(pin=12)

panAngle=0  
tiltAngle=0

pan.set\_angle(panAngle)  
tilt.set\_angle(tiltAngle)

picam2 = Picamera2()  
dispW=1280  
dispH=720  
picam2.preview\_configuration.main.size = (dispW,dispH)  
picam2.preview\_configuration.main.format = “RGB888”  
picam2.preview\_configuration.controls.FrameRate=30  
picam2.preview\_configuration.align()  
picam2.configure(“preview”)  
picam2.start()  
fps=0  
pos=(30,60)  
font=cv2.FONT\_HERSHEY\_SIMPLEX  
height=1.5  
weight=3  
myColor=(0,0,255)  
track=0

def onTrack1(val):  
global hueLow  
hueLow=val  
print(‘Hue Low’,hueLow)  
def onTrack2(val):  
global hueHigh  
hueHigh=val  
print(‘Hue High’,hueHigh)  
def onTrack3(val):  
global satLow  
satLow=val  
print(‘Sat Low’,satLow)  
def onTrack4(val):  
global satHigh  
satHigh=val  
print(‘Sat High’,satHigh)  
def onTrack5(val):  
global valLow  
valLow=val  
print(‘Val Low’,valLow)  
def onTrack6(val):  
global valHigh  
valHigh=val  
print(‘Val High’,valHigh)  
def onTrack7(val):  
global track  
track=val  
print(‘Track Value’ ,track)

cv2.namedWindow(‘myTracker’)

cv2.createTrackbar(‘Hue Low’,‘myTracker’,10,179,onTrack1)  
cv2.createTrackbar(‘Hue High’,‘myTracker’,20,179,onTrack2)  
cv2.createTrackbar(‘Sat Low’,‘myTracker’,100,255,onTrack3)  
cv2.createTrackbar(‘Sat High’,‘myTracker’,2555,255,onTrack4)  
cv2.createTrackbar(‘Val Low’,‘myTracker’,100,255,onTrack5)  
cv2.createTrackbar(‘Val High’,‘myTracker’,255,255,onTrack6)  
cv2.createTrackbar(‘Train-0 Track-1’,‘myTracker’ ,0,1,onTrack7)

while True:  
tStart=time.time()  
frame= picam2.capture\_array()  
frame=cv2.flip(frame,-1)  
frameHSV=cv2.cvtColor(frame,cv2.COLOR\_BGR2HSV)  
cv2.putText(frame,str(int(fps))+’ FPS’,pos,font,height,myColor,weight)  
lowerBound=np.array([hueLow,satLow,valLow])  
upperBound=np.array([hueHigh,satHigh,valHigh])  
myMask=cv2.inRange(frameHSV,lowerBound,upperBound)  
myMaskSmall=cv2.resize(myMask,(int(dispW/2),int(dispH/2)))  
myObject=cv2.bitwise\_and(frame,frame, mask=myMask)  
myObjectSmall=cv2.resize(myObject,(int(dispW/2),int(dispH/2)))

contours,junk=cv2.findContours(myMask,cv2.RETR\_EXTERNAL,cv2.CHAIN\_APPROX\_SIMPLE)  
if len(contours)\>0:  
contours=sorted(contours,key=lambda x:cv2.contourArea(x),reverse=True)  
#cv2.drawContours(frame,contours,-1,(255,0,0),3)  
contour=contours[0]  
x,y,w,h=cv2.boundingRect(contour)  
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)  
if track==1:  
error=(x+w/2)-dispW/2  
if error\>35:  
panAngle=panAngle-1  
GPIO.output(17,1)  
if panAngle\<-90:  
panAngle=-90  
pan.set\_angle(panAngle)

if error\<-35:  
panAngle=panAngle+1  
if panAngle\>90:  
panAngle=90  
pan.set\_angle(panAngle)  
tiltError=(y+h/2)-dispH/2  
if tiltError\>35:  
tiltAngle=tiltAngle+1  
if tiltAngle\>40:  
tiltAngle=40  
tilt.set\_angle(tiltAngle)  
if tiltError\<-35:  
tiltAngle=tiltAngle-1  
if tiltAngle\<-90:  
tiltAngle=-90  
tilt.set\_angle(tiltAngle)

cv2.imshow(“Camera”, frame)  
cv2.imshow(‘Mask’,myMaskSmall)  
cv2.imshow(‘My Object’,myObjectSmall)  
if cv2.waitKey(1)==ord(‘q’):  
break  
GPIO.cleanup()  
tEnd=time.time()  
loopTime=tEnd-tStart  
fps=.9_fps + .1_(1/loopTime)  
cv2.destroyAllWindows()

---

<div class="post-metadata">

### Author: ![myraspberrypi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@myraspberrypi](https://forum.sunfounder.com/u/myraspberrypi)
#### Post date: [October 17, 2023, 11:52pm UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/5 "2023-10-17T23:52:25Z")

</div>

It rejected attach py file so I sent text . Hope this helps

---

<div class="post-metadata">

### Author: ![myraspberrypi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@myraspberrypi](https://forum.sunfounder.com/u/myraspberrypi)
#### Post date: [October 18, 2023, 12:06am UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/6 "2023-10-18T00:06:31Z")

</div>

I am trying to turn on a LED line 109 , If PAN is told to go - . In the end I want to turn on 4 led PAN +/- and Tilt +/-  
Just trying one now.

If I put # in front of line 109. Camera runs great . But of course not output.

Also line 9 I do not know if the feed thru pins on the camera board are BCM or BOARD. I think BOARD. Tried both.  
still did not work and camera program still hangs up.  
I tried to send attachment with py file. Your system rejected it twice. So I am sending it as text below.

Ant help is appreciated  
Mike

import cv2  
from picamera2 import Picamera2  
import time  
import numpy as np  
from servo import Servo  
#picam2 = Picamera2()  
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)  
GPIO.setup(17,GPIO.OUT)

pan=Servo(pin=13)  
tilt=Servo(pin=12)

panAngle=0  
tiltAngle=0

pan.set\_angle(panAngle)  
tilt.set\_angle(tiltAngle)

picam2 = Picamera2()  
dispW=1280  
dispH=720  
picam2.preview\_configuration.main.size = (dispW,dispH)  
picam2.preview\_configuration.main.format = “RGB888”  
picam2.preview\_configuration.controls.FrameRate=30  
picam2.preview\_configuration.align()  
picam2.configure(“preview”)  
picam2.start()  
fps=0  
pos=(30,60)  
font=cv2.FONT\_HERSHEY\_SIMPLEX  
height=1.5  
weight=3  
myColor=(0,0,255)  
track=0

def onTrack1(val):  
global hueLow  
hueLow=val  
print(‘Hue Low’,hueLow)  
def onTrack2(val):  
global hueHigh  
hueHigh=val  
print(‘Hue High’,hueHigh)  
def onTrack3(val):  
global satLow  
satLow=val  
print(‘Sat Low’,satLow)  
def onTrack4(val):  
global satHigh  
satHigh=val  
print(‘Sat High’,satHigh)  
def onTrack5(val):  
global valLow  
valLow=val  
print(‘Val Low’,valLow)  
def onTrack6(val):  
global valHigh  
valHigh=val  
print(‘Val High’,valHigh)  
def onTrack7(val):  
global track  
track=val  
print(‘Track Value’ ,track)

cv2.namedWindow(‘myTracker’)

cv2.createTrackbar(‘Hue Low’,‘myTracker’,10,179,onTrack1)  
cv2.createTrackbar(‘Hue High’,‘myTracker’,20,179,onTrack2)  
cv2.createTrackbar(‘Sat Low’,‘myTracker’,100,255,onTrack3)  
cv2.createTrackbar(‘Sat High’,‘myTracker’,2555,255,onTrack4)  
cv2.createTrackbar(‘Val Low’,‘myTracker’,100,255,onTrack5)  
cv2.createTrackbar(‘Val High’,‘myTracker’,255,255,onTrack6)  
cv2.createTrackbar(‘Train-0 Track-1’,‘myTracker’ ,0,1,onTrack7)

while True:  
tStart=time.time()  
frame= picam2.capture\_array()  
frame=cv2.flip(frame,-1)  
frameHSV=cv2.cvtColor(frame,cv2.COLOR\_BGR2HSV)  
cv2.putText(frame,str(int(fps))+’ FPS’,pos,font,height,myColor,weight)  
lowerBound=np.array([hueLow,satLow,valLow])  
upperBound=np.array([hueHigh,satHigh,valHigh])  
myMask=cv2.inRange(frameHSV,lowerBound,upperBound)  
myMaskSmall=cv2.resize(myMask,(int(dispW/2),int(dispH/2)))  
myObject=cv2.bitwise\_and(frame,frame, mask=myMask)  
myObjectSmall=cv2.resize(myObject,(int(dispW/2),int(dispH/2)))

contours,junk=cv2.findContours(myMask,cv2.RETR\_EXTERNAL,cv2.CHAIN\_APPROX\_SIMPLE)  
if len(contours)\>0:  
contours=sorted(contours,key=lambda x:cv2.contourArea(x),reverse=True)  
#cv2.drawContours(frame,contours,-1,(255,0,0),3)  
contour=contours[0]  
x,y,w,h=cv2.boundingRect(contour)  
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),3)  
if track==1:  
error=(x+w/2)-dispW/2  
if error\>35:  
panAngle=panAngle-1  
GPIO.output(17,1)  
if panAngle\<-90:  
panAngle=-90  
pan.set\_angle(panAngle)

if error\<-35:  
panAngle=panAngle+1  
if panAngle\>90:  
panAngle=90  
pan.set\_angle(panAngle)  
tiltError=(y+h/2)-dispH/2  
if tiltError\>35:  
tiltAngle=tiltAngle+1  
if tiltAngle\>40:  
tiltAngle=40  
tilt.set\_angle(tiltAngle)  
if tiltError\<-35:  
tiltAngle=tiltAngle-1  
if tiltAngle\<-90:  
tiltAngle=-90  
tilt.set\_angle(tiltAngle)

cv2.imshow(“Camera”, frame)  
cv2.imshow(‘Mask’,myMaskSmall)  
cv2.imshow(‘My Object’,myObjectSmall)  
if cv2.waitKey(1)==ord(‘q’):  
break  
GPIO.cleanup()  
tEnd=time.time()  
loopTime=tEnd-tStart  
fps=.9_fps + .1_(1/loopTime)  
cv2.destroyAllWindows()

---

<div class="post-metadata">

### Author: ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)
#### Post date: [October 19, 2023, 3:13am UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/7 "2023-10-19T03:13:31Z")

</div>

I tested your code and it basically works fine, of course I’m not quite sure if the indentation of your code is correct, and if the mentioned line #109 refers to GPIO.output(17, 1), here are some of my suggestions:

1. GPIO.setmode() should use the (GPIO.BCM) parameter, for 17, the BCM serial number is valid, and the BOARD serial number, 17 corresponds to 3.3V

2. You can use wringpi tool to check the io status of pi by gpio readall command during installation.  
[wiringPi updated to 2.52 for the Raspberry Pi 4B | Wiring Pi](http://wiringpi.com/wiringpi-updated-to-2-52-for-the-raspberry-pi-4b/)

3. the warning “This channel is already in use” when running in thonny is due to not executing GPIO.cleanup() correctly when exiting, but it doesn’t affect the use.

4. you need to define and pre-assign variables to the hsv values, otherwise the values will only correspond to the values shown in the window if you drag each slider.  
 ![101901](https://canada1.discourse-cdn.com/flex029/uploads/sunfounder/original/1X/6ffca7865a021013db8ab64d7bdd6f2169cb377f.png)

5. It is recommended that the loop in the code use try, finally, to ensure that the end of the correct execution of some processing.  
 ![101902](https://canada1.discourse-cdn.com/flex029/uploads/sunfounder/original/1X/98b3dd2e91343b61a73c932c0b6748eef95868de.png)

6. If sufficient, it is recommended that the camera image resolution be set to 800\*600 to ensure a clear image and to increase the speed of operation.

---

<div class="post-metadata">

### Author: ![myraspberrypi](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@myraspberrypi](https://forum.sunfounder.com/u/myraspberrypi)
#### Post date: [October 20, 2023, 5:30pm UTC](https://forum.sunfounder.com/t/using-the-pan-tilt-output-pins/592/8 "2023-10-20T17:30:31Z")

</div>

It’s working great now.  
Thank you for the great support.  
Mike
