# Servo 8-bit Arduino

Discussion in 'The Projects Forum' started by Dualfire, Dec 5, 2012.

1. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
Hi All,
I have been using the 16-bit resolution of the Arduino (pin 9 or 10) to control a servo.
I came across something, and now if I am using a 8-bit resolution,
I was wondering something:
Since the pulse is sent to the servo every 20ms, from 1 to 2ms, there is one step right? (as in, it moves up or down once). So I calculate this:
256bit/20ms = x/1. solving for x, we get 12.8 possible positions.
I need to use 8bit so what possible ways are there to increase the number of possible positions?
What can I do, add timeouts? how does it work?
Or like allocate all 8 bit into the period from 1 to 2ms. How do i do it? i was thinking about updating the timer every 2 ms. I dont know if my thought process is correct, and I don't know how I would code this.
Could you hhelp me out please?

2. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
Could anybody help me on this please? I really do not know what to do. Easy way out, I use the 16-bit, but I can't for different unimportant reasons

Oct 18, 2012
3,531
675
4. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
Thanks for the reply!
I read both of them, it does not talk much about the 8-bit resolution though. I am trying to improve the number of possible positions from 12-13 to more. Is that possible? I thought it had to do with the usage of interrupts, or changing the active pulse from 1-2ms to more. Or is it limited by the servo itself?

5. ### tshuck Well-Known Member

Oct 18, 2012
3,531
675
Second link, first two paragraphs... this is exactly what you are doing...

6. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
I understand.
Just when I use the 8-bit resolution, I used a laser to measure the distance on a wall, and from there, I calculate the angle of one step. It seems to me that the servo is able to move more than 13 positions within 90 degrees. How come? Also, I did not get how I can improve the number of positions from using 8-bit. Yeah, fast 16 PWM but I am limited to 8-bit only, unfortunately. Sorry if this seems so mundane, but I have no experience in this, even though I have read a few more of those articles about servos and the Arduino

7. ### tshuck Well-Known Member

Oct 18, 2012
3,531
675
To explain a little further, your servo accepts signals from 1ms to 2ms width. It is the resolution of the PWM mode that determines your ability to direct the servo to any specified angle(however, your servos ability to move to that angle is a different story...).

Now, provided that your servo has the ability to respond to higher resolution than what an 8-bit PWM can give you, you can use a timer that uses more bits.

If you don't want to go this route, you can use interrupts to create a higher resolution signal than what an 8-bit PWM would be able to do, determined by your configuration and your willingness to not use Arduino(I think you shouldn't anyway, but that's another story).

You can do software PWM without interrupts, but your jitter will be quite unstable, making your servo jump and twitch like a crack-head with withdrawals.

I would recommend trying to get a 16-bit PWM module.... If you aren't using a 16-bit PWM for "unimportant" reasons, use the 16-bit PWM for important reasons...

8. ### tshuck Well-Known Member

Oct 18, 2012
3,531
675
The servo controller measures the amount of time the control signal is high. From this, it turns the little motor until it's feedback tells the controller that the output shaft is at the desired position. If your uC can generate more steps between 1ms and 2ms, the better your resolution = the more steps you get(provided you haven't hit the ceiling for the servo controller)...

9. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
Thanks! that helped me understand everything better.
You said "If you don't want to go this route, you can use interrupts to create a higher resolution signal than what an 8-bit PWM would be able to do, determined by your configuration and your willingness to not use Arduino"
How can I use those interrupts? Am I able to implement those with an Arduino?
As for why I cannot use a 16-bit resolution, of course I can, but the question is why I can't use the 8-bit one (for educational purposes). Like something in between 10 and 16 bits without actually using 16 bits.

"If your uC can generate more steps between 1ms and 2ms, the better your resolution = the more steps you get(provided you haven't hit the ceiling for the servo controller)... "

That is what I am trying to do. How do I achieve that?
And, I know my servo is able to achieve more than 12-13 positions. How is that possible?

You have been very helpful so far, I would like to thank you for that!

10. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,435
1,305
I'm not tshuck, obviously, but maybe I can help a bit too.

To make the servo adopt a posiiton you need to make a HI pulse with a length of 1 to 2 mS. The length of the pulse determines the position (I think you got that part?).

1mS is 1000uS, and 2mS is 2000uS. Now, if you can make a pulse length adjustable in uS you can get 1000 different servo positions between 1mS and 2mS.

A very simple technique looks like this;
(Arduino timer is in uS units, this must be set up for your AVR chip)
1. set output pin HI
2. clear timer to 0
3. if timer <1000 then goto 3
4. make output pin LO

That simple sequence will make a HI pulse of 1000uS in length.

Obviously in step 3 you could change the 1000 to 1300 etc to get a different pulse length and make a different servo position.

11. ### John P AAC Fanatic!

Oct 14, 2008
1,638
225
I'm not an Arduino user and I don't know what it's capable of, but I do know that a PIC could operate this way, so here's a suggestion.

Use the PWM output with 8 bit resolution, just as you're doing now. But instead of running it with a 20msec period, run it with a 2msec period, and interrupt the processor after every cycle, i.e. 500/sec. In the interrupt, either set the PWM for a duty cycle of 50% to 100%, or for 0. On 9 cycles out of 10, the 0 setting would be used. On the 10th cycle, you'd be generating the servo pulse, so the range would be from 128 (= 1msec, one end of travel) to 255 (=2msec, other end of travel). So instead of 12.8 possible settings for the servo, you have 128.

Now, can the Arduino do this? That is the question.

12. ### tubeguy Well-Known Member

Nov 3, 2012
1,157
197
You could take John P's idea even further by interrupting the MCU at 20ms, setting the pulse HI, then interrupting or waiting (counting?) 1 more ms then start the PWM routine. I think this would let you use the full 255 counts.

13. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
That's what I was looking for. How did you get to that solution, though? I am interested how you got to this. I think the Arduino is able to do it, since it for sure outputs more than 13 positions. But how would I code it? I still do not understand everything that you have said, and even if I did, I would not know how to code it. I know that your solution is correct thought (or seems correct to me). I know that I need to study the fundamentals more, but this 'project' is part of a bigger project, that is more mechanically involved; I don't know much about the coding portion.
I want to try to write it in pseudocode, but I wouldnt know where to begin.
And tubeguy, that would definitely be good, but I first need to understand John P's answer :/
Thanks!!

14. ### Dualfire Thread Starter New Member

Dec 5, 2012
10
0
Anybody here know how I could code it? or describe it with pseudocode. that would help me a lot

15. ### tubeguy Well-Known Member

Nov 3, 2012
1,157
197
Dualfire likes this.
16. ### John P AAC Fanatic!

Oct 14, 2008
1,638
225
"How did you get to that solution" is really just getting the processor to do what you want. It's not the only way to do it: you could also just generate 2 time intervals, one of them for "output on" with a length between 1 and 2msec, and the other equal to 20msec minus "output on".

On the PIC processor, there's a timer (TMR2) that counts up to the value in a certain register, then repeats. Each time it repeats, there can be an interrupt, so it's an easy way to generate a repeating time interval. That same timer is associated with a PWM output, where you set another register which is compared with the first, where if the counting register is lower, the output bit is high. So if you set this comparison register to 10, and you'd chosen to have the timer count to 100, the output would be high for the first 10 counts, then low for 90. That would be a PWM output. So what I'm saying is, to get the resolution you need, make the counting faster, and use multiple timer cycles (10 looks as if it will work well) to make up 1 servo control cycle. Out of the 10, you use a single one to generate the servo control pulse, and the others will just be zero because the pulse is high no more than 10% of the time.

As for the coding, I don't do Arduinos. It's pretty easy stuff, but you need to have a basic understanding of how microcontrollers work. Sorry, there isn't any substitute for a little experience.

Dualfire likes this.