Servo Controller TinyShield Tutorial
The Servo Controller Shield allows you to drive four independently controlled servos from your TinyCircuits processor! Servos are great for doing precise positioning and motion control.
An external power connection is necessary to supply power to the servo motors. Batteries work best.
Up to four Servo Controller Shields can be stacked on the same TinyCircuits processor, for controlling up to 16 total servo motors.
Learn more about the TinyDuino Platform
Technical DetailsTinyDuino Power Requirements
-
- The Servos are not powered from the main TinyDuino power, there is a separate power connection to power the servos.
- 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
Hardware
- Micro USB Cable
- TinyDuino and USB TinyShield OR
- Servo Controller TinyShield
- Servos with JST-SH Connectors
- Battery (Servos need an external power source)
Software
Hardware Assembly
Stack the Servo Shield onto the processor and plug it into your computer with a Micro USB cable. Follow the Software steps below to program your processor.
Once the processor is programmed, stack the Servo Controller Shield 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 ATtiny841 Arduino Library. To install an Arduino library, check out our Library Installation Page.
The Code
Upload the code below to your processor: Servo Example Sketch.
Code
//------------------------------------------------------------------------------- // TinyCircuits Servo Controller Basic Example // Last Updated 24 Feb 2020 // // 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 // //------------------------------------------------------------------------------- #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(); //This will block until the Serial Monitor is opened on TinyScreen+/TinyZero platform! while(!SerialMonitorInterface && millis() < 5000) servo.useResetPin(); //This tells the library to use the reset line attached to Pin 9 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 } for (int pos = 2000; pos >= 1000; pos -= 30) { // goes from 180 degrees to 0 } }
NOTE: If you want to run the servos wirelessly, comment out the line
while(!SerialMonitorInterface);
in the setup() loop to avoid needing to open the Serial Monitor to make them start moving. This line was put in place as a little safety precaution in case you have some type of robot hooked up while testing that you don’t want to “run” off your desk. It is a good idea to keep this line in place while you are testing so that you can choose when the servo motors start turning, by opening the Serial Monitor.
After uploading the program and opening the Serial Monitor, your servo(s) 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 Shield and the sketch is 0, with all four address resistors present.
Downloads
If you have any questions or feedback, feel free to email us or make a post on our forum. Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it.
Thanks for making with us!