Skip to content

Servo Controller TinyShield Tutorial

The Servo Controller TinyShield allows you to drive four independently controlled servos from your TinyDuino! This tutorial will help you get started with the Servo Controller TinyShield.

To learn more about the TinyDuino Platform, click here


Description

The Servo Controller TinyShield allows you to drive four independently controlled Servos from your TinyDuino! An external power connection is supplied to connect up to your power source (like a battery). Servos are great for doing precise positioning and motion control.

This TinyShield uses the TinyDuino I2C interface - saving GPIO pins on the TinyDuino processor for other tasks. An easy-to-use Arduino library is provided to control this. Multiple Servo Controller TinyShields can be stacked on the same TinyDuino board (up to four Servo Controller TinyShields). There are resistors that can be changed to set the I2C address.

We use the ATtiny841 to control the servos, so check out the datasheet for more in-depth information

NOTE: Be sure that your power supply is sufficient to operate these servos as well as your logic – batteries are the best. If you are running both the servos and the logic off of one power supply, we recommend avoiding using a switching power supply as the transients can potentially damage items connected to the logic side.

To see what other TinyShields are compatible with this TinySheild, see the TinyShield Compatibility Matrix

Technical Details TinyDuino Power Requirements
  • Voltage: 3.0V - 5.5V
  • Current: 5mA (Logic only)
Pins Used
  • A5/SCL - I2C Serial Clock line
  • A4/SDA - I2C Serial Data line
Dimensions
  • 20mm x 20mm (.787 inches x .787 inches)
  • Max Height (from lower bottom TinyShield Connector to upper top TinyShield Connector): 5.11mm (0.201 inches)
  • Weight: 1.62 grams (.06 ounces)

Notes

  • The Servos are not powered from the main TinyDuino power, there is a separate power connection labelled VM on this board that must be connected to your power supply for the motors.      
  • Be sure that your power supply is sufficient to operate these servos as well as your logic – batteries are the best. If you are running both the servos and the logic off of one power supply, we recommend avoiding using a switching power supply as the transients can potentially damage items connected to the logic side. 

Materials

TinyZero and Servo Controller TinyShield

Hardware:

Software


Hardware Assembly

Stack the USB TinyShield onto the TinyDuino Processor and plug it into your computer with a Micro USB cable. Follow the Software steps below to program your TinyDuino Processor.

An assembled stack of a TinyZero and Servo Controller TinyShield

Once the TinyDuino Processor is programmed, stack the Servo Controller Tinyshield onto the processor, plug in your servos into the 3-pin connectors, and plug in your battery into the 2-pin connector. The servos should begin to sweep back and forth.

Remember that the serial monitor must be opened to activate the servos in this example sketch. If the servos don't move, check to make sure that the processor is switched on, that the battery is charged, and that you have the correct address selected in the sketch (default is 0).


Software Setup

To upload the code, we will be using the Arduino IDE which can be downloaded for free here.

You will also need to download the TinyDuino Motor Library.


The Code

Upload the code below to your TinyDuino Processor: Servo Example Program.

Code
//-------------------------------------------------------------------------------
//  TinyCircuits Servo Controller Basic Example
//  Last Updated 11 July 2018
//  
//  This example code shows basic usage of the ASD2303 servo Controller TinyShield.
//  The library intance is created with the hardware address of the servo controller
//  which can be changed by removed resistors R1-R3, allowing for up to 16 TinyShields.
//
//  The board uses the ATtiny841 microcontroller and has direct register access-
//  so just about any of the internal peripherals could be put to use.
//
//  Written by Ben Rose, TinyCircuits http://tinycircuits.com
//
//-------------------------------------------------------------------------------


#include <Wire.h>
#include <ServoDriver.h>

ServoDriver servo(NO_R_REMOVED);//this value affects the I2C address, which can be changed by
                                //removing resistors R1-R3. Then the corresponding R1_REMOVED,
                                //R2_REMOVED, R1_R2_REMOVED, R1_R4_REMOVED and so on can be set.
                                //Default is NO_R_REMOVED


#if defined (ARDUINO_ARCH_AVR)
#define SerialMonitorInterface Serial
#elif defined(ARDUINO_ARCH_SAMD)
#define SerialMonitorInterface SerialUSB
#endif

void setup(){
  SerialMonitorInterface.begin(9600);
  Wire.begin();
  while(!SerialMonitorInterface)//This will block until the Serial Monitor is opened on TinyScreen+/TinyZero platform!
  pinMode(9, OUTPUT);//Pin 9 is the reset pin for the servo controller TinyShield
  digitalWrite(9, LOW);
  delay(10);
  digitalWrite(9, HIGH);
  delay(100);


  if(servo.begin(20000)){      //Set the period to 20000us or 20ms, correct for driving most servos
    SerialMonitorInterface.println("Motor driver not detected!");
    while(1);
  }
  //The failsafe turns off the PWM output if a command is not sent in a certain amount of time.
  //Failsafe is set in milliseconds- comment or set to 0 to disable
  servo.setFailsafe(1000);
}

void loop()
{
  for (int pos = 1000; pos <= 2000; pos += 30) {    // goes from 0 degrees to 180 degrees

    servo.setServo(1, pos);   // tell servo to go to position in variable 'pos'
    servo.setServo(2, pos);
    servo.setServo(3, pos);
    servo.setServo(4, pos);

    delay(20);                                  // wait 20ms for the servo to reach the position
  }
  for (int pos = 2000; pos >= 1000; pos -= 30) {    // goes from 180 degrees to 0 degrees
    servo.setServo(1, pos);   // tell servo to go to position in variable 'pos'
    servo.setServo(2, pos);
    servo.setServo(3, pos);
    servo.setServo(4, pos);

    delay(20);                                  // wait 20ms for the servo to reach the position
  }
}

After uploading the program, your servo should rotate forwards and then reverse as shown below.

You can change the I2C address of this board by removing some of the resistors. This would be useful if you are using multiple Servo Controller TinyShields, so that they don't have conflicting addresses. The default address for the TinyShield and the sketch is 0, with all four address resistors present.


Contact Us

If you have any questions or feedback, feel free to email us at info@tinycircuits.com or make a post on the forum.

Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it!

Thanks for making with us!


Downloads