# Trinket (& Gemma) Servo Control

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/010/897/medium800/trinket_IMG_2161.jpg?1378862149)

Info: 

The Adafruit Trinket's small size makes it ideal for lightweight or small projects including robotics. This project demonstrates the use of a standard hobby servo with the Trinket.   
  
The standard Arduino IDE servo library will not work with 8 bit AVR microcontrollers like the ATtiny85 on the Trinket and Gemma due to differences in available timer hardware. Fortunately the&nbsp;[Adafruit\_SoftServo library](https://github.com/adafruit/Adafruit_SoftServo "https://github.com/adafruit/Adafruit\_SoftServo")&nbsp;works well on any available pin (a hardware PWM (pulse width modulated) pin is not required). The library is not ideal, in that servos must be refreshed periodically. A true hardware library would be best if one could be coded, although it would be limited to pins capable of hardware PWM (GPIO #1 and #4).  
  
If you turn the potentiometer, the servo will rotate from zero to 180 degrees. The circuit can be expanded into a number of useful projects.  
  
Parts used:

- **Adafruit [Gemma M0](https://www.adafruit.com/product/3501), [Trinket M0](https://www.adafruit.com/product/3500),&nbsp;**[**Trinket Mini**](http://www.adafruit.com/category/261) or [**Gemma v2**](http://www.adafruit.com/products/1222)&nbsp;microcontroller board (if Trinket, either the [3.3V](http://www.adafruit.com/products/1500) or [5V](http://www.adafruit.com/products/1501) type works). We recommend using the&nbsp;**[Trinket M0](https://www.adafruit.com/product/3500).**
- USB cable for power and reprogramming

- [Standard 5 volt hobby servo](http://www.adafruit.com/index.php?main_page=adasearch&q=servo "Link: http://www.adafruit.com/index.php?main\_page=adasearch&q=servo")(several are available in the Adafruit shop)
- [Potentiometer](http://www.adafruit.com/products/356) (variable resistor), anything from 1K Ω to 10 KΩ, value not critical
- Breadboard, jumper wires/hookup wire
- Power supply and connector - if you want to have it connected to a battery pack or computer-independant

# Trinket (& Gemma) Servo Control

## Wiring

Wiring up the servo and trimpot is easy. You can use EITHER a 5V or 3V Trinket (or Gemma!)  
  
The fastest way to get started is to have the servo powered by **USB+** which is the 5V line from the USB port. This lets you get up to 500mA without going through the onboard voltage regulator. Then ground to **GND**. The control line of the servo goes to **#0**.   
  
The outer edges of the trimpot go to **GND** and **5V** or **3V** (whichever is available on the Trinket) and the middle pin goes to **#2**  
  
Connect the Trinket to the computer USB for power and programming!

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/010/947/medium800/trinketservo.gif?1448054910)

Alternatively, you can power the Trinket from an external 4.5-6VDC battery pack as shown below. Instead of **USB+** use **BAT+** for Trinket/servo power.

![](https://cdn-learn.adafruit.com/assets/assets/000/010/931/medium800/trinket_Wiring.jpg?1378927718)

You may solder the included header pins onto the Trinket to aid in attaching the board to a breadboard. The Trinket connects to power and ground (via the BAT pin) as well as the red and black (or red and brown) on the servo. The outer legs of the potentiometer also connect to power and ground (one to each, it is not polarity sensitive). The center leg of the potentiometer is connected to Trinket GPIO #2. The signal wire (yellow or orange) on the servo connects to Trinket Pin #0.  
  
Normal or extra-long header pins can be placed on the breadboard to help you connect the servo to the board.   
  
It is suggested you use an external wall or battery supply and not power the servo via the regulator. Servos can draw up to 500mA and the Trinket regulator can only source 150 milliamps (USB power generally 500 milliamps). The female DC adapter is helpful in connecting power supplies with barrel connectors to breadboards.

# Trinket (& Gemma) Servo Control

## Arduino Code

# Trinket M0 & Gemma M0
**The Trinket / Gemma M0 can use the default Servo library. The&nbsp;[Arduino Code for 'Knob](../../../../adafruit-arduino-lesson-14-servo-motors/arduino-code-for-knob)' is a good place to start.**

The following pins on each controller can be used for driving servos with the default Servo Library:

- Trinket M0 - Use PWM pins D0, D2, D3, D4 (pin D1 cannot be used)
- Gemma M0 - Use PWM pins D0, D2 (pin D1 cannot be used)

# Trinket & Gemma with ATtiny85

To control servos with the tiny microcontroller on the Trinket, we'll need a Servo library. The default Arduino Servo library is really only good for Uno/Leonardo/Due and similar beefy processors that can drive servos 'standalone'. Sadly, the Attiny85 can't quite do that as it does not have 16bit timers.   
  
So instead we'll use a simpler servo library. Luckily, we wrote one that's perfect! Download the Adafruit\_SoftServo library from [https://github.com/adafruit/Adafruit\_SoftServo](https://github.com/adafruit/Adafruit_SoftServo) by clicking the button below

[Download Adafruit_SoftServo library](https://github.com/adafruit/Adafruit_SoftServo/archive/master.zip)
Install the library into the Arduino libraries directory. See [All About Installing Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use) for a guide.  
  
Please ensure your Arduino IDE is augmented to support Trinket per the [Introducing Trinket Guide](http://learn.adafruit.com/introducing-trinket).  
  
The code below may be copied-and-pasted into a new project window in the Arduino IDE.

Info: 

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_Servo_Control/Trinket_Gemma_Servo_Control.ino

From the **Tools→Board** menu, select **Adafruit Trinket 8 MHz** (or **Gemma** ). Connect the USB cable between the computer and Trinket, press the reset button on the board, then quickly click the upload button (right arrow icon) in the Arduino IDE.  
  
If you get an error message (or a huge list of them), it’s usually one of the following:

- Is the Arduino IDE properly configured for Trinket use? Try compiling and uploading a simple sketch (like the Blink example, set for pin #1).
- Is the Adafruit\_SoftServo library properly installed? It must be correctly named and in the right location (the Arduino libraries folder - see [All About Installing Arduino Libraries](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use "Link: http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use") for a guide).
- If the code compiles but does not upload, press the reset button and try the upload again.

The code compiles to 1678 bytes of 5310 maximum.  
  
Now you can try twisting the potentiometer to watch the servo spin!&nbsp;

Info: 

http://youtu.be/nj7RgiMCM4c

Check this video for what you will see!

# Trinket (& Gemma) Servo Control

## CircuitPython Code

![](https://cdn-learn.adafruit.com/assets/assets/000/053/352/medium800/trinket_gemma-cp.jpg?1524952780)

 **Trinket M0&nbsp;** boards can&nbsp;run&nbsp; **CircuitPython** &nbsp;— a different approach to programming compared to Arduino sketches. In fact,&nbsp; **CircuitPython comes&nbsp;factory pre-loaded on Trinket M0**. If you’ve overwritten it with an Arduino sketch, or just want to learn the basics of setting up and using CircuitPython, this is explained in the&nbsp;[**Adafruit** &nbsp;](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwiK-__azsnXAhWFsVQKHeLhDgcQFgg8MAA&url=https%3A%2F%2Flearn.adafruit.com%2Fadafruit-trinket-m0-circuitpython-arduino%2Foverview&usg=AOvVaw1KR3kAPHYx-DXtGZjUQX60)**[Trinket M0 guide](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwiK-__azsnXAhWFsVQKHeLhDgcQFgg8MAA&url=https%3A%2F%2Flearn.adafruit.com%2Fadafruit-trinket-m0-circuitpython-arduino%2Foverview&usg=AOvVaw1KR3kAPHYx-DXtGZjUQX60).**

Info: 

Below is CircuitPython code that works&nbsp;similarly (though not exactly the same) as the Arduino sketch shown on a prior page. To use this, plug the Trinket M0 into USB…it should show up on your computer as a small&nbsp; **flash drive** …then edit the file “ **code.py** ” with your text editor of choice. Select and copy the code below and paste it into that file,&nbsp; **entirely replacing its contents** &nbsp;(don’t mix it in with lingering bits of old code). When you save the file, the code should&nbsp; **start running almost immediately** &nbsp;(if not, see notes at the bottom of this page).

**If Trinket M0 doesn’t show up as a&nbsp;drive, follow the Trinket M0 guide link above to prepare the board for CircuitPython.**

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Gemma/Trinket_Gemma_Servo_Control/code.py

This code requires an additional library be installed:

1. adafruit\_motor

If you’ve just reloaded the board with CircuitPython, create the “lib” directory and then download the[Adafruit CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases). You can copy 'adafruit\_motor' folder into the lib directory.

```
$ mkdir /Volumes/CIRCUITPY/lib 
$ cp -pr adafruit_motor /Volumes/CIRCUITPY/lib
```

[Adafruit CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases)
# Trinket (& Gemma) Servo Control

## Review and Going Further

 **How It Works** ![](https://cdn-learn.adafruit.com/assets/assets/000/010/912/medium800/trinket_servo.jpg?1378915399)

The potentiometer creates a voltage divider, providing a voltage from zero to five volts depending on how you turn the shaft. The voltage is read by the analog input on the Trinket. The Trinket calculates an angle from zero to 180 degrees in proportion to the voltage on the potentiometer. The Adafruit\_SoftServo library sends a pulse width modulated signal to the Trinket Pin 0, which is interpreted by the servo as a specific angle to move to depending on the pulse width.  
  
**The Adafruit\_SoftServo Library**  
  
You may define multiple Adafruit\_SoftServo objects and control them on different pins. In theory, all five pins should be capable or servo use, **but if using #3 and #4 remove the servo(s) while USB uploading!**  
  
The trickiest part of using this library is the constant software refresh to keep signals going to the servos. We take advantage of the Arduino IDE's built in timer (commonly known as **millis()**) and piggyback on top of it to create the 50-times-a-second update a servo requires.  
  
 As previously stated, it would be preferable to have an AVR 8 bit hardware timer based library. One on the web, **Servo8Bit** , is billed as ATtiny85 compatible. &nbsp;  
  
**Going Further**  
  
Two servos can create an x-y axis controller or a two wheeled robot. This would still leave 3 pins for other functions.


## Featured Products

### Adafruit Trinket M0 - for use with CircuitPython & Arduino IDE

[Adafruit Trinket M0 - for use with CircuitPython & Arduino IDE](https://www.adafruit.com/product/3500)
The&nbsp;Adafruit Trinket M0 may be small, but do not be fooled by its size! It's a tiny microcontroller board, built around the Atmel ATSAMD21, a little chip with _a lot_ of power. We wanted to design a microcontroller board that was small enough to fit into any project, and low...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3500)
[Related Guides to the Product](https://learn.adafruit.com/products/3500/guides)
### Adafruit GEMMA M0 - Miniature wearable electronic platform

[Adafruit GEMMA M0 - Miniature wearable electronic platform](https://www.adafruit.com/product/3501)
The **Adafruit Gemma M0** is a super small microcontroller board, with just enough built-in to create many simple projects. It may look small and cute: round, about the size of a quarter, with friendly alligator-clip sew pads. But do not be fooled! The Gemma M0 is incredibly...

In Stock
[Buy Now](https://www.adafruit.com/product/3501)
[Related Guides to the Product](https://learn.adafruit.com/products/3501/guides)
### Adafruit Trinket - Mini Microcontroller - 5V Logic

[Adafruit Trinket - Mini Microcontroller - 5V Logic](https://www.adafruit.com/product/1501)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1501)
[Related Guides to the Product](https://learn.adafruit.com/products/1501/guides)
### Adafruit Trinket - Mini Microcontroller - 3.3V Logic

[Adafruit Trinket - Mini Microcontroller - 3.3V Logic](https://www.adafruit.com/product/1500)
 **Deprecation Warning: The Trinket bit-bang USB technique it uses doesn't work as well as it did in 2014, many modern computers won't work well. So while we still carry the Trinket so that people can maintain some older projects, we no longer recommend it.** <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/1500)
[Related Guides to the Product](https://learn.adafruit.com/products/1500/guides)
### Standard servo - TowerPro SG-5010

[Standard servo - TowerPro SG-5010](https://www.adafruit.com/product/155)
This high-torque standard servo can rotate approximately 180 degrees (90 in each direction). You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without building a motor controller with feedback & gearbox. Comes with 3...

In Stock
[Buy Now](https://www.adafruit.com/product/155)
[Related Guides to the Product](https://learn.adafruit.com/products/155/guides)
### Micro servo

[Micro servo](https://www.adafruit.com/product/169)
Tiny little servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds you're used to but _smaller_. You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without...

Out of Stock
[Buy Now](https://www.adafruit.com/product/169)
[Related Guides to the Product](https://learn.adafruit.com/products/169/guides)
### Breadboard trim potentiometer

[Breadboard trim potentiometer](https://www.adafruit.com/product/356)
These are our favorite trim pots, perfect for breadboarding and prototyping. They have a long grippy adjustment knob and with 0.1" spacing, they plug into breadboards or perfboards with ease.

This is the same pot that comes with our character LCDs and tutorial...

In Stock
[Buy Now](https://www.adafruit.com/product/356)
[Related Guides to the Product](https://learn.adafruit.com/products/356/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)

## Related Guides

- [Adafruit Trinket M0](https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino.md)
- [CircuitPython Powered AT Hand-Raiser](https://learn.adafruit.com/at-hand-raiser.md)
- [Magical Mistletoe](https://learn.adafruit.com/magical-mistletoe.md)
- [Using Piezo Buzzers with CircuitPython & Arduino](https://learn.adafruit.com/using-piezo-buzzers-with-circuitpython-arduino.md)
- [Using Servos With CircuitPython and Arduino](https://learn.adafruit.com/using-servos-with-circuitpython.md)
- [Trinket / Gemma IR Control](https://learn.adafruit.com/trinket-gemma-ir-remote-control.md)
- [NeoPixel Basketball Hoop](https://learn.adafruit.com/neopixel-mini-basketball-hoop.md)
- [CircuitPython with Jupyter Notebooks](https://learn.adafruit.com/circuitpython-with-jupyter-notebooks.md)
- [Trinket / Gemma Blinky Eyes](https://learn.adafruit.com/trinket-gemma-blinky-eyes.md)
- [Trinket React Counter](https://learn.adafruit.com/trinket-react-counter.md)
- [NeoPixel 60 Ring Wall Clock](https://learn.adafruit.com/neopixel-60-ring-clock.md)
- [Adafruit Gemma M0](https://learn.adafruit.com/adafruit-gemma-m0.md)
- [Trinket Classic USB Foot Switch](https://learn.adafruit.com/usb-foot-switch.md)
- [Free-Wired 3x3x3 NeoPixel Cube](https://learn.adafruit.com/free-wire-3x3x3-neopixel-cube.md)
- [NeoPixel GoPro Lens Light](https://learn.adafruit.com/neopixel-gopro-lens-light.md)
- [LED candles: simple, easy, cheap](https://learn.adafruit.com/led-candles-simple-easy-cheap.md)
