I'm trying to drive servo motors using Trinket M0 with PCA9685 shield.
This is the code I'm using in main.py.
import board
import busio
import pca9685
import time
i2c = busio.I2C(board.SCL, board.SDA, frequency = 400000)
while not i2c.try_lock():
pass
scan = i2c.scan()
print("I2C addresses found:", [hex(i) for i in scan])
pcaobj = pca9685.PCA9685(i2c)
while True:
pcaobj.pwm(0, 1, 100)
print ('PWM generating')
time.sleep(500)
When i run this, I get OSError 19 in pca9685 library. Below code is pca9685 python code.
import ustruct
import time
class PCA9685:
def __init__(self, i2c, address=0x40):
self.i2c = i2c
self.address = address
self.reset()
def _write(self, address, value):
print('bytearray vaalue is ',bytearray([value]))
self.i2c.writeto(self.address, bytearray([value]))
def _read(self, address):
return self.i2c.readfrom_into(self.address, address, 0)
def reset(self):
self._write(0x00, 0x00) # Mode1
def freq(self, freq=None):
if freq is None:
return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
prescale = int(25000000.0 / 4096.0 / freq + 0.5)
old_mode = self._read(0x00) # Mode 1
self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
self._write(0xfe, prescale) # Prescale
self._write(0x00, old_mode) # Mode 1
time.sleep_us(5)
self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on
def pwm(self, index, on=None, off=None):
if on is None or off is None:
data = self.i2c.readfrom_into(self.address, 0x06 + 4 * index, 4)
return ustruct.unpack('<HH', data)
data = ustruct.pack('<HH', on, off)
self.i2c.writeto(0x06 + 4 * index, data)
def duty(self, index, value=None, invert=False):
if value is None:
pwm = self.pwm(index)
if pwm == (0, 4096):
value = 0
elif pwm == (4096, 0):
value = 4095
value = pwm[1]
if invert:
value = 4095 - value
return value
if not 0 <= value <= 4095:
raise ValueError("Out of range")
if invert:
value = 4095 - value
if value == 0:
self.pwm(index, 0, 4096)
elif value == 4095:
self.pwm(index, 4096, 0)
else:
self.pwm(index, 0, value)
I'm running this on Windows 7. Please help.
OSError 19 with PCA9685 interfaced with Trinket M0
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- shruthikr
- Posts: 4
- Joined: Wed Jan 24, 2018 7:01 am
OSError 19 with PCA9685 interfaced with Trinket M0
- Attachments
-
- Screenshot of error
- OSerror.JPG (37.91 KiB) Viewed 373 times
- tannewt
- Posts: 3315
- Joined: Thu Oct 06, 2016 8:48 pm
Re: OSError 19 with PCA9685 interfaced with Trinket M0
Where did you get the library? It doesn't look like the newest code from here: https://github.com/adafruit/Adafruit_Ci ... on_PCA9685
I recommend downloading the bundle to get the latest version. Instructions are here: https://learn.adafruit.com/micropython- ... cuitpython
We have a Motor library as well that has a Servo class to make working with Servos easier as well.
I recommend downloading the bundle to get the latest version. Instructions are here: https://learn.adafruit.com/micropython- ... cuitpython
We have a Motor library as well that has a Servo class to make working with Servos easier as well.
- shruthikr
- Posts: 4
- Joined: Wed Jan 24, 2018 7:01 am
Re: OSError 19 with PCA9685 interfaced with Trinket M0
It's working now. Thanks a lot. I was using the old pca9685 library.
Please be positive and constructive with your questions and comments.