Once you have Arduino installed and set up and you can upload simple blink sketches, you can move on to using each element of the MagTag board.
Using the Red LED
It's always good to blink the LED when you want to verify if something is happening on your board. The LED is on IO #13, but we recommend you use the LED_BUILTIN
macro and you can use this simple sketch example to blink the LED:
void setup() { // initialize built in LED pin as an output. pinMode(LED_BUILTIN, OUTPUT); // initialize USB serial converter so we have a port created Serial.begin(); } // the loop function runs over and over again forever void loop() { digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Reading the Buttons
There are four buttons on the front of the MagTag - they're connected to digital pins IO 11, 12, 14, and 15. (Note we skip 13 since thats the built in Red LED). However, we recommend you use the constants BUTTON_A
, BUTTON_B
, BUTTON_C
, BUTTON_D
.
// SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries // // SPDX-License-Identifier: MIT void setup() { Serial.begin(115200); pinMode(BUTTON_A, INPUT_PULLUP); pinMode(BUTTON_B, INPUT_PULLUP); pinMode(BUTTON_C, INPUT_PULLUP); pinMode(BUTTON_D, INPUT_PULLUP); } void loop() { if (! digitalRead(BUTTON_A)) { Serial.println("Button A pressed"); } if (! digitalRead(BUTTON_B)) { Serial.println("Button B pressed"); } if (! digitalRead(BUTTON_C)) { Serial.println("Button C pressed"); } if (! digitalRead(BUTTON_D)) { Serial.println("Button D pressed"); } // small debugging delay delay(10); }
Open the serial console and press buttons to see the serial output printed!
Using On-board Speaker
A small buzzer on the back of the MagTag can be used to create tones!
Note that, by default, the speaker amplifer is disabled so you have to enable it like so:
// set speaker enable pin to output pinMode(SPEAKER_SHUTDOWN, OUTPUT); // and immediately disable it to save power digitalWrite(SPEAKER_SHUTDOWN, LOW);
You can then turn it back on with
digitalWrite(SPEAKER_SHUTDOWN, HIGH);
when you're ready to play tones or short audio clips
Using On-Board NeoPixels
There are 4 NeoPixels on pin IO #1 (we recommend using the macro PIN_NEOPIXEL
), they also have a power enable pin on NEOPIXEL_POWER
you will need to set it to be an OUTPUT and LOW before writing data to the NeoPixels. If you turn the power pin off (by setting it HIGH) you will need to re-send your NeoPixel data after re-enabling power because the pixels will forget their setting when they lose power.
Start by installing NeoPixel library support for Arduino.
Here's an example sketch that builds on the button press example to turn the LEDs different colors when you press the buttons.
Note we fill the NeoPixels after we turn on the power and then turn them off when no buttons are pressed.
// SPDX-FileCopyrightText: 2020 Limor Fried for Adafruit Industries // // SPDX-License-Identifier: MIT #include <Adafruit_NeoPixel.h> Adafruit_NeoPixel pixels = Adafruit_NeoPixel(4, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800); void setup() { //Initialize serial Serial.begin(115200); pinMode(BUTTON_A, INPUT_PULLUP); pinMode(BUTTON_B, INPUT_PULLUP); pinMode(BUTTON_C, INPUT_PULLUP); pinMode(BUTTON_D, INPUT_PULLUP); // Neopixel power pinMode(NEOPIXEL_POWER, OUTPUT); digitalWrite(NEOPIXEL_POWER, LOW); // on pixels.begin(); pixels.setBrightness(50); pixels.fill(0xFF00FF); pixels.show(); // Initialize all pixels to 'off' } void loop() { if (! digitalRead(BUTTON_A)) { Serial.println("Button A pressed"); digitalWrite(NEOPIXEL_POWER, LOW); // on pixels.fill(0xFF0000); pixels.show(); } else if (! digitalRead(BUTTON_B)) { Serial.println("Button B pressed"); digitalWrite(NEOPIXEL_POWER, LOW); // on pixels.fill(0x00FF00); pixels.show(); } else if (! digitalRead(BUTTON_C)) { Serial.println("Button C pressed"); digitalWrite(NEOPIXEL_POWER, LOW); // on pixels.fill(0x0000FF); pixels.show(); } else if (! digitalRead(BUTTON_D)) { Serial.println("Button D pressed"); digitalWrite(NEOPIXEL_POWER, LOW); // on pixels.fill(0xFF00FF); pixels.show(); } else { // No buttons pressed! turn em off digitalWrite(NEOPIXEL_POWER, HIGH); } }
Using On-board Accelerometer
There's a pre-soldered accelerometer that you can use to detect orientation or motion.
Start by installing the Arduino LIS3DH library
You can test the LIS3DH by loading the included acceldemo in the Arduino library
Before you upload, go down to find this section:
if (! lis.begin(0x18)) { // change this to 0x19 for alternative i2c address Serial.println("Couldnt start"); while (1) yield(); }
And change lis.begin(0x18)
to lis.begin(0x19)
Now you can upload, reset, and check the serial port for acceleration data!
Using the E-Ink Display
You've been so patient, it's time to draw to the display!
Install the Adafruit EPD / ThinkInk library and helper libraries as shown here, and then return once installed.
Open the ThinkInk_gray4 example
Change the pin definitions near the top to:
#define EPD_DC 7 #define EPD_CS 8 #define EPD_BUSY -1 #define SRAM_CS -1 #define EPD_RESET 6
Make sure this line near the top is uncommented, for the ThinkInk_290_Grayscale_T5
type of display (there are a lot of different displays!)
// 2.9" Grayscale Featherwing, MagTag or Breakout: ThinkInk_290_Grayscale4_T5 display(EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);
You can now upload the example to your MagTag to see it display various graphics and text in monochrome and grayscale.
For more information on E-Ink displays, check out our detailed guide
For more information on how to display graphics, and text, check out the Adafruit GFX guide
Text editor powered by tinymce.