WT87
Posts: 10
Joined: Thu Jan 17, 2019 7:37 pm
Location: Haarlem, Netherlands

Servo on Adafruit 16-channel PWM/Servo HAT ZeroDivisionError: float division by zero

Wed Apr 17, 2019 7:07 pm

Hi guys,

I while ago I was able to create a small script whereby on a press of a button a LED lid up, sound was played and a servo was running. The servo was directly connected to the Pi and unfortunately always jittered.
On advice I bought a adafruit 16-channel PWM/Servo HAT for the Raspberry Pi. https://learn.adafruit.com/adafruit-16- ... i/overview
By using the short script it works perfectly but when I add it to my existing script I get an ZeroDivisionError: float division by zero error. When I run the original short scipt again, which worked well before, I also get the ZeroDivisionError.
I tried to find a solution but unfortunately without result.

Could you alter my small script so that when I press a button (will be one of three) the LED lights up, a sound is played and the servo runs(still need to figure out the exact rotation).

The provided script by Adafruit which works well except when run after getting the error:

Code: Select all

import time
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)

kit.servo[0].angle = 180
time.sleep(1)
kit.servo[0].angle = 0



The shortened script which works well to light up a led and play sound on the press of a button:

Code: Select all

import time
from gpiozero import Button, LEDBoard
import RPi.GPIO as GPIO
import pygame
pygame.init()

#button + LED + sound setup
button_1 = Button(2)
led = LEDBoard(6,8,19,20)
sound1 = pygame.mixer.Sound("samples/Bender-03.wav")


while True:
    if button_1.is_pressed:
        led.value = (1,1,0,0) 
        sound1.play()
        time.sleep(2)
        led.value = (0,0,0,0)[/quote]


The script which gets the error:
[quote]from time import sleep
from gpiozero import Button, LEDBoard
import RPi.GPIO as GPIO
from adafruit_servokit import ServoKit
import pygame
pygame.init()

kit = ServoKit(channels=16)
#button + LED + sound setup
button_1 = Button(2)
led = LEDBoard(6,8,19,20)
sound1 = pygame.mixer.Sound("samples/Bender-03.wav")

while True:
    if button_1.is_pressed:
        sound1.play()
        led.value = (1,1,0,0)
        kit.servo[0].angle = 180.0
        time.sleep(1)
        kit.servo[0].angle = 0.0    
        led.value = (0,0,0,0)

The error

Code: Select all

Traceback (most recent call last):
  File "/home/pi/Desktop/testfolder/adafruit_CircuitPython_ServoKit.py", line 6, in <module>
    kit.servo[0].angle = 180
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_servokit.py", line 150, in __getitem__
    servo = adafruit_motor.servo.Servo(self.kit._pca.channels[servo_channel])
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_motor/servo.py", line 98, in __init__
    super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_motor/servo.py", line 45, in __init__
    self.set_pulse_width_range(min_pulse, max_pulse)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_motor/servo.py", line 49, in set_pulse_width_range
    self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xffff)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_pca9685.py", line 71, in frequency
    return self._pca.frequency
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_pca9685.py", line 145, in frequency
    return self.reference_clock_speed / 4096 / self.prescale_reg
ZeroDivisionError: float division by zero
Thank you in advance!

ghp
Posts: 1391
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Servo on Adafruit 16-channel PWM/Servo HAT ZeroDivisionError: float division by zero

Wed Apr 17, 2019 8:33 pm

Some code analysis: There is a property 'frequency' in the underlying PCA9685 class. When frequency is set, the value prescale_reg is set.

Looks as if this mechanism is not working. Do you run it with python 2 or python3 ? If python2, then try python3.

Return to “Python”