# CircuitPython Rotary Trinkey Brightness Crank

## Overview

https://youtu.be/WhHm7yaTjzg

This device will help encourage you to spend less time with social media or tech devices. It will turn the brightness of your device down as long as you aren't cranking the rotary encoder arm. You must continue cranking in order to raise and keep the brightness level up.

This is a good project to show how you can use CircuitPython and HID to interact with mobile devices without having to write an app!

![](https://cdn-learn.adafruit.com/assets/assets/000/103/191/medium800/3d_printing_Untitled.jpg?1624913519)

## Parts
Featured
### Adafruit Rotary Trinkey - USB NeoPixel Rotary Encoder

[Adafruit Rotary Trinkey - USB NeoPixel Rotary Encoder](https://www.adafruit.com/product/4964)
It's half USB Key, half Adafruit Trinket, half rotary encoder_..._it's **Rotary Trinkey** , the circuit board with a Trinket M0 heart, a NeoPixel glow, and a rotary encoder body. [We were inspired by this...](https://twitter.com/todbot/status/1365107469345648643)

In Stock
[Buy Now](https://www.adafruit.com/product/4964)
[Related Guides to the Product](https://learn.adafruit.com/products/4964/guides)
![Video of a woman turning the rotary  on Rotary Trinkey. The LED changes rainbow colors.](https://cdn-shop.adafruit.com/product-videos/640x480/4964-06.jpg)

Featured
### Rotary Encoder + Extras

[Rotary Encoder + Extras](https://www.adafruit.com/product/377)
This rotary encoder is the best of the best, it's a high-quality 24-pulse encoder, with detents and a nice feel. It is panel mountable for placement in a box, or you can plug it into a breadboard (just cut/bend the two mechanical side tabs.) We also include a nice soft-touch knob with an...

In Stock
[Buy Now](https://www.adafruit.com/product/377)
[Related Guides to the Product](https://learn.adafruit.com/products/377/guides)
![Rotary Encoder with rubbery knob](https://cdn-shop.adafruit.com/640x480/377-02.jpg)

Or

Featured
### USB OTG Host Cable - MicroB OTG male to A female

[USB OTG Host Cable - MicroB OTG male to A female](https://www.adafruit.com/product/1099)
This cable looks like a USB micro cable but it isn't! Instead of a USB A Plug, it has a USB A Socket on the end. This cable is designed for use with OTG (On the Go) host devices (like the Arduino Due or many tablets) so you can connect a mouse, keyboard, or any other USB device. You'll...

In Stock
[Buy Now](https://www.adafruit.com/product/1099)
[Related Guides to the Product](https://learn.adafruit.com/products/1099/guides)
![USB OTG Host Cable with Micro B OTG male to A female in black](https://cdn-shop.adafruit.com/640x480/1099-00.jpg)

# CircuitPython Rotary Trinkey Brightness Crank

## 3D Printing

![](https://cdn-learn.adafruit.com/assets/assets/000/103/113/medium800/3d_printing_printed_parts.jpg?1624826496)

## Crank Arm
The rotary encoder crank arm comes from the&nbsp;[USB HID Crank Controller](https://learn.adafruit.com/usb-hid-crank-controller "USB HID Crank Controller")&nbsp;guide.

![3d_printing_crank.jpg](https://cdn-learn.adafruit.com/assets/assets/000/103/114/medium640/3d_printing_crank.jpg?1624828429)

[Download Crank Arm](https://cdn.thingiverse.com/assets/78/86/31/f2/22/hinged-crank-dual.stl)
## Box
The box has a circle cut out on one side for the rotary encoder, and a rectangular cutout on an adjacent side for the USB cable to be plugged in. It was printed with supports enabled due to the overhang on the USB cable cutout.

![3d_printing_vox.jpg](https://cdn-learn.adafruit.com/assets/assets/000/103/115/medium640/3d_printing_vox.jpg?1624828562)

[Download Box](https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/main/Rotary_Trinkey_Brightness_Crank/3D_Model/rotary_trinkey_crank_box.stl)
## Lid
The lid is friction fit to the top of the box with the tabs in each corner. It's slightly rectangular so if it doesn't seem to fit try turning 90 degrees.

![3d_printing_lid.jpg](https://cdn-learn.adafruit.com/assets/assets/000/103/116/medium640/3d_printing_lid.jpg?1624828793)

[Download Lid](https://github.com/adafruit/Adafruit_Learning_System_Guides/raw/main/Rotary_Trinkey_Brightness_Crank/3D_Model/rotary_trinkey_crank_lid.stl)
# CircuitPython Rotary Trinkey Brightness Crank

## Assembly

## Attach Rotary Trinkey
Fit the rotary encoder through the hole in the side of the box and then tighten it on with the encoder's nut. The USB plug edge should face the cutout on the other side of the box.

![3d_printing_encoder_fit.jpg](https://cdn-learn.adafruit.com/assets/assets/000/103/117/medium640/3d_printing_encoder_fit.jpg?1624829182)

![3d_printing_encoder_mounted.jpg](https://cdn-learn.adafruit.com/assets/assets/000/103/118/medium640/3d_printing_encoder_mounted.jpg?1624829324)

## Lid and Crank Arm
Push the lid onto the top. It only fits one way; if it's not fitting, try turning it 90 degrees.

Push the crank arm onto the encoder.

![3d_printing_finished.jpg](https://cdn-learn.adafruit.com/assets/assets/000/103/119/medium640/3d_printing_finished.jpg?1624829461)

# CircuitPython Rotary Trinkey Brightness Crank

## CircuitPython Setup

Download the project files with the button below. Unzip and copy/paste the **code.py** to your **CIRCUITPY** drive. The only libraries required by this project are included in the firmware so you don't need to copy them to the drive.

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

# CircuitPython Rotary Trinkey Brightness Crank

## Code Walk-Through

## Brightness Consumer Codes

To change the brightness up and down we can send HID media consumer control codes for brightness up and down. The `adafruit_hid` library makes sending these easy.

## Main Logic

Every few seconds, `ACTION_INTERVAL`, the program will check how many values the rotary encoder has changed by.

- If it is less than `STAY_EVEN_CHANGE_THRESHOLD`, we send the brightness down code.
- If it is above `STAY_EVEN_CHANGE_THRESHOLD`, it will remain steady not changing the brightness.
- If it's over `INCREASE_CHANGE_THRESHOLD`, we will send the brightness up code.

Different devices will have different brightness levels. You can change the threshold variables or `ACTION_INTERVAL` to suit your device or to make it easier or harder to keep the brightness up.

## Alternate Code - Pausing

In the version below, pressing the rotary encoder button will pause the device so that it won't change the brightness until resumed by pressing the button again.

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


## Guide Products

### Adafruit Rotary Trinkey - USB NeoPixel Rotary Encoder

[Adafruit Rotary Trinkey - USB NeoPixel Rotary Encoder](https://www.adafruit.com/product/4964)
It's half USB Key, half Adafruit Trinket, half rotary encoder_..._it's **Rotary Trinkey** , the circuit board with a Trinket M0 heart, a NeoPixel glow, and a rotary encoder body. [We were inspired by this...](https://twitter.com/todbot/status/1365107469345648643)

In Stock
[Buy Now](https://www.adafruit.com/product/4964)
[Related Guides to the Product](https://learn.adafruit.com/products/4964/guides)
### Rotary Encoder + Extras

[Rotary Encoder + Extras](https://www.adafruit.com/product/377)
This rotary encoder is the best of the best, it's a high-quality 24-pulse encoder, with detents and a nice feel. It is panel mountable for placement in a box, or you can plug it into a breadboard (just cut/bend the two mechanical side tabs.) We also include a nice soft-touch knob with an...

In Stock
[Buy Now](https://www.adafruit.com/product/377)
[Related Guides to the Product](https://learn.adafruit.com/products/377/guides)
### iOS Lightning to USB OTG Cable

[iOS Lightning to USB OTG Cable](https://www.adafruit.com/product/3940)
Your iOS phone or tablet may not have a USB port on the bottom but that doesn't mean you can't use it to connect USB devices. Secretly known as a 'Camera Connector' or 'MIDI Connector' cable, this doohicky plugs into the bottom of your iOs device and gives you a USB A...

In Stock
[Buy Now](https://www.adafruit.com/product/3940)
[Related Guides to the Product](https://learn.adafruit.com/products/3940/guides)
### USB OTG Host Cable - MicroB OTG male to A female

[USB OTG Host Cable - MicroB OTG male to A female](https://www.adafruit.com/product/1099)
This cable looks like a USB micro cable but it isn't! Instead of a USB A Plug, it has a USB A Socket on the end. This cable is designed for use with OTG (On the Go) host devices (like the Arduino Due or many tablets) so you can connect a mouse, keyboard, or any other USB device. You'll...

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

## Related Guides

- [Adafruit Rotary Trinkey](https://learn.adafruit.com/adafruit-rotary-trinkey.md)
- [LED Rocket Lamp](https://learn.adafruit.com/led-rocket-lamp.md)
- [Face Mask Efficacy Testing with SCIENCE!](https://learn.adafruit.com/raspberry-pi-camera-vision-face-mask-efficacy-testing.md)
- [Retroreflective Greenscreen Light Ring QT Py Controller ](https://learn.adafruit.com/retroreflective-green-screen-light-ring-controller.md)
- [Edit CircuitPython Code on iOS with Runestone](https://learn.adafruit.com/editing-circuitpython-on-ios-with-runestone.md)
- [Go Fishing with Rotary Encoders](https://learn.adafruit.com/gone-fishing-game.md)
- [Using the Android CircuitPython Editor](https://learn.adafruit.com/using-the-android-circuitpython-editor.md)
- [Feather Waveform Generator in CircuitPython](https://learn.adafruit.com/waveform-generator.md)
- [Watchmen's Sister Night NeoPixel Goggles](https://learn.adafruit.com/watchmen-sister-night-circuitpython-neopixel-goggles.md)
- [Grand Central USB MIDI Controller in CircuitPython](https://learn.adafruit.com/grand-central-usb-midi-controller-in-circuitpython.md)
- [Trellis M4 Expressive MIDI Controller](https://learn.adafruit.com/trellis-m4-midi-keypad-and-tilt-controller.md)
- [Servo Boss](https://learn.adafruit.com/servo-boss.md)
- [Digital Circuits 6: An EPROM Emulator](https://learn.adafruit.com/digital-circuits-6-eprom-emulator.md)
- [PyGamer - Gaming Handheld Crank](https://learn.adafruit.com/gaming-handheld-crank.md)
- [A NeoPixel Pomodoro Timer](https://learn.adafruit.com/a-neopixel-pomodoro-timer.md)
- [Wireless NeoPixel Controller](https://learn.adafruit.com/neotrellis-neopixel-controller.md)
