Adafruit's "adafruit_servokit" library returns error on setting angle of servo - raspberry-pi

I'd like to control a servo by a given angle.
I am using a RaspberryPi 4 Model B which is running Raspian.
The servos are connected to a Adafruit PCA9685 16-Channel Servo Driver.
The servo driver is connected to the RaspberryPi via i2c.
Python version 3.7.
I used the following tutorial: https://learn.adafruit.com/16-channel-pwm-servo-driver/python-circuitpython
I am able to properly control a LED (just like in the above tutorial) with the setup.
The LED is connected to the servo driver on channel 8, whereas the servo is connected at channel 2.
So here's my code (controlling the LED also included):
import board
import busio
import adafruit_pca9685
from adafruit_servokit import ServoKit
i2c = busio.I2C(board.SCL, board.SDA)
pca = adafruit_pca9685.PCA9685(i2c)
pca.frequency = 60
pca.channels[8].duty_cycle = 0x7FFF
kit = ServoKit(channels=16)
kit.servo[2].angle = 180
And here's the error code I get in return:
Traceback (most recent call last):
File "/home/pi/rover/Main.py", line 12, in <module>
kit.servo[2].angle = 180
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_servokit.py", line 147, in __getitem__
servo = adafruit_motor.servo.Servo(self.kit._pca.channels[servo_channel])
File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 89, in __init__
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 29, in __init__
self.set_pulse_width_range(min_pulse, max_pulse)
File "/usr/local/lib/python3.7/dist-packages/adafruit_motor/servo.py", line 33, in set_pulse_width_range
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 56, in frequency
return self._pca.frequency
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_pca9685.py", line 134, in frequency
return self.reference_clock_speed / 4096 / self.prescale_reg
ZeroDivisionError: float division by zero

I solved the problem myself. Here's what I did:
I was confused by the difference between circuitpython and regular python.
As far as I understand this, circuitpython is a whole programming language with its environment. If that is even possible to install on the RaspberryPi, I am not sure. I checked on circuitpython's official website and it does not seem to be supported, check the download's page.
In case you have circuitpython installed, you can refer to this Github page: https://github.com/adafruit/Adafruit_CircuitPython_PCA9685
Anyway, what I am looking for is Adafruit's library for "regular" python. This can be pulled from here: https://github.com/adafruit/Adafruit_Python_PCA9685
Check out the readme.md for setup instructions.
With this, it worked for me. Controlling servos is now fairly easy.

Related

Bitbanging errors on adafruit feather m0 with circuit python

My understanding is that circuit python will support bitbanging by default, but I might be mistaken...
For this example I am using a DHT11 temp and humidity sensor (I have a adafruit stemma/quic one on the way but shipping in Australia is awfully slow)
import board
import adafruit_dht
# Initialize the DHT11 sensor
dht = adafruit_dht.DHT11()
# Read the temperature and humidity
temperature = dht.temperature
humidity = dht.humidity
# Print the values
print("Temperature: {:.1f} C".format(temperature))
print("Humidity: {:.1f} %".format(humidity))
and the error I am being returned is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "adafruit_dht.py", line 295, in __init__
File "adafruit_dht.py", line 82, in __init__
Exception: Bitbanging is not supported when using CircuitPython.
I have tried using the adafruit_bitbangio library, but I haven't been making much headway with that whatever docs I see are navigating me to either use this, or that is should just work without it.
Any help on this topic would be a huge help!
Thanks.

driving nema14 with circuitpython on a esp32 s2 with A4988

I am trying to drive a stepper motor with circuitpython 7.3.3 on a esp32 s2 with a A4988 stepper motor driver board. I have been fiddling for a while now. Since adafruit does not carry the A4988 boards there is no example code to start from on there pages, so I ended up trying to modify a piece of micropython code from TechToTinker.
https://techtotinker.blogspot.com/2021/05/036-micropython-technotes-stepper-motor.html%5B/url%5D
Unfortunately I can't seem to make the most basics of code to work and get the stepper to move.
import time
import board
from digitalio import DigitalInOut, Direction, Pull
dir_1 = DigitalInOut(board.IO1)
dir_1.direction = Direction.OUTPUT
step_1 = DigitalInOut(board.IO2)
step_1.direction = Direction.OUTPUT
def rotate(angle=0, rotation='cw'):
if rotation == 'cw':
dir_1.value(0)
for i in range(0, 200, 1):
step_1.value(1) # one step
time.sleep(0.5)
step_1.value(0)
time.sleep(0.5)
if rotation == 'ccw':
dir_1.value(1)
for i in range(200-1, -1, -1):
step_1.value(1)
time.sleep(0.5)
step_1.value(0)
time.sleep(0.5)
With the following lines of codes I should be able to test the stepper using the REPL:
rotate(360, 'cw')
rotate(360, 'ccw')
I checked the above code with mu and corrected all the found (style) error's, still when I run it I get the following error.
Traceback (most recent call last): File "<stdin>", line 1, in
<module> NameError: name 'rotate' is not defined.
A google search gave me an ansers like:
It looks like a variable named 'python' is used which is not present at the given scope during execution[/quote],
Unfortunately I can't do much with that, as far as I understand I am not doing that, so if I am, I have no idea how I am doing that, additionally I thought to have defined 'rotate'.
I tried the following to get some use out of the stepper:
I searched the adafruit site for relevant example code.
I searched the adafruit forum for similar issues but found only topics on arduino boards.
I searched the net for similar pieces of ciruit/micropython code to use with the A4988 board.
I finally tried to write, modify code myself, from relevant code from whichI tried to solve all errors.
I googled the final additional error but did not understand what I found.

Problem transferring sensor data with LoRa sx1262 + Raspberry Pi Pico

I am new to the micro-controller game and I ran into some issues trying to send data from one Pi Pico to another one using the SX1262 LoRa module by Waveshare.
Basically what I eventually want to do is make a sensor network and send all the data that I am going to gather from the sensors to a gateway and through there to a server for further development.
The first step is to send a simple string from one Raspberry Pi Pico to the other one, so I can understand the driver a little bit better and eventually make any modifications needed to create my project.
The driver that I found for the SX1262 is this one.
When I try to run the code on my Pico using the Thonny IDE, I get the following errors:
Traceback (most recent call last):
File "< stdin >", line 11, in <module>
File "sx1262.py", line 27, in begin
File "sx126x.py", line 115, in begin
File "sx126x.py", line 240, in reset
File "sx126x.py", line 389, in standby
File "sx126x.py", line 1270, in SPIwriteCommand
File "sx126x.py", line 1287, in SPItransfer
TypeError: object with buffer protocol required
Those errors happen both on the TX and TX (non blocking), which are in the examples folder.
Can someone help me with this and hopefully explain some things to me? Because as long as I get the driver running fine I can continue with my project.
TypeError: object with buffer protocol required means that you are trying to send something that's neither a string nor a byte array. Check your code, and convert your data to a proper type.

Why is this code not working for mcp3008? (from official adafruit site)

I'd like some help to debug the example code provided by adafruit. I have connected a potentiometer and an analog servo motor which when using the python2 script give me values just fine. I took from this site: https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008
Since I have to upgrade to python3 I looked into new tutorials here: https://learn.adafruit.com/mcp3008-spi-adc/python-circuitpython
The problem I have is no matter what pin value I give in the board I always get 0 values. The pin I use for mcp3008 selector is BCM 22:
I've also provided an image of the connection here:
mcp3008 potentiometer raspberry pi zero w
import busio
import digitalio
import board
import adafruit_mcp3xxx.mcp3008 as MCP
from adafruit_mcp3xxx.analog_in import AnalogIn
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs = digitalio.DigitalInOut(board.D5)
mcp = MCP.MCP3008(spi, cs)
What am I doing wrong? Thanks a lot for your time.
It turns out the raspberry pi required restart which surprised me as I suspect it's because I installed the adafruit packages.

Raspberry Pi GPIO stops working after some time

when using the GPIO library on the Raspberry PI and having an example code like this:
while True:
GPIO.setup(21, GPIO.OUT)
pwm = GPIO.PWM(21, 50)
pwm.start(0)
for i in range(0, 101, 2):
pwm.ChangeDutyCycle(i)
time.sleep(0.03)
for i in range(100, -1, -2):
pwm.ChangeDutyCycle(i)
time.sleep(0.03)
pwm.stop()
GPIO.cleanup(21)
time.sleep(1)
The code might stop suddenly after a while. No error, just no changes via pwm are recognized any longer. Anyone got an idea why this is?
This issue has been mentioned here in the old sourceforge repo:
https://sourceforge.net/p/raspberry-gpio-python/tickets/111/
https://sourceforge.net/p/raspberry-gpio-python/tickets/94/
Its because the GPIO library by default creates a new pthread for every call without cleaning up afterwards, but the number of threads might be limited to a number of 250 or so.
I created a fork of this repo (https://github.com/wuestkamp/raspberry-gpio-python) which solves this and contains instructions on how to use this on your PI.