Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

servo with Metro Mini not behaving as expected
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

servo with Metro Mini not behaving as expected

by TJay89 on Thu Mar 19, 2020 2:32 am

Hi there,

I am using the Adafruit Metro Mini 328 (PRODUCT ID: 2590), and I'm trying to control a Micro Servo (PRODUCT ID: 4326).
My code is basically the sweep example, see below. Its connected with just the 3 lead to ground, 5v and 3.

For some reason the system is behaving very inconsistent. Sometimes with works fine, but sometimes the program just freezes in random places in the script. Usually pretty quickly after start up or the first time myservo.write(); is called. Without the servo connected the code always loads fine and seems to run well (based on serial output and leds).

Is it possible that the servo is drawing too much power which causes the controller to freeze? If so, is there a way to prevent surges (clearly sometimes it does work, so I'm wondering if there are weird surges in power use at times), or should I power the servo externally? I thought this controller would be fine running just one servo.

If this combination should not misbehave like it does here, I've probably broken my metro mini. But I don't know how I can check....
Any suggestions are welcome :)


Code: Select all | TOGGLE FULL SIZE
#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position
int led = 13;
int servopin = 3;

void setup() {
  myservo.attach(servopin);  // attaches the servo on pin 9 to the servo object
    Serial.begin(9600);
  while (!Serial) {
     ;
  }
  Serial.println("Start setup");
  myservo.write(90); 
  Serial.println("90");
  delay(3000);
  Serial.println("End setup");
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.println(pos);
    delay(30);                       // waits 15ms for the servo to reach the position
  }
  digitalWrite(led, HIGH);
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    Serial.println(pos);
    delay(30);                       // waits 15ms for the servo to reach the position
  }
  digitalWrite(led, LOW);
}

TJay89
 
Posts: 4
Joined: Wed Mar 18, 2020 1:44 am

Re: servo with Metro Mini not behaving as expected

by adafruit_support_bill on Thu Mar 19, 2020 5:25 am

Servos do create a lot of electrical noise and current spikes which can cause processors to malfunction. You can try adding a capacitor between +5v and GND to smooth some of that out. Something in the 100uF to 220uF range is a good place to start.
https://www.adafruit.com/?q=electrolytic

adafruit_support_bill
 
Posts: 79035
Joined: Sat Feb 07, 2009 10:11 am

Re: servo with Metro Mini not behaving as expected

by TJay89 on Fri Mar 20, 2020 1:57 am

You can try adding a capacitor between +5v and GND to smooth some of that out. Something in the 100uF to 220uF range is a good place to start.

That worked! Servo is now behaving as expected.
Really appreciate the quick help. Thanks :)

TJay89
 
Posts: 4
Joined: Wed Mar 18, 2020 1:44 am

Re: servo with Metro Mini not behaving as expected

by adafruit_support_bill on Fri Mar 20, 2020 5:27 am

Good to hear. Thanks for the update.

adafruit_support_bill
 
Posts: 79035
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.