Raspberry Pi pico-driven robotic arm is programmed with pca9685 MicroPython

Raspberry Pi pico practice

The first chapter uses pico+pca9685 to drive multiple servos



foreword

Recently, I was playing with the robotic arm to grab the box, and I just bought a pico to practice and learn.
First of all, I would like to thank up for explaining about pico at station B, the evil fat bacteria


1. What is pca9685?

You can go to the boss to learn about the programming of the PCA9685 multi-rudder controller.
PCA9685 is a 12-bit precision 16-channel PWM wave output chip based on IIC bus communication. When the chip was first launched by NXP, it was mainly used for LED switch dimming. , 16-channel 12-bit PWM signal generator, can be used to control steering gear, led, motor and other equipment, i2c communication, saving host resources. I just want to control several servos, but it takes up too much pin resources, so I thought of this artifact.
In fact, the popular point is that generally a servo will occupy a data pin and a positive and negative power pin. If you want to directly connect multiple servos to a development board, it will take up pin resources. With the servo driver board, it solves the problem of power supply. The wiring problem also solves the pin occupation problem, which is very fragrant! !

2. Pico programming

Some basics are needed here, such as pico burning firmware, and the installation and use of Thonny needs to be done by yourself. You can refer to Raspberry Pi official website tutorial Raspberry Pi pico Chinese site

1.main.py

You can download and run according to the sample code first.
Among them, Pin() in i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=10000) indicates GPIO0 and GPIO1
s.position Parameter 1 of (0,0) indicates the number position on the motor drive board to be controlled, and parameter 2 indicates how many degrees to control the steering gear to rotate
to The position of 0 degrees
You can directly enter s.position(0,0) in the terminal to control the servo.

from machine import I2C,Pin
import time
from servo import Servos
i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=10000)
s=Servos(i2c,address=0x40)
s.position(0,0)
s.position(1,0)
s.position(0,90)
s.position(1,90)

#pico-PCA9685

#3v3 OUT--VCC
#GND--GND
#GPIO0--SDA
#GPIO1--SCL


#PCA 9685 接线
#0-15为外接舵机
#外接5V电源
#黄色 数据
#红色 5V


2.servo.py

The code is as follows (example):

import pca9685
import math


class Servos:
    def __init__(self, i2c, address=0x40, freq=50, min_us=600, max_us=2400,
                 degrees=180):
        self.period = 1000000 / freq
        self.min_duty = self._us2duty(min_us)
        self.max_duty = self._us2duty(max_us)
        self.degrees = degrees
        self.freq = freq
        self.pca9685 = pca9685.PCA9685(i2c, address)
        self.pca9685.freq(freq)

    def _us2duty(self, value):
        return int(4095 * value / self.period)

    def position(self, index, degrees=None, radians=None, us=None, duty=None):
        span = self.max_duty - self.min_duty
        if degrees is not None:
            duty = self.min_duty + span * degrees / self.degrees
        elif radians is not None:
            duty = self.min_duty + span * radians / math.radians(self.degrees)
        elif us is not None:
            duty = self._us2duty(us)
        elif duty is not None:
            pass
        else:
            return self.pca9685.duty(index)
        duty = min(self.max_duty, max(self.min_duty, int(duty)))
        self.pca9685.duty(index, duty)

    def release(self, index):
        self.pca9685.duty(index, 0)



Summarize

Guess you like

Origin blog.csdn.net/lzsm_/article/details/126066031