How to use an Adafruit 16-Channel PWM HAT with a Raspberry Pi to Control Sixteen Servos

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 มิ.ย. 2024
  • 16 Servos and 1 Raspberry Pi! These single board computers are exceptional and here is the best way to control a whole bunch of 5V servos with them.
    Full Article - core-electronics.com.au/tutor...
    Related Tutorials
    Control One or Two Servo Control With Raspberry Pi - core-electronics.com.au/tutor...
    I2C communication with the Raspberry Pi - core-electronics.com.au/tutor...
    Controlling a Solenoid with Raspberry Pi - core-electronics.com.au/tutor...
    UPS PiJuice HAT With Raspberry Pi 4 Model B - core-electronics.com.au/tutor...
    What is a HAT? - core-electronics.com.au/tutor...
    Facial Recognition with a Raspberry Pi and OpenCV - core-electronics.com.au/tutor...
    This guide will teach you how to control up to 992 5V Servos through 2 GPIO pins on your Raspberry Pi! But more practically, without stacking many multiple HATS (Hardware Attached on Top), this guide will teach exactly how to control Sixteen 5V servos using a single Raspberry Pi single-board computer. This is all possible via the Adafruit Servo HAT which will act as a PWM (Pulse Width Modulation) expander, taking up only 2 GPIO pins and communicating through I2C. The setup created in this guide will be able to send out 16 unique Hardware PWM signals. PWM signals are great for servos (both standard and continuous) and can be used for a myriad of other purposes (like LED light control, motor speed, and even the heat output of a heater!). This guide will show you how to control a single standard servo, a single rotational servo and 16 servos all at once! It will clearly go through the hardware assembly, the terminal commands and the scripts (Python code) to make our system all work together. So instead of being limited by the two pairs of hardware capable PWM GPIO Pins that the Pi 4 has we can instead use this HAT as a PWM expander board. It is worth noting that all GPIO pins on a Raspberry Pi can generate digital PWM signals which are referred to as Soft PWM signals. A PWM Signal produced this way is timed by the linux scheduler which results in a less accurate and less flexible signal than the Hardware PWM signal counterpart. A Soft PWM signal is not great for servos control as they will excessively jitter to and fro instead of holding still and will cause glitches when controlling a LEDs brightness. And one of the best things about this HAT is you can stack multiple on top of you allowing you to feasibility control 992 5 Volt servos all from a single Raspberry Pi using 2 GPIO pins. I asked if we had that many on hand for me to demonstrate to you and since then they have been hiding the barrels of servos from me :P.
    If you have any questions about this content or want to share a project you're working on head over to our maker forum, we are full time makers and here to help - coreelec.io/forum
    Core Electronics is located in the heart of Newcastle, Australia. We're powered by makers, for makers. Drop by if you are looking for:
    Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi (used here): core-electronics.com.au/catal...
    Adafruit 16-Channel PWM / Servo Bonnet for Raspberry Pi (For Pi Zero and Zero 2): core-electronics.com.au/catal...
    Makeblock 9g Micro Servo Pack (used here): core-electronics.com.au/catal...
    FEETECH FS90R micro Continuous Rotation Servo (used here): core-electronics.com.au/catal...
    Raspberry Pi Model B 2GB (used here): core-electronics.com.au/catal...
    Raspberry Pi 3 Model B+: core-electronics.com.au/catal...
    Raspberry Pi 4 Power Supply: core-electronics.com.au/catal...
    5V DC 4A Fixed 2.1mm Plugpack: core-electronics.com.au/catal...
    0:00 Intro
    0:15 What this HAT does
    0:45 16 PWM Signals!
    1:00 What You Need
    1:55 Hardware Assembly
    3:06 Terminal Commands
    4:32 Configure I2C Communication
    4:56 Download Scripts
    5:10 Standard Servo Control
    5:30 First Payoff
    5:36 Basic Servo Control Script Analysis
    6:56 Continuous Servo Control
    7:42 Second Payoff
    7:48 Basic Servo Continuous Control Script Analysis
    8:35 Tip to Tune a Continuous Servos
    9:06 Controlling All The Servos
    9:22 The Big Payoff!
    9:40 Max Servo Control Script Analysis
    10:58 Outro
    The following trademarks are owned by Core Electronics Pty Ltd:
    "Core Electronics" and the Core Electronics logo
    "Makerverse" and the Makerverse logo
    "PiicoDev" and the PiicoDev logo
    "GlowBit" and the GlowBit logo

ความคิดเห็น • 75

  • @whitneydesignlabs8738
    @whitneydesignlabs8738 ปีที่แล้ว +3

    Great tutorial and great code. Thanks for the help. Got this board up and running and testing with a servo in minutes.

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Glad you found our guide helpful. Happy making! 👍

  • @tishinpadilla100
    @tishinpadilla100 2 ปีที่แล้ว +1

    "Very Satisfying." That hat close-up footage is very good.

  • @syedhasanaskaririzvi3659
    @syedhasanaskaririzvi3659 2 ปีที่แล้ว +1

    The final code can be shortened by using loop.
    In setup section above the while loop we can have one for or while loop for variable say i to go from 0-15. Then we can just put one line and instead of number put i.
    Same in while loop we can have 2 loops one for the n where we can use this n as t/n and in that loop we can have a nested loop for value say j to go from 0-15. And now just replace whole with only 3 lines and place j instead of number.
    And there you have it. A reduced code with same functionality.
    Though hat's off to the creators and for well explained tutorial 🔥

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      Cheers for your work mate 🙂! As soon as I have a second to regroup and sharpen up this tutorial I'll dive in and update the code.

  • @GunnerAl9
    @GunnerAl9 4 หลายเดือนก่อน

    I'm confused on how the code you wrote gets from the Pi board into the Adafruit 16 Channel Servo Breakout Board? Does the sketch changes get loaded onto the processing board automatically as you change them, or is that a save command in the program. I have no experience in coding and I have to modify some servo movements for my R2D2 Droid for the halo projector lights. A great video, I learned a lot. Good verbal speed and very clear voice. Thank you.

  • @AnthonySherritt
    @AnthonySherritt 8 หลายเดือนก่อน

    This is terrific! I have my eye on doing animatronics. What would be a good library for sequencing/tweening servo settings?

  • @floridian080
    @floridian080 ปีที่แล้ว +1

    Great tutorial, thank you. Is there a hat with built in potentiometer(s) that could be used to control the servos? Otherwise potentiometer control would require a chip such as mcp3008 and the libraries to work with it.

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Thank you 😊 a Raspberry Pi HAT with a Potentiometer and Servo Control doesn't instantly spring to my mind. Perhaps a Servo Tester would be a product worth checking out.

  • @si9lix6
    @si9lix6 2 ปีที่แล้ว +1

    Thanks for the video, helped me a lot! Just one question: It looks like the hat uses all the GPIO pins of the Raspberry Pi. Does this mean I can‘t do anything else with the Pi like for example connect an IMU?

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      Glad to hear 😊 this Hat uses the GPIO pins mainly as a physical support. It actually only uses 2 GPIO pins for communication. All the other pins are free to be used for other electrical tasks (like an IMU) and can be best accessed if you use a GPIO Stacking Header like the one I have linked here - core-electronics.com.au/gpio-header-for-raspberry-pi-b-extra-long-2x20-female-header.html

  • @shailigupta4086
    @shailigupta4086 ปีที่แล้ว

    May you tell about how to program if we want to run 5 servomotor simultaneously.

  • @robertoamaya6980
    @robertoamaya6980 2 ปีที่แล้ว +1

    Amazing
    I want to write a app python for this.

  • @redpandarules3793
    @redpandarules3793 ปีที่แล้ว +1

    Hello! Thank you for the tutorial! One of the most well explained ones I found!
    I managed to get 16 servos running, but I would like to go even further (Big project)
    If I were to connect a second board in (SLA, SDA) continuation, then the 2 boards would have different addresses. How would this change the code? Is there a way to select the address when initializing the ServoKit function?
    Thank you for your time and the tutorial again!

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Excellent question and thanks for your kind words 😊! These HAT boards all communicate using I2C and you can alter their address by soldering some jumpers found on the top of the board. You can see exactly what I mean here - learn.adafruit.com/adafruit-16-channel-pwm-servo-hat-for-raspberry-pi/stacking-hats
      In scripts, you can create a new object for each HAT with | pwm = PWM(0x40) | and | pwm = PWM(0x60) | Where 0x40 is the default HAT address and 0x60 is a custom one made by soldering that jumper on the top.

    • @redpandarules3793
      @redpandarules3793 ปีที่แล้ว +1

      @@Core-Electronics Thank you very much for your response!

  • @chattarikapollaor2973
    @chattarikapollaor2973 2 ปีที่แล้ว +2

    Hi Tim, I am wondering that instead of using Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi - Mini Kit, can I use another servo driver which is the PCA9685 and go with the same of your code and method/process that you have made to control multiple servos?
    looking forward to your advice ^^
    Thank you

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      That Adafruit 16-Channel I2C HAT with that PCA9685 should be a perfect drop in. The Python code should function exactly the same 😊

  • @kavorka8855
    @kavorka8855 ปีที่แล้ว

    Hi, I didn't see you controlling all at the same time. I mean, simultaneously. Did you try that yourself? Thanks!

  • @manyasterisks
    @manyasterisks 6 หลายเดือนก่อน

    Would it be possible to use this to tune motor RPM to chromatic note frequencies?

  • @abu8422
    @abu8422 10 หลายเดือนก่อน

    Can I also control 16 12V DC motors ??

  • @mike0rr
    @mike0rr 2 ปีที่แล้ว +1

    So does this shield just reduce the amount of wires needed to control several servos using I2C, or does it do more.
    I was mostly wondering if this still uses software to control the servos or if it's hardware. Just overthinking things and looking into getting rid of servo jitter on a project.

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว +1

      Both software and hardware. If you use this HAT you will be able to eliminate servo jitter.

  • @martinled7493
    @martinled7493 ปีที่แล้ว +1

    Hi. This is a very detailed video thank you for that. I'm done with the soldering part and have put the hat on my pi. But do you know if it can run one servo without additional power to the hat? and only use power from the pi? I want a portable setup to control the servo on my RC car :)

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว +1

      Cheers for your kind words 😊 perhaps if you only want to control a single Servo come check out this guide here - core-electronics.com.au/guides/control-servo-raspberry-pi/

    • @martinled7493
      @martinled7493 ปีที่แล้ว +1

      @@Core-Electronics You are very welcome. made it work last night 😊 attached a USB voltage regulator (4.7 V) on the hars GND + 5+ and plug'd it in to the usb ports of the PI and it works great! Do you know where to find python code, to get intruction to W=forward S=backwards A=Left D=rigth? :) ill share a video to you when its done. Your basic script works fine and tilts the front tires to each side, with an 3 sec delay. now i need the ERC to controll the throttle and speed. again thanks for the video! :)

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว +1

      If you want to control your Servo like this hit up this Pan-Tilt HAT guide with Raspberry Pi - core-electronics.com.au/guides/pan-tilt-hat-raspberry-pi/
      I know at first that might seem a bit random, but the mechanisms behind the Pan-Tilt HAT are just servos. The second Python example runs has a process inside it that control the two servo using keyboard inputs 😊 which is right up your alley. With that framework there you will be able to change it for your situation.

    • @martinled7493
      @martinled7493 ปีที่แล้ว +1

      @@Core-Electronics ill take a look at it, thank you so much for the help. a last question. do you know if its possible to control the DC motor through the ERC cotroller with the HAT? an what code would work, to control the speed. You should also make a vido with an Rc car :P Kind regards

    • @martinled7493
      @martinled7493 ปีที่แล้ว

      th-cam.com/video/25CyMKf5dGE/w-d-xo.html

  • @cx3268
    @cx3268 2 ปีที่แล้ว +1

    S0 for example, told to go to 0 position. Mill the motor try to hold it at 0 position if force is presented to rotate it?
    OR does the controller release power to that servo after it gets to its position.
    OR can you tell it to release power to hold position for any one of those servos?

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      The servos will hold their position. You can tell the servos to 'deactivate' once it has reached a certain angle in code,which will then allow you to rotate the servo manually without crunching gears, if you so desire.

  • @Videogame9559
    @Videogame9559 7 หลายเดือนก่อน

    Hi would this work motor control board shim or hat small one

  • @ozcanison
    @ozcanison 2 ปีที่แล้ว +2

    What about moving multiple servos at the same time, instead of in sequence?

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว +1

      You absolutely can move multiple servos at the same time. Just remove any time delays (sleep) and they will all operate simultaneously.

  • @RolandsZeltins
    @RolandsZeltins 2 ปีที่แล้ว +1

    Thank you for awesome, video.
    I couldnt get in my country any Raspberry Pi model except that with keyboard Raspberry Pi 400
    My qesution is, how do I align, correctly pins using GPIO Ribbon Cable for Raspberry Pi Model A+/B+/Pi 2/Pi 3/Pi 4 - (40 pins)?
    Because side what goes in Raspberry Pi 400 is marked and goes in only one way, buy other side I can plug in bouth ways in Adafruit 16-Channel PWM / Servo HAT for Raspberry Pi - Mini Kit
    Need help.

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      Heyya mate,
      Raspberry Pi 400 are sweet (and actually have a higher overclock (2.2GHz) than the other Raspberry Pi boards.
      If you look on the back of the case near the GPIO it actually has labels saying | Pin40 | and | Pin1 |. From this you can look at the normal pin out for a Raspberry Pi 4 like in the video and figure out the orientation. This guide will help too - core-electronics.com.au/guides/raspberry-pi-generations/

  • @alexwu8502
    @alexwu8502 6 หลายเดือนก่อน

    Hi Tim at Core Electronics,
    I am Alex. I currently doing the work at my workplace. I am trying to find ways to use the Raspberry Pi System to make 6 or 8 servomotor running . I have a question as the information listed below:
    1. I know you told me that you used the Adafruit 16 channel PWM Servohat to make more than 2 servomotor running. How many gpio pins do I need to make more than 2 servomotor running with the PWM servohat ?
    2. Is it necessary for me to connect all the pins from the PWM servohat to the Raspberry Pi Model 3 Computer Board?

    • @Core-Electronics
      @Core-Electronics  6 หลายเดือนก่อน

      Hey Alex,
      1. The servo HAT uses I2C, meaning that it only needs those 2 pins regardless of if you are controlling 2 or 16 servos.
      2. You wont need to connect all the pins, but as it is a HAT it is designed to be connected to all the pins. If you need to access some more pins on the Pi, the hat has some pinouts of the Pi's pins on top of it.
      Cheers!

  • @Didanihaaaa
    @Didanihaaaa ปีที่แล้ว

    Good job. I have a question. I want to rotate the servo and once it done I want to clean the pin. I see that is done in GPIO by calling stop method however I am not sure how to stop using adafruit hat. My experience shows that the servos tend to gitter while trying to stop at a constant position. thanks for help.

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว +1

      I created a guide on Controlling Standard Servos with a Raspberry Pi which should help you along - core-electronics.com.au/guides/control-servo-raspberry-pi/
      In that guide I address the jittering and how to minimise it when not using a Servo HAT like in the above video.

  • @Toni73RU
    @Toni73RU 6 หลายเดือนก่อน

    А сколько максимум серво моторов можно подключить?

  • @johnconnor6735
    @johnconnor6735 ปีที่แล้ว +1

    is it possible for a raspberry pi to control a larger motor, like a windshield wiper motor or larger dc motor that can aid in helping a disabled person walk as long we we power it seperatley?

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Absolutely champ come check this product over here, there some different variants worth checking out - core-electronics.com.au/catalog/product/view/sku/POLOLU-5051 -
      You will be setting them up in a very similar workflow as the above video. You'll be able to control some pretty serious motors with those HATs (just check all the spec sheets) and I adore your project idea 😊 would love to see how it comes out.

  • @Videogame9559
    @Videogame9559 ปีที่แล้ว +1

    Hi would do this with controlling it with flysky receiver and audunio or raspberry pi any help please

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Heyya mate, this guide here will give you the run down on controlling hardware attached to a Raspberry Pi using an RC Controller - github.com/samfok/remote_receiver_tutorial
      I reckon that will be a proper help 😊

  • @thedude2897
    @thedude2897 ปีที่แล้ว +1

    I have a ghostbusters trap that someone made. Has a chip in it and servos for the doors. They don't work right. So I can't just swap them out? I'd have to reprogram the new ones?

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Depends on the condition of the servos. If they have three wires coming off them and look functional you may not even need to swap them out. If they are small 9 grams servos like the ones in the above video you could definitely connect it up with a similar Raspberry Pi system. If the servos are larger/higher voltage you should take a look at this guide to see the process to work with them - core-electronics.com.au/guides/control-servo-raspberry-pi/

  • @antitialaman7301
    @antitialaman7301 ปีที่แล้ว +1

    Hello 😊
    I'm stuck, I have my Raspberry Pi Zero, my PCA9685 and my servo, but the servo doesn't move. I think maybe you need extra power, maybe it's not enough with just Pi Zero, I don't know if you can confirm this for me. Because I imported the libraries and used your code and I didn't have any errors.
    Another idea is... The pca9685 was sold to me broken (but it has a red light whren it is comnected).

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว +1

      Could be a number of things, come write up a post at our forum with some pictures of your set up and I'll best be able to help 😊 - forum.core-electronics.com.au/

  • @ahmedsherif6110
    @ahmedsherif6110 ปีที่แล้ว +1

    Is there any way to use PCA9685 with AVR? Cause I've tried hard to write a driver to it, but I faild.

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      Check out this right here, should get you right into the thick of driving servos - github.com/prestonsn/PCA9685-Multi-Channel-Servo-Controller-Driver-for-AVR-ATmega

  • @oskarmozdzen2697
    @oskarmozdzen2697 2 ปีที่แล้ว +1

    Can I control 6 7,4V servo motors with this controller?

    • @Core-Electronics
      @Core-Electronics  2 ปีที่แล้ว

      Works with any servo that can be powered by 5V and take 3.3V logic level signals.

  • @ihmSelbst
    @ihmSelbst ปีที่แล้ว +1

    Can it runs 16 Motors at the same time?

  • @krishnakanthajith2350
    @krishnakanthajith2350 ปีที่แล้ว

    Could you tell me how we can chain two pca9685 using this python library

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      I haven't got a quick guide to show you but this fellow does using An Arduino and two pca9685 - th-cam.com/video/v1f_EDHV390/w-d-xo.html
      If you take his script and see how each device is being referred to in the script (along with how to write a script that can deal with the extra PCA9585) then you should be well on your way. Also always feel free to ask these more complicated questions on our Forum - forum.core-electronics.com.au/latest

  • @urkoolabarria4163
    @urkoolabarria4163 ปีที่แล้ว

    Looks great. I have the material to build something similar synchronizing the sound with the movements. Where could I find a document with all the steps starting from 0? I need to read it slowly as a total beginner Thanks!

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      I love that idea! This video here is a great introduction to controlling servos, when it comes to capturing sound to activate servos I have seen it done very basically with an Arduino Uno - www.instructables.com/Audio-Sensor-Servo-Reactive-Motion/
      Come write up a post outlining your project goals at our Forum and we'll best be able to help 😊 forum.core-electronics.com.au/

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว

      I also found this but its a touch scary - th-cam.com/video/3Nss_2_rwdE/w-d-xo.html

  • @jayeshpatil754
    @jayeshpatil754 หลายเดือนก่อน

    sudo pip3 install isn't working

  • @Shiven34567
    @Shiven34567 ปีที่แล้ว +2

    Would this work with regular DC motors

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว +1

      DC motors are PWM devices, so you would be able to hack together a solution, however I would recommend using something more readily made for the job, like the Adafruit DC Stepper Motor HAT which you can learn more about here - core-electronics.com.au/guides/raspberry-pi/raspberry-pi-dc-stepper-motor-guide/
      Also keep in mind that Continuous Servos work with this Servo HAT and act very similar to DC Motors.

    • @Shiven34567
      @Shiven34567 ปีที่แล้ว +2

      Thanks

    • @Shiven34567
      @Shiven34567 ปีที่แล้ว +1

      How many motors can I control using this and if it's stackable how many would I need to control 8 dc motors

    • @Core-Electronics
      @Core-Electronics  ปีที่แล้ว +1

      Heyya mate, with the Adafruit DC & Stepper Motor HAT you can control 4 DC Motors. This HAT can be stacked up to 32 times letting you control a whopping 128 DC Motors! Everything you need to run one can be learned here 😊 core-electronics.com.au/guides/raspberry-pi/raspberry-pi-dc-stepper-motor-guide/

    • @Shiven34567
      @Shiven34567 ปีที่แล้ว +2

      Thank you so much I will get one.

  • @ZenHulk
    @ZenHulk 5 หลายเดือนก่อน

    all i have is 6 or 7 volt servos and there is no help anywhere on planet earth

    • @lex-ig7qu
      @lex-ig7qu หลายเดือนก่อน

      Have you tried H Bridges?