Three Arduino libraries are used to facilitate programming:
- The Arduino IDE Wire library provides I2C communications between the Trinket and the display. The Adafruit_LEDBackpack code loads in the Wire library so we do not need to do it in the main sketch unless we want to use I2C communications for other devices.
- The Adafruit LED Backpack library has routines to talk to the display. This library was updated in 2016 to use the Trinket compatible Wire library.
- The Adafruit GFX library is required to accompany the backpack library. The statement to include this library is done in the LEDBackpack library. Adafruit BusIO must also be installed manually if using an older version of the Arduino IDE (pre-1.8.10), newer versions handle this automatically with the GFX library.
Using the Adafruit libraries take up a fair amount of space but simplifies programming.
Please see the Adafruit tutorial All About Arduino Libraries for information on adding these libraries to your Arduino environment. Install the libraries into your arduinosketch/libraries directory. Restart the IDE to ensure the libraries load.
Cut and paste the following code into your IDE window:
/*************************************************** Adafruit Trinket-based Room Occupancy sensor and display Featuring a Trinket 5V and the 8x8 Bi-color LED Matrix and I2C Backpack For Trinket, this program is within 120 bytes of the maximum code size of 5,310 bytes. Adding significantly more functionality may not be possible. Version 2.0 New Wire Library support for newer IDE Mike Barela for Adafruit Industries ****************************************************/ // The Adafruit_LEDBackpack library will pull in the standard Arduino // Wire library and needs the Adafruit_GFX library to be installed also! #include "Adafruit_LEDBackpack.h" const int PIRpin = 1; // PIR signal pin on Trinket Pin #1 uint8_t pirState = LOW; // Stores state of the PIR sensor Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix(); void setup() { pinMode(PIRpin, INPUT); // Initial state is low matrix.begin(0x70); // pass in the address } static const uint8_t PROGMEM // X and square bitmaps x_bmp[] = { B10000001, B01000010, B00100100, B00011000, B00011000, B00100100, B01000010, B10000001 }, box_bmp[] = { B11111111, B10000001, B10000001, B10000001, B10000001, B10000001, B10000001, B11111111 }; void loop() { int sense = digitalRead(PIRpin); // Read PIR Sensor if(sense == HIGH) { // If high and it was low, sensor tripped if(pirState == LOW) { // and we display a red X matrix.clear(); matrix.drawBitmap(0, 0, x_bmp, 8, 8, LED_RED); matrix.writeDisplay(); pirState = HIGH; } } else { if(pirState == HIGH) { // If low and state was high, sensor set matrix.clear(); // and we sisplay a green box matrix.drawBitmap(0, 0, box_bmp, 8, 8, LED_GREEN); matrix.writeDisplay(); pirState = LOW; } } }
Ensure you set the Arduino IDE as follows:
Tools -> Board to: Adafruit Trinket 8MHz
Tools -> Programmer to: USBtinyISP
Press the verify checkmark icon to verify your code compiles. If it does not, check the code and the libraries. Note there are only a few bytes left in the code space so adding functionality might take up too much space. This includes adding the delay function.
Take your Trinket out of the circuit and plug it into the USB cable.
Then press the Trinket reset button on the board then quickly click the upload icon (right arrow) in the Arduino IDE. This will upload the code to the Trinket.
Considerations
The code space is very tight using all the libraries. More code space could be obtained by using low level I2C calls. This was done in the Space Invaders pendant tutorial. This would require additional work to get the correct calls coded and since this project works great as is, we didn't spend the extra hacking-time!Page last edited March 08, 2024
Text editor powered by tinymce.