0
$\begingroup$

I am working on project that requires lots of PWM channels and decided to use PCA9685 and Raspberry Pi 4B as my controller, which I've never used. I installed Ubuntu 22.04LTS in order to use ROS-humble and was wondering how do I set up the PCA9685, I got it working with Arduino Nano but can't seem to figure out on how to set this thing up on the Pi. I tried follow the Adafruit tutorial but I ran into some issues which I am assuming is because it is meant for RaspbianOS. Does anyone have link to a tutorial or advice on how to set this thing up.

This pwm board is pretty common so I was hoping to find someone who went through this exact problem.

Notes: if example code can be provided to test, that would also be great.

Somethings I've tried.

import time
import board
from adafruit_pca9685 import PCA9685
from adafruit_motor import servo

#Create the I2C bus interface.
i2c = board.I2C()

#servo specifications
USMIN = 600
USMAX = 2400
USNEUTREL = 1500
SERVO_FREQ = 60
SERVO_MIN = 150
SERVO_MAX = 600
ACT_RANGE = 180

#Create a simple PCA9685 class instance
pca = PCA9685(i2c)

# Set the PWM frequency to 60hz.
pca.frequency = 60

#Set the PWM duty cycle for channel zero to 50%. duty_cycle is 16 bits to match other PWM objects
#but the PCA9685 will only actually give 12 bits of resolution
pca.channels[0].duty_cycle = 0x7FFF #look more into this

servo0 = servo.Servo(pca.channels[0],ACT_RANGE,USMIN,USMAX)


def test_servos():
    for i in range (180):
        servo0.angle = i
        time.sleep(0.03)
    
    for i in range (180):
        servo0.angle = 180 - i
        time.sleep(0.03)

def main(args = None):
    print("Running 'test_servos' script")
    test_servos()

if __name__ == '__main__':
    main()

ERROR:

Traceback (most recent call last):
  File "/home/mcc/Desktop/servos.py", line 2, in <module>
    import board
  File "/usr/local/lib/python3.10/dist-packages/board.py", line 47, in <module>
    from adafruit_blinka.board.raspberrypi.raspi_4b import *
  File "/usr/local/lib/python3.10/dist-packages/adafruit_blinka/board/raspberrypi/raspi_4b.py", line 6, in <module>
    from adafruit_blinka.microcontroller.bcm2711 import pin
  File "/usr/local/lib/python3.10/dist-packages/adafruit_blinka/microcontroller/bcm2711/pin.py", line 5, in <module>
    from RPi import GPIO
ModuleNotFoundError: No module named 'RPi'

I've pretty sure I tried installing RPi, but I will try again later.

$\endgroup$
1
  • $\begingroup$ always format error messages as code for readability $\endgroup$
    – jsotola
    Commented Jun 11 at 6:04

1 Answer 1

2
$\begingroup$

You have to install the RPi gpio through pip.

sudo apt update
sudo apt upgrade
sudo apt install python3-pip python3-dev
pip3 install --user RPi.GPIO   

The original answer is posted here Link to original answer

$\endgroup$
3
  • $\begingroup$ Will give it try, thanks. $\endgroup$
    – msalaz03
    Commented Jun 11 at 23:13
  • $\begingroup$ Followed your instructions and got this Requirement already satisfied: RPi.GPIO in ./.local/lib/python3.10/site-packages (0.7.1) $\endgroup$
    – msalaz03
    Commented Jun 13 at 23:44
  • 1
    $\begingroup$ sudo apt-get install python3-rpi.gpio this command did for me, thanks. $\endgroup$
    – msalaz03
    Commented Jun 14 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.