Python Forum
How to use servo motor with TFMini Distance Sensor in python script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use servo motor with TFMini Distance Sensor in python script?
I am using Adafruit TFmini distance sensor, with servo motor. I want the servo motor to move when distance sensor senses a certain distance. My code makes the servo motor move continuously, like crazy.

Credits: TfMini distance sensor code is taken from tfmini GitHub, and servo motor code is from raspberry pi tutorials.

# -*- coding: utf-8 -*
import serial
import math
import time
import RPi.GPIO as GPIO

servoPIN = 18
GPIO.setup(servoPIN, GPIO.OUT)

fire_gun = GPIO.PWM(servoPIN, 50)   #GPIO 5 for PWM with 50Hz
fire_gun.start(2.5) #Initialization

def gunRight():
    #fire_gun = GPIO.PWM(servoPIN, 50)  #GPIO 5 for PWM with 50Hz
    #fire_gun.start(2.5)    #Initialization

    print "Shot Fire The First Net Gun"
    print "Servo Motor Back To Rest Position"

ser = serial.Serial("/dev/ttyUSB0", 115200)

def getTFminiData():
    while True:
        count = ser.in_waiting
        if count > 8:
            recv =
            if recv[0] == 'Y' and recv[1] == 'Y': # 0x59 is 'Y'
                low = int(recv[2].encode('hex'), 16)
                high = int(recv[3].encode('hex'), 16)
                distance = low + high * 256
                #My unit conversion starts here
                dist = float(distance) /100 
                print "Distance to object is:",dist,"Meters"
                #GPIO.setup(TRIG, GPIO.OUT)
                #GPIO.setup(ECHO, GPIO.IN)

                #if dist <= 0.70:
                    #GPIO.output(5, True)   

    if ser.is_open == False:
       for i in range(500):
           if dist <= 0.70:

except KeyboardInterrupt:   # Ctrl+C
    if ser != None:
This package does half of what you want:
perhaps you can easily figure out how to drive servo motor using this for sensor.

Possibly Related Threads…
Thread Author Replies Views Last Post
  stepper motor and servo key control cel 4 425 Jul-27-2020, 06:26 AM
Last Post: DPaul
  Stepper Motor key control cel 2 362 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 386 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  How to kill a bash script running as root from a python script? jc_lafleur 4 675 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 505 May-28-2020, 05:27 PM
Last Post: micseydel
  Audio and servo at the same time cgreenwell 4 844 Jul-04-2019, 01:41 AM
Last Post: Larz60+
  Package python script which has different libraries as a single executable or script tej7gandhi 1 862 May-11-2019, 08:12 PM
Last Post: keames
  Control 2 stepper motor simultaneously jihene 2 1,249 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 934 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  RPi with IR Sensor pimlicosnail 1 932 Jun-24-2018, 08:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020