NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » Setting up dual servos in the ServoSquirter project

August 02, 2009
by Kelp
Kelp's Avatar

Ok, I've been working at it for hours now and I can't seem to set up the code to work with two servos. I'm not even really sure which pin the second one needs to be connected to. I've tried deciphering the datasheet, but I'm pretty sure that did more harm than good.

Any help or guidance at all would be greatly appreciated.



August 02, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi there,

Let me help try to get you started. You can activate the second output of Timer/Counter #1. Right now in the Servo Squirter code, the OCR1A register is used to set the "TOP" value of the counter, but if you instead use the ICR1 register for TOP, this will free up OCR1A to be another independent PWM output. Here's what you need to do:

  1. Change the line "OCR1A = 36864;" to "ICR1 = 36864;" (so that the 20ms limit for TOP is now stored in ICR1)
  2. Change the line "TCCR1A = (1<<COM1B1) | (1<<WGM11) | (1<<WGM10);" to "TCCR1A = (1<<COM1A1) | (1<<COM1B1) | (1<<WGM11);" (so that it looks for ICR1 as the TOP value, and so that OC1A is used for non-inverting PWM mode)
  3. Change the line "DDRB |= (1<<PB2) | (1<<PB3);" to "DDRB |= (1<<PB1) | (1<<PB2) | (1<<PB3);" (so that PB1 is also set as an output pin)

Now, you should be able to set OCR1A to some value, just as you set OCR1B in the pwm_set function, and this should provide PWM output on PB1.

Please give that a try and see if it works! Look at pages 131-133 in the ATmega168 datasheet if you want more details. Best,


August 03, 2009
by Kelp
Kelp's Avatar

Yes! Thank you!

I knew I had to open up a new PWM output, but I couldn't figure out how. Once I stored the TOP value in ICR1, the rest of the code worked perfectly

Thanks again!

April 01, 2011
by hariharan
hariharan's Avatar

What is the relation ship between OCR1A register and frequency? that is, what would i set the register to in order to output a specific frequency? I am a ametuer in programming and electronics, so i might need some 'extra' help. THANKS IN ADVANCE!

April 01, 2011
by mongo
mongo's Avatar

I did it for dual servos a while back and posted it in use on youtube. I'll look up the code this weekend and try to get it posted. It is designed to run from either a scripted batch file or directly from a keyboard through a terminal program.

Post a Reply

Please log in to post a reply.

Did you know that you can build a circuit to convert the "dit" and "dah" of Morse code back into letters automatically? Learn more...