Help programming servo

Hello!

I just got my first little bits kit the Arduino coding kit and love it! I am having a bit of trouble understanding how to program the servo and was wondering if any one could help me.

I have the p1 power bit connected to the i6 dimmer, connected to the w6 Arduino bit at a1 and d9 of this bit connected to o11 servo.

When I have the Arduino bit set to “analog” and the servo set to “swing” the servo turns about 126 degrees . It stops when I set the dimmer to 0. However, I can’t see (or time) a difference in the speed of the of the swing when I adjust the dimmer (it only works as an on/off switch). I expected to be able to adjust the speed as I found the following information on the littlebits website “In SWING mode, the servo will move back and forth on its own like a pair of windshield wipers – the input signal controls the speed of the swing.”

When I change the servo to “turn” mode the servo turns about 10 degrees and then just vibrates in place regardless of the dimmer setting.

I didn’t notice any difference when I changed the arduino block from analog to pwm. (I don’t really understand the difference in those two settings, but changing the bargraph bit from analog to pwm made the lights switch from the number of lights lit up to how bright the ligths were, so I thought maybe it would do something here too?)

I am hoping someone can have a look at my code and see if I have made a silly mistake or if I just don’t understand how the bit works, but I also have two additional questions:

  1. Is there a page that explains in simple terms how each of the little bits correspond to the parameters of the Arduino code? I found the Arduino reference page and library which was helpful, but I have been really struggling to see how changing the values in the code are going to affect the function of the servo bit.

  2. Is there a way to stop or pause the littlebits from the arduino IDE? I can’t find a stop or pause button and have to keep turning the power off of the little bits when I want to modify my program.

Oh, I only have a very limited understanding of programming and most of what I have read on this topic is way over my head, so I apologize if some of my terms are nonsense (feel free to point this out so I can learn) and if this was already answered and I either couldn’t find or didn’t understand the answer.

Thank you so much and sorry for all of the questions on my first post! Here is my program:

/*Attempt to program Servo*/

// Information from littlebits website: 
// The servo has 2 modes. In TURN mode, the input from other Bits determines the position of the hub – 
// try using a dimmer to set the angle you want. In SWING mode, the servo will move back and forth on its 
// own like a pair of windshield wipers – the input signal controls the speed of the swing. 
// The servo's range of motion is about 110 degrees.

#include <Servo.h>

/*pin numbers*/
const int dimmer = A1;
const int servoPin = 9;

/*create servo object*/
Servo myservo;

/*variables*/
int dimmerPosition = 0;
int scaledDimmerValue = 0;

/*initializations*/
void setup() {
  
  Serial.begin(9600);
  pinMode(dimmer, INPUT);
  myservo.attach(servoPin);

}


void loop() {

  dimmerPosition = analogRead(dimmer);
  //littlebits.cc says the servo turns about 110 degrees (I also tried using 180 degrees and 126 which is what my servo seems to turn)
  scaledDimmerValue = dimmerPosition/(1023/126);
  //Serial.println(analogRead(dimmer));
  //Serial.println(scaledDimmerValue);
  myservo.write(scaledDimmerValue);
  //delay(15);

}

Hi @wilsonrl
The code you’ve written is for a normal arduino servo situation.
Littlebits uses the analogWrite command for controlling the servo.
As an example, just connect the powered dimmer to the servo input.
You will notice that when turning the dimmer, the servo reacts as it should.
This is because the servo bit has a tiny microcontroller which controls the servo motor.
For more info, also check this out : Servo control with Arduino
Greetings

Thank you so much for the tip! It didn’t even occur to me try to use the bits without including the Arduino bit. Now that I connected the pieces without the Arduino bit, I can see how the servo is suppose to work, which will make trouble shooting significantly easier.

I saw that post you mentioned, but didn’t really understand it. Are you saying that the servo library from Arduino doesn’t work with the littlebits servo and I will need to use the analogWrite() to make my servo work?

Thank you so much!

Wow! I was really over complicating this. The following code worked:

/*Attempt to program Servo*/

// Information from littlebits website: 
// The servo has 2 modes. In TURN mode, the input from other Bits determines the position of the hub – 
// try using a dimmer to set the angle you want. In SWING mode, the servo will move back and forth on its 
// own like a pair of windshield wipers – the input signal controls the speed of the swing. 
// The servo's range of motion is about 110 degrees.

/*pin numbers*/
const int dimmer = A1;
const int servoPin = 9;

/*variables*/
int dimmerPosition = 0;
int scaledDimmerValue = 0;

/*initializations*/
void setup() {
  
  Serial.begin(9600);
  pinMode(dimmer, INPUT);

}


void loop() {

/***************************************************************************************************
 Connect p1 power bit to i6 dimmer to w6 arduino at a1 and then connect o11 servo at d9. 
 Make sure the arduino is set to analog mode (pwm seemed to make the servo irratic).
 
 Use this code with the servo in ***swing mode*** to make it swing back and forth. 
 Adjust the speed with the dimmer. 

 Use this code with the servo in ***turn mode*** to make it move.
 It (usually) stops when you stop turning the dimmer.
 The servo becomes irratic if you turn the dimmer too quickly. 

NOTES: 
  1023 is the max output value for the dimmer 
  255 is the max value for analogWrite()
 **************************************************************************************************/
 
  dimmerPosition = analogRead(dimmer);
  //Serial.println(analogRead(dimmer));
  scaledDimmerValue = dimmerPosition/(1023/255);
  //Serial.println(scaledDimmerValue);
  analogWrite(servoPin, scaledDimmerValue);
  //delay(15);

}
1 Like

Hi @wilsonrl
Indeed, as mentioned in the link of the forum thread, you should use analogWrite in your sketch.
What they tried to solve, is some jitter on the servo when using the arduino.
For that reason, they used a protobit ( = break out module ) to connect a normal servo to the littlebits arduino.
And then they used the servo.h library to control this servo with success.
The conclusion was, when using a combination of a littlebits servo with the litttlebits arduino, some jitter may occur.
I believe that @alexpikkert investigated this also.
Maybe he can highlight some of his experiences.
Greetings

[quote=“wilsonrl, post:3, topic:24847, full:true”]

Are you saying that the servo library from Arduino doesn’t work with the littlebits servo and I will need to use the analogWrite() to make my servo work?[/quote]

Flip the servo bit upside down, so you can see the underside of the circuit board. The largest chip - it’s black and square - is an ATTiny25 AVR microcontroller. It’s similar the Arduino’s chip, but smaller: there’s only 2K of Flash and half a K of RAM. The “tiny25” uses an 8 MHz crystal oscillator, so there are only 3 IO pins available: the servo output, input from the bitSnap, and the mode switch. The analog littleBit’s system sends a voltage to this chip, which in turn, controls the servo motor.

1 Like

Thank you so much for your help!

I thought I had the servo figured out, but it feels like it has a mind of its own. The problem that I can’t understand is that at the start of each loop, the servo does its own little “dance” that, as far as I can understand, I did not tell it to do. I would understand if it had to this little dance one time every time the program started, but doing a dance every loop seems a bit crazy.
I have the power connected to the arduino ( a1) and the arduino (d9) to the servo. Here is the program I ran and then the video of the bonus “dances” that it keeps doing (the dances aren’t always exactly the same).

/*pin numbers*/
  const int servoPin = 9;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
  analogWrite(servoPin, 0);
  delay(500);
  analogWrite(servoPin, 50);
  delay(500);
  analogWrite(servoPin, 100);
  delay(500);
  analogWrite(servoPin, 150);
  delay(500);
  analogWrite(servoPin, 200);
  delay(500);

}

Hi @wilsonrl,

Did you set the little analog/pwm switch on the Arduino for D9 on “analog”?
(And I suppose the servo bit to “turn” instead of “swing”)
If not the servo chip will dance a lot… It needs an analog voltage instead of pwm pulses.
I tested with my servo with a small arm fixed to it and then it does not dance a lot.
When the servo moves all the way from 200 to zero it has to stop suddenly and due to the inertia of the mass of the arm it will shake a little. This effect increases when you use a larger arm. When you let it rest one or two seconds more after you send it to the zero position it also becomes much more stable. :grinning:

Try this:
analogWrite(servoPin, 0);
delay(2500);

thank you @alexpikkert

I do have the arduino set to analog and the servo set to turn mode. Since you mentioned swing mode, I did try running the code in swing mode. This worked as expected without any funny stops to twitch and dance between loops.

I did notice that the arm kind of whiplashes back into place when it returns to zero position. I also noticed in my previous code where I used the dimmer to adjust the servo in turn mode, if I turn the dimmer too quickly, the servo freaks out and I have to wait for it to “calm” back down before the dimmer works again.

I tried changing the delay time as you mentioned, but this didn’t work for me. However, because of the whiplash effect, I tried this code to reduce the momentum when the servo returns to zero position:

void loop() {
  analogWrite(servoPin, 0);
  delay(500);
  analogWrite(servoPin, 50);
  delay(500);
  analogWrite(servoPin, 100);
  delay(500);
  analogWrite(servoPin, 150);
  delay(500);
  analogWrite(servoPin, 200);
  delay(500);
  analogWrite(servoPin, 150);
  delay(500);  
  analogWrite(servoPin, 100);
  delay(500);
  analogWrite(servoPin, 50);
  delay(500);
}

This got rid of the whiplash, but surprisingly, my servo still behaves erratically. In a final attempt to try and figure out if I am losing my mind, I removed the servo and connected my bargraph bit at 9. The bargraph works as expected with this code; no issues whatsoever. It repeats the loop as long as I let it and never goes crazy. This leads me to believe that something is wrong with my servo.

Next, I tried connecting both the servo and the barograph in series at 9 (I tried in both orders: servo then bargraph and bargraph then servo). The order didn’t seem to matter (I guess the bits are like Christmas lights, if one goes bad/out all of the ones in that series go bad/out). In both cases, the bargraph mimics the behavior of the servo. When the servo responds to the program as expected the barograph does as well, but when the servo behaves erratically the bargraph lights up just as you would expect for the way the servo moves or pauses.

Any thoughts?

I have two more kits arriving soon (I hope) and they each have a servo, so I will be able to test if my servo is defective when the new ones arrive.

Thanks!

Hi @wilsonrl,
Your program code is OK.
I think the erratic behaviour of the servo lies in the servo itself.
Putting a weight on the servo arm is something the servo hates I think. I once made a dancing mr. Littlebits (he still lives somewhere in the dungeons of the project pages) and this animal had 4 servos to move 4 legs.
When it stood on its legs it went completely haywire when the servos were activated. I had to lift his body up so the feet were floating above ground and could only simulate dancing.
Can you try how the servo behaves when you fully remove the long arm? I expect that it will move correctly.
Second thought : the erratic vibrations become worse after a certain time. My servos are in service now for a little more than 2 years and I know for sure they behaved OK in the beginning.
I posted a small video on YouTube to demonstrate the effect, it has been discussed earlier on this forum…
See:

Thank you @alexpikkert

I forgot to mention that I did try without the arm, but I couldn’t see a difference in the erratic behavior. I tried again with the bargraph attached because it is a little more difficult to tell how it is moving without the arm and sure enough the bargraph was lighting up randomly, but in sync with the servo.

Too bad about the dancing Mr. Littlebits, that was exactly what I had in mind to try when I collected enough servo bits :cry:

Anyway, I think I am about done with the servo, but for future reference, it looks like the white thing with the three wires in it connected to the actual bit can pop out (although I can’t figure out how). Is it possible to take the current servo out and attach a new better one? And, by attach, I mean just pop in and out (I have seen lots of other fancy things on the forum which is awesome, but I am no where near ready for that).

If the above is possible, I saw servos for sale online with the same colored wires (I am not sure the colors matter, but at least they look the same). Is that little piece that the wires are attached to a universal size? The littlebits piece has some groves on the side that I don’t see on the ones online…does that matter?

My next question is that it seems that most servos I can find have an operational voltage from 4.8 - 7.2 volts. I really don’t know what I am talking about here, but I think I read that the little bits only sends up to 5 volts (even though the battery is 9V?). If so, would that mean that this kind of servo would only work/move with the littlebits between 4.8 and 5 volts? In which case, even if I could attach it, it would be useless?

The other thing that seems interesting on the servo descriptions is the stall torque. Does that information tell you how much weight the servo can “hold” before it can’t move or breaks? So, a larger stall torque number means it is “stronger” and can hold more weight without breaking? The descriptions say x kg-cm. What does the “-cm” mean? Minus centimeters? Centimeters of what?

Finally, is this kind of information (operational voltage and stall torque) available on the littlebits website someplace? I didn’t see it on the product details page for the servo and I think it might help me understand more about how the bit works.

Did you also try the P3 USB power bit?
It can deliver more power, if your P1 power bit is not strong enough it might cause trouble too…
The white thing on the bit is just a removable plug.
Most servos have the same plug and as long as they have three wires and they can operate on 5 volt they will work. Do not replace with a much stronger version, else the bit will be overloaded and might be damaged.

You can remove the mini plug by pulling it out vertically and insert another servo if you want. The sequence of the wires is important, if it does not work just flip it.
4.8 - 7.2 volt means the servo will operate between these values. Littlebits uses 5 volt. There is little info about the servo, I could only find the diagram.
It shows 3= ground, 2=VCC 3= control.

Servos going haywire can be tamed by adding friction, see:

1 Like

Thank you @alexpikkert and @Yuziana

I do have a very difficult time connecting the arduino bit to my computer, but I just keep assuming that I am doing something wrong. It often takes multiple trials and lots of orange error messages before I can connect to the computer even with programs that have worked with no issues in the past, so perhaps the power supply is the problem.

I don’t have the USB power bit yet, but it is coming in the new kit I ordered. In the mean time, I think I will have to pick up a new battery from the store. :grinning:

I often have to close the serial monitor to get the Arduino responding. And if that doesn’t work, switch to another port and back. Sort of resets stuff :wink:

Hi @wilsonrl,
One thing I forgot to mention:
A battery with an Arduino and a servo is not a good combination. Those bits need too much energy for a battery, causing erratic behaviour when this battery becomes depleted…
:joy: When you use an USB power bit with a wall wart it will work I think.

Hi @wilsonrl,

See this info about torque and stall torque:


Most little servos have three wires, GND, VCC and Control.
VCC with a value of 5 Volt is very common.
The plug on the Littlebit has a groove to avoid wrong connection.
Using another servo with a plug without groove works, a wrong connection does not damage the bit (my experience at least).
These servos I own have a little plug without groove and they all work fine with the Littlebits bit o11:;
Modelcraft Y-2009
Turnigy TG9
Tower Pro SG90 micro servo
Parallax Continuous Rotation Servo #900-00008
Note:
The Parallax servo rotates full right when not activated, full left when full activated and stops when activated with 50% VCC (2.5 volt).
In swing mode it rotates full right when not activated and it swings when activated…
I think it is the same design as used by Littebits in the “tethered motor” from the gizmos and gadgets kit.
. :grinning:

@alexpikkert and @Yuziana,

Thank you so much for all of your help! I guess the issue was in fact the battery. My new kits arrived today with the usb power bit and my servo works perfectly with it. There is no funny dancing or twitching; the program and servo works perfectly. :grinning:

1 Like

…OK!
:blush: