The location of the Servo connections on Crickit are similar on all three versions of the board: Circuit Playground Express, Feather, and micro:bit.
Test Servos
Lets start by controlling some servos. You'll want at least one servo to plug in and test out the servo code. Visit our recommended servo page to check that you have a servo that works. Once you do, plug in a servo into SERVO #1 spot, making sure the yellow or white wire is next to the 1 text label.
This example will show rotating one servo from 0 to 180 degrees with a stop at 90 degrees.
#include "Adafruit_Crickit.h" #include "seesaw_servo.h" Adafruit_Crickit crickit; seesaw_Servo myservo(&crickit); // create servo object to control a servo void setup() { Serial.begin(115200); if(!crickit.begin()){ Serial.println("ERROR!"); while(1) delay(1); } else Serial.println("Crickit started"); myservo.attach(CRICKIT_SERVO1); // attaches the servo to CRICKIT_SERVO1 pin } void loop() { myservo.write(0); delay(1000); myservo.write(90); delay(1000); myservo.write(180); delay(1000); myservo.write(90); delay(1000); }
More Servos!
OK that was fun but you want MORE servos right? You can control up to four! The servos are on the seesaw pins 17 (CIRCKIT_SERVO1), 16 (CIRCKIT_SERVO2), 15 (CIRCKIT_SERVO3), 14 (CIRCKIT_SERVO4)
This example is similar to the 1 servo example, but instead of creating one myservo
object, we'll make an array called servos
that contains 4 servo objects. Then we can assign them using servo[0].write(90);
or iterate through them as we do in the loop. You don't have to do it this way, but its very compact and doesn't take a lot of code lines to create all 4 servos at once!
#include "Adafruit_Crickit.h" #include "seesaw_servo.h" Adafruit_Crickit crickit; #define NUM_SERVOS 4 //create an array of 4 servos with our crickit object seesaw_Servo servos[] = { seesaw_Servo(&crickit), seesaw_Servo(&crickit), seesaw_Servo(&crickit), seesaw_Servo(&crickit) }; //these are the pins they will be attached to int servoPins[] = { CRICKIT_SERVO1, CRICKIT_SERVO2, CRICKIT_SERVO3, CRICKIT_SERVO4 }; void setup() { Serial.begin(115200); //begin the crickit if(!crickit.begin()){ Serial.println("ERROR!"); while(1) delay(1); } else Serial.println("Crickit started"); //attach the servos to their pins for(int i=0; i<NUM_SERVOS; i++) servos[i].attach(servoPins[i]); // attaches the servo to the pin } void loop() { //repeat for all 4 servos for(int i=0; i<NUM_SERVOS; i++){ servos[i].write(0); delay(1000); servos[i].write(90); delay(1000); servos[i].write(180); delay(1000); servos[i].write(90); delay(1000); } }
Min/Max Pulse control
In theory, servos should all use 1ms to 2ms long pulses, at 50 Hz to set the 0 and 180 degree locations. However, not all servos have their full range at those pulse widths. You can easily tweak your code to change the min and max pulse widths, which will let your servo turn more left and right. But don't set the widths too small/large or you can hit the hard stops of the servo which could damage it, so try tweaking the numbers slowly until you get a sense of what the limits are for your motor.
All you need to do is change the
myservo.attach(CRICKIT_SERVO1);
to, say,
myservo.attach(CRICKIT_SERVO1, 750, 2250);
Here we've change the minimum pulse from the default 1000 microseconds to 750, and the default maximum pulse from 2000 microseconds to 2250. Again, each servo differs. Some experimentation may be required!
Continuous Rotation Servos
If you're using continuous servos, you can use the angle assignments and just remember that 0 is rotating one way, 90 is 'stopped' and 180 and rotating the other way.
If your continuous servo doesn't stop once the script is finished you may need to tune the min
and max
pulse timings so that the center makes the servo stop. Or check if the servo has a center-adjustment screw you can tweak.
Disconnecting Servos or Custom Pulses
If you want to 'disconnect' the Servo by sending it 0-length pulses, you can do that by 'reaching in' and adjusting the underlying PWM duty cycle with:
myservo.writeMicroseconds(0);
Likewise you can set the duty cycle to a custom value with
myservo.writeMicroseconds(number);
where number
is the pulse length is microseconds between 0 (off) and 20000 (fully on). For example, setting it to 10000 will be 50% duty cycle, at the 50 Hz update rate
Page last edited January 22, 2025
Text editor powered by tinymce.