How to use servo and pwm motor libraries

I want to use servo motors and control the speed of DC motors on the Jetson Nano.

from adafruit_servokit import ServoKit # Servo library that works with Jetson
import Jetson.GPIO as GPIO			       # Part of PWM DC motor control 
GPIO.setmode(GPIO.BOARD)               # Error here

Error Description:

Traceback (most recent call last):
  File "brew.py", line 4, in <module>
    GPIO.setmode(GPIO.BOARD)
  File "/usr/lib/python3/dist-packages/Jetson/GPIO/gpio.py", line 317, in setmode
    raise ValueError("A different mode has already been set!")
ValueError: A different mode has already been set!

If there are any libraries that are simple to set up like ServoKit for the PCA9685 board, any easy fixes to have my current code run, or even a way to not use GPIO.setmode please let me know.

hello carlos144,

could you please confirm the pin configuration, this error indicate that there’s different mode has been set.

I’m still learning all the lingo.
Is this what you are asking for?


I have my PWM output on pin33.

hello carlos144,

it’s GPIO13/GPIO3_PE.06 by default a GPIO pin, so, had you already configure this pin as PWM?
in addition, are you able to follow the sample in Topic 178959 for testing your pin?

I configured pin 33 as pwm and I haven’t touched any other pins as far as I remember.

As for the Topic you mentioned, I did indeed get it working just today.

When I attempted to run that code along with the adafruit library it would give me that error I mentioned above.

It’s likely the AdaFruit library sets the GPIO mode already to whatever they want it to be.
Which means that you can’t use that library, and set the mode to something else.
Given that it’s open source, I imagine you can open up the code and read through it to figure out what they’re doing, if you want to attempt to change it.

2 Likes

I ran down that rabbit hole. Afafruit sets it in tegra SOC mode in a file called pin.py.

I’ve spent three days trying to make an l298n motor driver plus an Ada fruit 9826 16 channel pwm driver board work.

Pure. Hell.

I think it comes down to frequencies and once adafruit grabs the board you’re screwed because it will set the frequencies at about 50 and then the motor driver freaks out or doesn’t work.

Really really frustrated with this. All I want is a motor driver and some servos it doesn’t seem like it should be this hard.

1 Like

Yea I totally agree, at least I wasn’t the only person with this problem. Did you manage to eventually make it work or no?

I was actually just about to sit down and give it about the second to last try…

So far and I’m sure I will miss some things I have tried this…

I set up the 16-channel pwm card via i2c and I got all the servos to run I wanted.

I connected a pwm to the motor driver and freaked out everything. I got a soft reply on adafruit message board that you can’t run them off the same board because of frequencies. Servos like 50 or 60 and motors like a thousand or something like that… so I fiddled around with the frequencies and found I could get intermittent working around 400 and pulled my hair out trying to make that work for a while unsuccessfully.

Next I decided to use the Jetson pwm pins and ran directly into the problem you found. If you really study the error you can track down the file and they set it In tegra SOC mode which is what the hell? Really?

Ok…So now you have to figure out the tegra names for those pins and it’s some obscure reference somewhere I managed to find great. So I called the pins and adfruit has grabbed not just the i2c but the whole gpio and now we’re back to frequency…

Currently I can make 16 servos tap dance. I’ve actually connected a Bluetooth controller and I can spin my wheels forward or backward. But I can’t do both.

Afafruit suggested two pwm boards, you can chain them and address them but the servokit library is angles and the motor kit library doesn’t work on that board.

Seriously someone above my pay grade needs to look at this #@&$@#.

1 Like

Okay, so I was doing some searching and found this.
I haven’t tried it yet but it looks like to bypass the one frequency cap you could use two of these boards…
It will look a little funky but it should work out in the end I’d hope so.

Biggest problem is the libraries won’t do both. I decided to chunk the whole adafruit thing and just use a pie zero to control the motor with the l298n and the servos with inherent software pwm. Downside is you have to deal with jitters. Upside is it works.

So turns out I managed a very hacky way around this. I used two PCA 9685 boards to hit both the high and low frequencies at the same time since you can only use one per board.
Here is my set up:

This will likely be a temporary measure until I find a more elegant way around this library problem. Please let me know if yall have any questions or possible solutions.

1 Like