Moving servos with arduino, pca9685 and a nunchuk

Hi all!

As all you know, we have many adafruit libraries allocated at biicode, including the one to handle the PCA9685, capable of moving up to 16 servos. Also You can find the nunchuck library, that allows to use nunchucky. That’s all the software you need to move some servos using a joystick.


Let’s begin!.

These are the main ingredients you need to achieve successfully this recipe:

Now you have all you need, take a look at the sketch below:

servos and nunchuck sketch


You can follow the connections from the picture. We have used 2 servos at ports 0 and 4 for clarity.

Where is the code I need?

You can also reuse, branch, extend or just take a look at our code below (from recluising/servos_nunchuk) by using bii open: bii open recluising/servos_nunchuk

You can also reuse from the necessary libraries to code your own sketch with bii init project, in this case the libraries reused are:

Pay special attention to the #includes linking to the libraries that control the drivers we need. You just need to write the #include, and biicode will configure everything for you (using bii find).

At set-up we set port 9600 to get the serial output. We will print values captured by nunchuk so we can know how the nunchuk is working and what values it’s registering. If you test the code, you should see something like this:

serial port output

serial port output

We are moving two servos with the buttons and pads provided by the nunchuk joystick. We decided to use the logical pad to move the servos depending on axis values (x,y).

So, when we move the pad trough the x axis, one servo is moved and  if the y axis is the one we’re moving through, we move the other servo!

Just execute bii arduino: upload  to load the code into your arduino and this is the way it works:

Now it’s your turn

This example is not cool enough, We’re sure you can do it better, so… here are some ideas, if you test them, we’ll be happy to know your feedback and your experience using biicode:

  • Try something with more than 2 servos,  you can use up to 16!
  • Change the way you move the servos, maybe switching servo with nunchuk buttons.
  • Use the nunchuck sensors (maybe the accelerometer) to move the servos depending on its values.

Happy coding!

Stay tuned

Related Posts