Trinket M0 programming problems

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
CodyThatWilson
 
Posts: 7
Joined: Fri Dec 24, 2021 8:59 pm

Trinket M0 programming problems

Post by CodyThatWilson »

I'm trying to do [this][1] project. However, I was unable to program the adafruit trinket. I then switched to the blink program below, which I don't think is working either.

I can connect my trinket M0 (P3500) to my laptop (USB to microUSB, and it will open up some files. I then click the button twice so that the LED is green with a second red pulsing LED. At this point, it comes up as pIRkey at COM4. I can then hit upload and I get

> 'upload complete.'

At this point the device switches from being green and red pulsing to pink constant and red is off, however it is not blinking, but it is also not running the original program.

I noticed in the documentation that I need to use a specific programmer, but under tools I don't have the option for USBtinyISP, I only have AtmeltICE and JLink. Is that part of the issue?

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

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; // blink '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() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Screenshot 2021-12-24 165749.png
Screenshot 2021-12-24 165749.png (124.35 KiB) Viewed 251 times
[1]: https://learn.adafruit.com/guardian-rob ... w/software

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: Trinket M0 programming problems

Post by danhalbert »

A Trinket M0 is not a "classic Trinket". Follow the instructions here for using Arduino with a Trinket M0:
https://learn.adafruit.com/adafruit-tri ... -ide-setup

You don't need a separate programmer. Just double-click the reset button and you should see a TRINKETBOOT drive. Then select the right port from the Port menu in Arduino. Make sure the board is set to Trinket M0.

User avatar
dbetz
 
Posts: 49
Joined: Thu Jul 10, 2008 7:21 pm

Re: Trinket M0 programming problems

Post by dbetz »

Sorry, this isn't a Trinket M0. It's one of the old ATtiny85-based boards.

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: Trinket M0 programming problems

Post by danhalbert »

Is it 5V or 3.3V?

The older Trinkets have a software-based USB bootloader which doesn't work properly on USB3 ports and in some other situations. You can repair its bootloader: https://learn.adafruit.com/introducing- ... bootloader. Read the rest of that guide too for other help.

User avatar
CodyThatWilson
 
Posts: 7
Joined: Fri Dec 24, 2021 8:59 pm

Re: Trinket M0 programming problems

Post by CodyThatWilson »

danhalbert wrote:A Trinket M0 is not a "classic Trinket". Follow the instructions here for using Arduino with a Trinket M0:
https://learn.adafruit.com/adafruit-tri ... -ide-setup

You don't need a separate programmer. Just double-click the reset button and you should see a TRINKETBOOT drive. Then select the right port from the Port menu in Arduino. Make sure the board is set to Trinket M0.
Totally rad! I followed the walkthrough and I was able to upload and change the blink rate (so I can tell things are working correctly!). I didn't have the SAMD library, which was the issue.

Another question - am I going to run into issues now with the library inclusion on this project? I think I ended up with the wrong trinket (I think I remember buying what was in stock hoping there wasn't a difference).

I'm getting this output for the real project:

C:\Users\user\Documents\Arduino\src\Guardian_code\Guardian_code.ino:7:10: fatal error: avr/power.h: No such file or directory
7 | #include <avr/power.h>
| ^~~~~~~~~~~~~
compilation terminated.
Compilation error: exit status 1}.

The original product code states that it uses the adafruit TiCoServo library - is there an alternative I can use with the M0 if this one doesn't work with my M0?
// Libraries: uses Adafruit_TiCoServo library to manage servo pulses,
// even though NeoPixels are NOT being used here.

Here's the full project files I'd like to run

Code: Select all

// Trinket Servo Monster sketc
// Hardware: Adafruit Trinket (3V or 5V), micro servo, LED + resistor
// Libraries: uses Adafruit_TiCoServo library to manage servo pulses,
// even though NeoPixels are NOT being used here.

#include <Adafruit_TiCoServo.h>
#include <avr/power.h>

// Servo parameters.  Pin MUST be 1 or 4 on a Trinket.  Servo position
// is specified in raw timer/counter ticks (1 tick = 0.128 milliseconds).
// Servo pulse timing is typically 1-2 ms, but can vary slightly among
// servos, so you may need to tweak these limits to match your reality.
#define SERVO_PIN  4 // Pins 1 or 4 are supported on Trinket
#define SERVO_MIN  4 // ~1 ms pulse
#define SERVO_MAX 26 // ~2 ms pulse

#define LED_PIN    0 // "Eye" LED is connected here

Adafruit_TiCoServo servo;

void setup(void) {
#if (F_CPU == 16000000L)
  // 16 MHz Trinket requires setting prescale for correct timing.
  // This MUST be done BEFORE servo.attach()!
  clock_prescale_set(clock_div_1);
#endif
  servo.attach(SERVO_PIN);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);
}

uint32_t lastLookTime = 0; // Time of last head-turn

void loop(void) {

  unsigned long t = millis(); // Current time

  // If more than 1/2 second has passed since last head turn...
  if((t - lastLookTime) > 500) {
    if(random(10) == 0) { // There's a 1-in-10 chance...
      // ...of randomly moving the head in a new direction:
      servo.write(random(SERVO_MIN, SERVO_MAX));
      lastLookTime = t;   // Save the head-turn time for future reference
    }
  }

  // Unrelated to head-turn check,
  if(random(10) == 0) { // there's a 1-in-10 chance...
    // ...of an "eye blink":
    digitalWrite(LED_PIN, LOW);  // The LED turns OFF
    delay(random(50, 250));      // for just a short random moment
    digitalWrite(LED_PIN, HIGH); // then back ON
  }

  delay(100); // Repeat loop() about 10 times/second
}

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: Trinket M0 programming problems

Post by danhalbert »

Can you use the regular Servo library? https://www.arduino.cc/reference/en/libraries/servo/
Or do you need NeoPixels at the same time?

The TiCoServo library is only for AVR (not M0 SAMD21) boards. For an M0 board, see this note: https://learn.adafruit.com/neopixels-an ... 2548662-10, which points to this guide: https://learn.adafruit.com/dma-driven-n ... s/overview.

User avatar
CodyThatWilson
 
Posts: 7
Joined: Fri Dec 24, 2021 8:59 pm

Re: Trinket M0 programming problems

Post by CodyThatWilson »

danhalbert wrote:Can you use the regular Servo library? https://www.arduino.cc/reference/en/libraries/servo/
Or do you need NeoPixels at the same time?

The TiCoServo library is only for AVR (not M0 SAMD21) boards. For an M0 board, see this note: https://learn.adafruit.com/neopixels-an ... 2548662-10, which points to this guide: https://learn.adafruit.com/dma-driven-n ... s/overview.
Good feedback! I got it to compile with these changes, but have I overlooked something that won't work?

Code: Select all

// Trinket Servo Monster sketc
// Hardware: Adafruit Trinket (3V or 5V), micro servo, LED + resistor
// Libraries: uses Adafruit_TiCoServo library to manage servo pulses,
// even though NeoPixels are NOT being used here.

#include <Arduino.h>
#include <Servo.h>
//#include <Adafruit_TiCoServo.h>

// Servo parameters.  Pin MUST be 1 or 4 on a Trinket.  Servo position
// is specified in raw timer/counter ticks (1 tick = 0.128 milliseconds).
// Servo pulse timing is typically 1-2 ms, but can vary slightly among
// servos, so you may need to tweak these limits to match your reality.
#define SERVO_PIN  4 // Pins 1 or 4 are supported on Trinket
#define SERVO_MIN  4 // ~1 ms pulse
#define SERVO_MAX 26 // ~2 ms pulse

#define LED_PIN    0 // "Eye" LED is connected here

Servo servo;

void setup(void) {
#if (F_CPU == 16000000L)
  // 16 MHz Trinket requires setting prescale for correct timing.
  // This MUST be done BEFORE servo.attach()!
  clock_prescale_set(clock_div_1);
#endif
  servo.attach(SERVO_PIN);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, HIGH);
}

uint32_t lastLookTime = 0; // Time of last head-turn

void loop(void) {

  unsigned long t = millis(); // Current time

  // If more than 1/2 second has passed since last head turn...
  if((t - lastLookTime) > 500) {
    if(random(10) == 0) { // There's a 1-in-10 chance...
      // ...of randomly moving the head in a new direction:
      servo.write(random(SERVO_MIN, SERVO_MAX));
      lastLookTime = t;   // Save the head-turn time for future reference
    }
  }

  // Unrelated to head-turn check,
  if(random(10) == 0) { // there's a 1-in-10 chance...
    // ...of an "eye blink":
    digitalWrite(LED_PIN, LOW);  // The LED turns OFF
    delay(random(50, 250));      // for just a short random moment
    digitalWrite(LED_PIN, HIGH); // then back ON
  }

  delay(100); // Repeat loop() about 10 times/second
}

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: Trinket M0 programming problems

Post by danhalbert »

I would say start with a regular Servo example, from, say, https://learn.adafruit.com/using-servos ... on/arduino, or any Arduino examples for Servo. For instance the `F_CPU` check and `clock_prescale_set(clock_div_1);` should not be needed.

User avatar
CodyThatWilson
 
Posts: 7
Joined: Fri Dec 24, 2021 8:59 pm

Re: Trinket M0 programming problems

Post by CodyThatWilson »

Alright will do, I'll walk through a servo example and see if I can get it to work. I wasn't planning on making code changes but that's how these things go :)

Thanks for all the help!

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”