Hello everyone. I have a problem with programming to control the server.
I have a Pi Pico microcontroller and I bought an Adafruit 16-Channel PCA9685 for it.And that’s where the trouble began. I don’t know how to start programming.I have the libraries installed.I would like some advice or a simple script as I am new to the topic.
Hi Dariusz,
I’d give this library a go: GitHub - jposada202020/MicroPython_PCA9685: MicroPython Driver for the PCA9685 PWM control IC. Its commonly used to control servos, leds and motors.
There are some examples in the repo as well!
Thank You.Unfortunately it failed. It can’t find the micropython module pca9685.
no module named micropython_pca9685.
Hi @Dariusz277626, Welcome to the forums!!!
It sounds like the library may not be installed.
Could you let us know what method you used to install the library. Screenshots could be helpful.
Could you also share the code you are trying to use as well.
Hello Aaron. There was a mistake, it’s not about the server, but about the servos. I have a pi pico and I use micropython as the language. I use the Tonny interpreter. As for libraries, I install them via Tonny on the pi pico. So I download the library script, e.g. pca9685, copy and paste to Tonny, then save as, select rasberry pi pico, then enter the name, e.g. pca9685. py and ok, it shows me the script under that name in the saved files.
Adafruit use Circuit Python for their boards, if you switch to that from Micropython on the Pico you will probably get it to work. Circuit Python is an Adafruit creation, they do not provide libraries for Micropython for their products. I have found this when trying to use Adafruit products
Anyway that is my understanding, happy to be corrected.
Cheers
Jim
Hello. I tried the CircuitPython program but it’s the same, maybe the pi pico board is faulty? I don’t know anymore. But I found one script that works after installing from the same page pca9685.py and servo.py. I just don’t know how to expand it. I used to deal with Arduino, but the programming language is too difficult, and it was ok. This is where I got lost with micropython. Are there any other ways to install micropython and libraries? Oh, I forgot, is there a library needed for the ultrasonic sensor to work because it doesn’t work for me.
Hi @Dariusz277626,
As others have mentioned, some code snippets and pictures of your setup would be really helpful here. You have mentioned that the pca9685.py script is present in your files, have you imported it into your main.py? That is the only idea I have without seeing any code.
Highly unlikely that the Pico is faulty, they’re typically pretty reliable.
Regarding the US sensor, depending on which sensor you have, there is likely a library file similar to the pca9685.py script available to use with it.
Hello. I don’t have a file like main.py. I’m just starting out and I don’t know my way around. But one script works for me.
pt., 4 paź 2024, 11:49 AM użytkownik Zach via Core Electronics Forum <notifications@core-electronics.discoursemail.com> napisał:
Hey @Dariusz277626,
I think it would be best to start out with something relatively simple. We’ve got a great guide on our Youtube Channel by @Michael that outlines how to use a standard Ultrasonic sensor.
This guide is great, but it uses some of our PiicoDev hardware to make using the device a little simpler. If you don’t have this hardware, it might be better to use this guide by Tom’s Hardware to get started
Hope this helps!
Hello. Thank you, I downloaded the class to the library and ran the script and it works, or rather worked because after running the script again it says NameError: name 'signaloff isn’t defined .
Or maybe please tell me how to install the libraries correctly, maybe that’s the problem
pt., 4 paź 2024, 12:20 PM użytkownik Zach via Core Electronics Forum <notifications@core-electronics.discoursemail.com> napisał:
Hey @Dariusz277626,
I think using extra hardware is probably a step further than you should start.
If you’re new to this environment, I would highly recommend starting basic. We have a really great guide by one of our engineers @Jaryd detailing, from start to finish, how to program a Raspberry Pi Pico. Once you have completed this guide (it shouldn’t take too long), coming back to the hardware-interfacing side of things will be a lot simpler.
Once you’ve completed this you’ll be in a great position to start using these for your projects.
Good luck!
Thank You.
pt., 4 paź 2024, 1:49 PM użytkownik Zach via Core Electronics Forum <notifications@core-electronics.discoursemail.com> napisał:
Hello. Thank you for the advice but I see that it is fine because if it was installed nothing would work but something does. After connecting adafruit pca9685 I run 1 script.
from pca9685 import PCA9685
from machine import I2C, Pin
from servo import Servos
sda = Pin(0)
scl = Pin(1)
id = 0
i2c = I2C(0, sda=sda, scl=scl)
pca = PCA9685(i2c=i2c)
servo = Servos(i2c=i2c)
servo.position(index=0, degrees=90)
I know how to start 4 servos. Is there any way to improve this script?
Hey @Dariusz277626,
That code looks pretty good! I have a few suggestions to improve it.
from pca9685 import PCA9685
from machine import I2C, Pin
from servo import Servos
sda = Pin(0)
scl = Pin(1)
i2c = I2C(0, sda=sda, scl=scl)
pca = PCA9685(i2c)
servo = Servos(i2c)
servo.position(index=0, degrees=90)
You can see in this rewritten code I have made a few small changes.
Unless the variable ‘id’ is used somewhere else in your project it is unused here and can be removed.
additionally defining the variables ‘pca’ and ‘servo’ with ‘(i2c=i2c)’ is not needed. You can simply do this with ‘(i2c)’
Does this code work for you as expected? If not I am happy to help you troubleshoot any issues that come up.
Hello .Samuel. Thank you for your help. Basically, what I meant was to expand e.g.
import time
from machine import Pin, I2C
from micropython_pca9685 import PCA9685
from micropython_pca9685 import Servo
i2c = I2C(1, sda=Pin(2), scl=Pin(3)) # Correct I2C pins for RP2040
pca = PCA9685(i2c)
servo7 = Servo(pca.channels[7])
pca.frequency = 50
# We sleep in the loops to give the servo time to move into position.
for i in range(180):
servo7.angle = i
time.sleep(0.03)
for i in range(180):
servo7.angle = 180 - i
time.sleep(0.03)
# You can also specify the movement fractionally.
fraction = 0.0
while fraction < 1.0:
servo7.fraction = fraction
fraction += 0.01
time.sleep(0.06)
pca.deinit()
Something like that.
wt., 8 paź 2024, 10:50 AM użytkownik Samuel via Core Electronics Forum <notifications@core-electronics.discoursemail.com> napisał:
Hey @Dariusz277626,
That code you’ve sent looks good! I don’t have much “hands-on” experience with these boards, but I would be surprised if that doesn’t work as expected.
Looks like you’re making great progress learning Python/RPi. If you still have questions about the PCA9685, or the process in general, be sure to let us know!