pinMode() & digitalWrite() & digitalRead()
You can use pinMode() to make inputs and outputs on any of digital pins #0 thru #4
digitalWrite also works well, and you can also use it with pinMode(INPUT) to activate the internal pull-up resistor on a pin
For example, to set up digital #0 as an input, with an internal pullup, and then check if it is being pulled to ground via a button or switch and turn on the red LED when it is pressed:
/* Button Turns on an LED when a switch connected from #0 to ground is pressed This example code is in the public domain. To upload to your Gemma or Trinket: 1) Select the proper board from the Tools->Board Menu 2) Select USBtinyISP from the Tools->Programmer 3) Plug in the Gemma/Trinket, make sure you see the green LED lit 4) For windows, install the USBtiny drivers 5) Press the button on the Gemma/Trinket - verify you see the red LED pulse. This means it is ready to receive data 6) Click the upload button above within 10 seconds */ #define SWITCH 0 #define LED 1 // the setup routine runs once when you press reset: void setup() { // initialize the LED pin as an output. pinMode(LED, OUTPUT); // initialize the SWITCH pin as an input with a pullup pinMode(SWITCH, INPUT_PULLUP); } // the loop routine runs over and over again forever: void loop() { if (! digitalRead(SWITCH)) { // if the button is pressed digitalWrite(LED, HIGH); // light up the LED } else { digitalWrite(LED, LOW); // otherwise, turn it off } }
analogRead()
You can read an analog voltage from digital #2 (called Analog 1), digital #3 (called Analog 3) and digital #4 (called Analog 2)
For example, to read an analog voltage on pin #2, you would call analogRead(1) to read an analog voltage on pin #4 call analogRead(2)
This is a bit confusing because the analog pins are numbered differently than the digital pins!
analogWrite()
There are a few PWM outputs on the Trinket, you can call analogWrite() on digital #0, #1 and #4.
For example, to pulse the built-in LED slowly, upload this code:
/* Pulse Pulses the internal LED to demonstrate the analogWrite function This example code is in the public domain. To upload to your Gemma or Trinket: 1) Select the proper board from the Tools->Board Menu 2) Select USBtinyISP from the Tools->Programmer 3) Plug in the Gemma/Trinket, make sure you see the green LED lit 4) For windows, install the USBtiny drivers 5) Press the button on the Gemma/Trinket - verify you see the red LED pulse. This means it is ready to receive data 6) Click the upload button above within 10 seconds */ int led = 1; // pulse 'digital' pin 1 - AKA the built in red LED // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { for (int i=0; i<256; i++) { analogWrite(led, i); // PWM the LED from 0 to 255 (max) delay(5); } for (int i=255; i>=0; i--) { analogWrite(led, i); // PWM the LED from 255 (max) to 0 delay(5); } }
Make sure you're using the latest Trinket IDE so you can access pin #4's PWM capabilities. If you aren't using the latest IDE you need to manually add functions like the following to init and write analog values to pin #4. However if you have the latest IDE it includes fixes to make pin #4 usable with Arduino's analogWrite function!
void PWM4_init() { // Set up PWM on Trinket GPIO #4 (PB4, pin 3) using Timer 1 TCCR1 = _BV (CS10); // no prescaler GTCCR = _BV (COM1B1) | _BV (PWM1B); // clear OC1B on compare OCR1B = 127; // duty cycle initialize to 50% OCR1C = 255; // frequency } // Function to allow analogWrite on Trinket GPIO #4 void analogWrite4(uint8_t duty_value) { OCR1B = duty_value; // duty may be 0 to 255 (0 to 100%) }
Wire (i2c)
You can use I2C with the Trinket! If you have our board manager package v1.6.4 or later (thats the version of the trinket support package, not IDE) then Wire will work on Attiny85
On the Trinket boards, pin #0 is SDA (I2C data), pin #2 is SCK (I2C clock).
More...
We also know the following libraries work:
- Adafruit NeoPixel - control up to ~100 Neopixels via a Trinket!
- SoftwareSerial - the built in SoftSerial library can (at least) transmit data on any digital pin.
- More as we do more testing and verification!
Text editor powered by tinymce.